Class: Embulk::Output::Vertica
- Inherits:
-
OutputPlugin
- Object
- OutputPlugin
- Embulk::Output::Vertica
- Defined in:
- lib/embulk/output/vertica.rb
Defined Under Namespace
Classes: Error, NotSupportedType
Class Method Summary collapse
- .connect(task) ⇒ Object
-
.to_sql_schema(schema, column_options) ⇒ String
Sql schema used to CREATE TABLE.
- .to_sql_type(type) ⇒ Object
- .transaction(config, schema, processor_count, &control) ⇒ Object
Instance Method Summary collapse
- #abort ⇒ Object
- #add(page) ⇒ Object
- #close ⇒ Object
- #commit ⇒ Object
- #finish ⇒ Object
-
#initialize(task, schema, index) ⇒ Vertica
constructor
A new instance of Vertica.
Constructor Details
#initialize(task, schema, index) ⇒ Vertica
105 106 107 108 |
# File 'lib/embulk/output/vertica.rb', line 105 def initialize(task, schema, index) super @jv = self.class.connect(task) end |
Class Method Details
.connect(task) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/embulk/output/vertica.rb', line 64 def self.connect(task) jv = ::Jvertica.connect({ host: task['host'], port: task['port'], user: task['username'], password: task['password'], database: task['database'], }) if block_given? begin yield jv ensure jv.close end end jv end |
.to_sql_schema(schema, column_options) ⇒ String
86 87 88 89 90 91 92 |
# File 'lib/embulk/output/vertica.rb', line 86 def self.to_sql_schema(schema, ) schema.names.zip(schema.types).map do |column_name, type| sql_type = ([column_name] and [column_name]['type']) ? [column_name]['type'] : to_sql_type(type) "#{::Jvertica.quote_identifier(column_name)} #{sql_type}" end.join(',') end |
.to_sql_type(type) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/embulk/output/vertica.rb', line 94 def self.to_sql_type(type) case type when :boolean then 'BOOLEAN' when :long then 'INT' # BIGINT is a synonym for INT in vertica when :double then 'FLOAT' # DOUBLE PRECISION is a synonym for FLOAT in vertica when :string then 'VARCHAR' # LONG VARCHAR is not recommended when :timestamp then 'TIMESTAMP' else raise NotSupportedType, "embulk-output-vertica cannot take column type #{type}" end end |
.transaction(config, schema, processor_count, &control) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/embulk/output/vertica.rb', line 11 def self.transaction(config, schema, processor_count, &control) task = { 'host' => config.param('host', :string, :default => 'localhost'), 'port' => config.param('port', :integer, :default => 5433), 'username' => config.param('username', :string), 'password' => config.param('password', :string, :default => ''), 'database' => config.param('database', :string, :default => 'vdb'), 'schema' => config.param('schema', :string, :default => 'public'), 'table' => config.param('table', :string), 'copy_mode' => config.param('copy_mode', :string, :default => 'AUTO'), 'abort_on_error' => config.param('abort_on_error', :bool, :default => false), 'column_options' => config.param('column_options', :hash, :default => {}), } unless %w[AUTO DIRECT TRICKLE].include?(task['copy_mode'].upcase) raise ConfigError, "`copy_mode` must be one of AUTO, DIRECT, TRICKLE" end now = Time.now unique_name = "%08x%08x" % [now.tv_sec, now.tv_nsec] task['temp_table'] = "#{task['table']}_LOAD_TEMP_#{unique_name}" sql_schema = self.to_sql_schema(schema, task['column_options']) quoted_schema = ::Jvertica.quote_identifier(task['schema']) quoted_table = ::Jvertica.quote_identifier(task['table']) quoted_temp_table = ::Jvertica.quote_identifier(task['temp_table']) connect(task) do |jv| # drop table if exists "DEST" # 'create table if exists "TEMP" ("COL" json)' jv.query %[drop table if exists #{quoted_schema}.#{quoted_temp_table}] jv.query %[create table #{quoted_schema}.#{quoted_temp_table} (#{sql_schema})] end begin yield(task) connect(task) do |jv| # create table if not exists "DEST" ("COL" json) # 'insert into "DEST" ("COL") select "COL" from "TEMP"' jv.query %[create table if not exists #{quoted_schema}.#{quoted_table} (#{sql_schema})] jv.query %[insert into #{quoted_schema}.#{quoted_table} select * from #{quoted_schema}.#{quoted_temp_table}] jv.commit end ensure connect(task) do |jv| # 'drop table if exists TEMP' jv.query %[drop table if exists #{quoted_schema}.#{quoted_temp_table}] end end return {} end |
Instance Method Details
#abort ⇒ Object
126 127 |
# File 'lib/embulk/output/vertica.rb', line 126 def abort end |
#add(page) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/embulk/output/vertica.rb', line 114 def add(page) @jv.copy(copy_sql) do |stdin| page.each_with_index do |record, idx| stdin << record.map {|v| ::Jvertica.quote(v) }.join(",") << "\n" end end @jv.commit end |
#close ⇒ Object
110 111 112 |
# File 'lib/embulk/output/vertica.rb', line 110 def close @jv.close end |
#commit ⇒ Object
129 130 131 |
# File 'lib/embulk/output/vertica.rb', line 129 def commit {} end |
#finish ⇒ Object
123 124 |
# File 'lib/embulk/output/vertica.rb', line 123 def finish end |