embulk-output-vertica 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/embulk-output-vertica.gemspec +1 -1
- data/lib/embulk/output/vertica.rb +34 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 758fc8b31ffadedaf028b5775fd8820867e2998b
|
4
|
+
data.tar.gz: 8be72ce20cfe83be5f9d7ef827f81a0c9691cfec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08188e9dcc7209b7317e8b4256eca1e262dcd5759d44a43a4223ea5ebb6dac8a7f205140d789816de2b1032dc95e0074a074c6e85f107e43beb7450be855614e
|
7
|
+
data.tar.gz: c7184dd4edae3f1a4c988e1ae62442ff634b72765e805e590fa365bb698594fd00e6ef5bf4ea98b39ffc2cec756726c3c2034e171f3f33d65e76446005499736
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "embulk-output-vertica"
|
3
|
-
spec.version = "0.2.
|
3
|
+
spec.version = "0.2.7"
|
4
4
|
spec.authors = ["eiji.sekiya", "Naotoshi Seo"]
|
5
5
|
spec.email = ["eiji.sekiya.0326@gmail.com", "sonots@gmail.com"]
|
6
6
|
spec.summary = "Vertica output plugin for Embulk"
|
@@ -46,31 +46,38 @@ module Embulk
|
|
46
46
|
unique_name = "%08x%08x" % [now.tv_sec, now.tv_nsec]
|
47
47
|
task['temp_table'] = "#{task['table']}_LOAD_TEMP_#{unique_name}"
|
48
48
|
|
49
|
-
sql_schema = self.to_sql_schema(schema, task['column_options'])
|
50
|
-
|
51
49
|
quoted_schema = ::Jvertica.quote_identifier(task['schema'])
|
52
50
|
quoted_table = ::Jvertica.quote_identifier(task['table'])
|
53
51
|
quoted_temp_table = ::Jvertica.quote_identifier(task['temp_table'])
|
54
52
|
|
53
|
+
sql_schema ||= self.existing_sql_schema(task) unless task['mode'] == 'REPLACE'
|
54
|
+
sql_schema ||= self.to_sql_schema(schema, task['column_options'])
|
55
|
+
sql_schema_expression = sql_schema.map {|name, type| "#{::Jvertica.quote_identifier(name)} #{type}" }.join(',')
|
56
|
+
|
55
57
|
connect(task) do |jv|
|
56
|
-
|
57
|
-
query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_table}])
|
58
|
-
end
|
58
|
+
# create a temp table
|
59
59
|
query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_temp_table}])
|
60
|
-
query(jv, %[CREATE TABLE #{quoted_schema}.#{quoted_temp_table} (#{
|
60
|
+
query(jv, %[CREATE TABLE #{quoted_schema}.#{quoted_temp_table} (#{sql_schema_expression})])
|
61
61
|
end
|
62
62
|
|
63
63
|
begin
|
64
|
-
#
|
65
|
-
task_reports = yield(task)
|
64
|
+
# insert data into a temp table
|
65
|
+
task_reports = yield(task) # obtain an array of task_reports where one report is of a task
|
66
66
|
Embulk.logger.info { "embulk-output-vertica: task_reports: #{task_reports.to_json}" }
|
67
|
+
|
67
68
|
connect(task) do |jv|
|
68
|
-
|
69
|
+
# create the target table if not exists or mode == replace
|
70
|
+
if task['mode'] == 'REPLACE'
|
71
|
+
query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_table}])
|
72
|
+
end
|
73
|
+
query(jv, %[CREATE TABLE IF NOT EXISTS #{quoted_schema}.#{quoted_table} (#{sql_schema_expression})])
|
74
|
+
# insert select from the temp table
|
69
75
|
query(jv, %[INSERT INTO #{quoted_schema}.#{quoted_table} SELECT * FROM #{quoted_schema}.#{quoted_temp_table}])
|
70
76
|
jv.commit
|
71
77
|
end
|
72
78
|
ensure
|
73
79
|
connect(task) do |jv|
|
80
|
+
# clean up the temp table
|
74
81
|
query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_temp_table}])
|
75
82
|
Embulk.logger.debug { "embulk-output-vertica: #{query(jv, %[SELECT * FROM #{quoted_schema}.#{quoted_table} LIMIT 10]).map {|row| row.to_h }.join("\n") rescue nil}" }
|
76
83
|
end
|
@@ -166,8 +173,8 @@ module Embulk
|
|
166
173
|
else
|
167
174
|
sql_type = to_sql_type(type)
|
168
175
|
end
|
169
|
-
|
170
|
-
end.
|
176
|
+
[column_name, sql_type]
|
177
|
+
end.to_h
|
171
178
|
end
|
172
179
|
|
173
180
|
def self.to_sql_type(type)
|
@@ -181,6 +188,22 @@ module Embulk
|
|
181
188
|
end
|
182
189
|
end
|
183
190
|
|
191
|
+
# Get sql schema if table exists
|
192
|
+
def self.existing_sql_schema(task)
|
193
|
+
quoted_schema = Jvertica.quote(task['schema'])
|
194
|
+
quoted_table = Jvertica.quote(task['table'])
|
195
|
+
sql = "SELECT column_name, data_type FROM v_catalog.columns " \
|
196
|
+
"WHERE table_schema = #{quoted_schema} AND table_name = #{quoted_table}"
|
197
|
+
|
198
|
+
sql_schema = {}
|
199
|
+
connect(task) do |jv|
|
200
|
+
result = query(jv, sql)
|
201
|
+
sql_schema = result.map {|row| [row[0], row[1]] }.to_h
|
202
|
+
end
|
203
|
+
Embulk.logger.info "embulk-output-vertica: existing_sql_schema: #{sql_schema}"
|
204
|
+
sql_schema.empty? ? nil : sql_schema
|
205
|
+
end
|
206
|
+
|
184
207
|
def self.query(conn, sql)
|
185
208
|
Embulk.logger.info "embulk-output-vertica: #{sql}"
|
186
209
|
conn.query(sql)
|