class Egis::Database

Interface for database manipulation and querying.

Extends the interface of {Egis::Client} but all the queries scheduled using {Egis::Database} are executed within the database's context. SQL table references without explicit database will implicitly refer to the database they are executed from.

It is recommended to create database objects using {Egis::Client#database} method.

@!attribute [r] name

@return [String] Athena database name

Attributes

client[R]
name[R]
output_downloader[R]

Public Class Methods

new(name, client: Egis::Client.new, output_downloader: Egis::OutputDownloader.new(client.aws_s3_client)) click to toggle source
# File lib/egis/database.rb, line 17
def initialize(name, client: Egis::Client.new, output_downloader: Egis::OutputDownloader.new(client.aws_s3_client))
  @client = client
  @output_downloader = output_downloader
  @name = name
end

Public Instance Methods

create() click to toggle source

Creates database in Athena.

@return [void]

# File lib/egis/database.rb, line 44
def create
  log_database_creation

  client.execute_query("CREATE DATABASE IF NOT EXISTS #{translate_name(name)};", async: false,
                                                                                 system_execution: true)
end
create!() click to toggle source

The same as {#create} but raising error if it already exists.

@return [void]

# File lib/egis/database.rb, line 56
def create!
  log_database_creation

  client.execute_query("CREATE DATABASE #{translate_name(name)};", async: false, system_execution: true)
end
drop() click to toggle source

Removes database in Athena.

@return [void]

# File lib/egis/database.rb, line 67
def drop
  log_database_removal

  client.execute_query("DROP DATABASE IF EXISTS #{translate_name(name)} CASCADE;", async: false,
                                                                                   system_execution: true)
end
drop!() click to toggle source

The same as {#drop} but raising error if it the database does not exist.

@return [void]

# File lib/egis/database.rb, line 79
def drop!
  log_database_removal

  client.execute_query("DROP DATABASE #{translate_name(name)} CASCADE;", async: false, system_execution: true)
end
execute_query(query, **options) click to toggle source

(see Egis::Client#execute_query)

# File lib/egis/database.rb, line 88
def execute_query(query, **options)
  client.execute_query(query, **{database: name, **options})
end
exists?() click to toggle source

Checks whether database with such name exists in Athena.

@return [Boolean]

# File lib/egis/database.rb, line 104
def exists?
  query_status = client.execute_query("SHOW DATABASES LIKE '#{name}';", async: false, system_execution: true)
  parsed_result = output_downloader.download(query_status.output_location)
  parsed_result.flatten.include?(name)
end
query_status(query_id) click to toggle source

(see Egis::Client#query_status)

# File lib/egis/database.rb, line 95
def query_status(query_id)
  client.query_status(query_id)
end
table(table_name, table_schema, table_location, **options) click to toggle source

Creates {Egis::Table} object. Executing it doesn't create Athena table yet.

@param [String] table_name @param [Egis::TableSchema] table_schema @param [String] table_location S3 URL with table location (e.g. `s3://s3_bucket/table/location/`) @param [:tsv, :csv, :orc, {serde: 'SerdeClass', serde_properties: {property: value}}] format Table format (defaults to :tsv) @return [Egis::Table]

# File lib/egis/database.rb, line 35
def table(table_name, table_schema, table_location, **options)
  Table.new(self, table_name, table_schema, table_location, client: client, options: options)
end

Private Instance Methods

log_database_creation() click to toggle source
# File lib/egis/database.rb, line 114
def log_database_creation
  Egis.logger.info { "Creating database #{name}" }
end
log_database_removal() click to toggle source
# File lib/egis/database.rb, line 118
def log_database_removal
  Egis.logger.info { "Removing database #{name}" }
end
translate_name(name) click to toggle source
# File lib/egis/database.rb, line 122
def translate_name(name)
  Egis.mode.database_name(name)
end