Class: Embulk::Output::Bigquery
- Inherits:
-
OutputPlugin
- Object
- OutputPlugin
- Embulk::Output::Bigquery
- Defined in:
- lib/embulk/output/bigquery.rb,
lib/embulk/output/bigquery/auth.rb,
lib/embulk/output/bigquery/helper.rb,
lib/embulk/output/bigquery/gcs_client.rb,
lib/embulk/output/bigquery/file_writer.rb,
lib/embulk/output/bigquery/google_client.rb,
lib/embulk/output/bigquery/bigquery_client.rb,
lib/embulk/output/bigquery/value_converter_factory.rb
Defined Under Namespace
Classes: Auth, BackendError, BigqueryClient, Error, FileWriter, GcsClient, GoogleClient, Helper, InternalError, JobTimeoutError, LocalFile, NotFoundError, RateLimitExceeded, ValueConverterFactory
Constant Summary collapse
- FILE_WRITER_KEY =
:embulk_output_bigquery_file_writer
Class Method Summary collapse
- .add_file_writer(file_writer) ⇒ Object
- .auto_create(task, bigquery) ⇒ Object
- .bigquery ⇒ Object
- .configure(config, schema, task_count) ⇒ Object
- .converters ⇒ Object
- .file_writers ⇒ Object
- .rehearsal_thread ⇒ Object
- .rehearsal_thread=(rehearsal_thread) ⇒ Object
- .reset_file_writers ⇒ Object
- .transaction(config, schema, task_count, &control) ⇒ Object
- .transaction_report(task, responses) ⇒ Object
Instance Method Summary collapse
- #abort ⇒ Object
-
#add(page) ⇒ Object
called for each page in each task.
-
#close ⇒ Object
called for each page in each task.
-
#commit ⇒ Object
called after processing all pages in each task, returns a task_report.
-
#file_writer ⇒ Object
Create one FileWriter object for one output thread, that is, share among tasks.
- #finish ⇒ Object
-
#initialize(task, schema, index) ⇒ Bigquery
constructor
instance is created on each task.
- #load_rehearsal ⇒ Object
Constructor Details
#initialize(task, schema, index) ⇒ Bigquery
instance is created on each task
472 473 474 475 476 477 478 |
# File 'lib/embulk/output/bigquery.rb', line 472 def initialize(task, schema, index) super if task['with_rehearsal'] and @index == 0 @rehearsaled = false end end |
Class Method Details
.add_file_writer(file_writer) ⇒ Object
448 449 450 451 452 |
# File 'lib/embulk/output/bigquery.rb', line 448 def self.add_file_writer(file_writer) @file_writers_mutex.synchronize do @file_writers << file_writer end end |
.auto_create(task, bigquery) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/embulk/output/bigquery.rb', line 292 def self.auto_create(task, bigquery) if task['auto_create_dataset'] bigquery.create_dataset(task['dataset']) else bigquery.get_dataset(task['dataset']) # raises NotFoundError end if task['mode'] == 'replace_backup' and task['dataset_old'] != task['dataset'] if task['auto_create_dataset'] bigquery.create_dataset(task['dataset_old'], reference: task['dataset']) else bigquery.get_dataset(task['dataset_old']) # raises NotFoundError end end temp_table_expiration = task['temporary_table_expiration'] = {'expiration_time' => temp_table_expiration} case task['mode'] when 'delete_in_advance' bigquery.delete_table_or_partition(task['table']) bigquery.create_table_if_not_exists(task['table']) when 'replace' bigquery.create_table_if_not_exists(task['temp_table'], options: ) bigquery.create_table_if_not_exists(task['table']) # needs for when task['table'] is a partition when 'append' bigquery.create_table_if_not_exists(task['temp_table'], options: ) bigquery.create_table_if_not_exists(task['table']) # needs for when task['table'] is a partition when 'replace_backup' bigquery.create_table_if_not_exists(task['temp_table'], options: ) bigquery.create_table_if_not_exists(task['table']) bigquery.create_table_if_not_exists(task['table_old'], dataset: task['dataset_old']) # needs for when a partition else # append_direct if task['auto_create_table'] bigquery.create_table_if_not_exists(task['table']) else bigquery.get_table(task['table']) # raises NotFoundError end end end |
.bigquery ⇒ Object
255 256 257 |
# File 'lib/embulk/output/bigquery.rb', line 255 def self.bigquery @bigquery end |
.configure(config, schema, task_count) ⇒ Object
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/embulk/output/bigquery.rb', line 33 def self.configure(config, schema, task_count) task = { 'mode' => config.param('mode', :string, :default => 'append'), 'auth_method' => config.param('auth_method', :string, :default => 'application_default'), 'json_keyfile' => config.param('json_keyfile', LocalFile, :default => nil), 'project' => config.param('project', :string, :default => nil), 'destination_project' => config.param('destination_project', :string, :default => nil), 'dataset' => config.param('dataset', :string), 'location' => config.param('location', :string, :default => nil), 'table' => config.param('table', :string), 'dataset_old' => config.param('dataset_old', :string, :default => nil), 'table_old' => config.param('table_old', :string, :default => nil), 'table_name_old' => config.param('table_name_old', :string, :default => nil), # lower version compatibility 'auto_create_dataset' => config.param('auto_create_dataset', :bool, :default => false), 'auto_create_table' => config.param('auto_create_table', :bool, :default => true), 'schema_file' => config.param('schema_file', :string, :default => nil), 'template_table' => config.param('template_table', :string, :default => nil), 'delete_from_local_when_job_end' => config.param('delete_from_local_when_job_end', :bool, :default => true), 'job_status_max_polling_time' => config.param('job_status_max_polling_time', :integer, :default => 3600), 'job_status_polling_interval' => config.param('job_status_polling_interval', :integer, :default => 10), 'is_skip_job_result_check' => config.param('is_skip_job_result_check', :bool, :default => false), 'with_rehearsal' => config.param('with_rehearsal', :bool, :default => false), 'rehearsal_counts' => config.param('rehearsal_counts', :integer, :default => 1000), 'abort_on_error' => config.param('abort_on_error', :bool, :default => nil), 'progress_log_interval' => config.param('progress_log_interval', :float, :default => nil), 'column_options' => config.param('column_options', :array, :default => []), 'default_timezone' => config.param('default_timezone', :string, :default => ValueConverterFactory::DEFAULT_TIMEZONE), 'default_timestamp_format' => config.param('default_timestamp_format', :string, :default => ValueConverterFactory::DEFAULT_TIMESTAMP_FORMAT), 'payload_column' => config.param('payload_column', :string, :default => nil), 'payload_column_index' => config.param('payload_column_index', :integer, :default => nil), 'description' => config.param('description', :string, :default => nil), 'open_timeout_sec' => config.param('open_timeout_sec', :integer, :default => nil), 'timeout_sec' => config.param('timeout_sec', :integer, :default => nil), # google-api-ruby-client < v0.11.0 'send_timeout_sec' => config.param('send_timeout_sec', :integer, :default => nil), # google-api-ruby-client >= v0.11.0 'read_timeout_sec' => config.param('read_timeout_sec', :integer, :default => nil), # google-api-ruby-client >= v0.11.0 'retries' => config.param('retries', :integer, :default => 5), 'application_name' => config.param('application_name', :string, :default => 'Embulk BigQuery plugin'), 'sdk_log_level' => config.param('sdk_log_level', :string, :default => nil), 'path_prefix' => config.param('path_prefix', :string, :default => nil), 'sequence_format' => config.param('sequence_format', :string, :default => '.%d.%d'), 'file_ext' => config.param('file_ext', :string, :default => nil), 'skip_file_generation' => config.param('skip_file_generation', :bool, :default => false), 'compression' => config.param('compression', :string, :default => 'NONE'), 'gcs_bucket' => config.param('gcs_bucket', :string, :default => nil), 'auto_create_gcs_bucket' => config.param('auto_create_gcs_bucket', :bool, :default => false), 'source_format' => config.param('source_format', :string, :default => 'CSV'), 'max_bad_records' => config.param('max_bad_records', :integer, :default => 0), 'field_delimiter' => config.param('field_delimiter', :string, :default => ','), 'encoding' => config.param('encoding', :string, :default => 'UTF-8'), 'ignore_unknown_values' => config.param('ignore_unknown_values', :bool, :default => false), 'allow_quoted_newlines' => config.param('allow_quoted_newlines', :bool, :default => false), 'time_partitioning' => config.param('time_partitioning', :hash, :default => nil), 'clustering' => config.param('clustering', :hash, :default => nil), # google-api-ruby-client >= v0.21.0 'schema_update_options' => config.param('schema_update_options', :array, :default => nil), 'temporary_table_expiration' => config.param('temporary_table_expiration', :integer, :default => nil), # for debug 'skip_load' => config.param('skip_load', :bool, :default => false), 'temp_table' => config.param('temp_table', :string, :default => nil), 'rehearsal_table' => config.param('rehearsal_table', :string, :default => nil), } now = Time.now task['mode'] = task['mode'].downcase unless %w[append append_direct replace delete_in_advance replace_backup].include?(task['mode']) raise ConfigError.new "`mode` must be one of append, append_direct, replace, delete_in_advance, replace_backup" end if %w[append replace delete_in_advance replace_backup].include?(task['mode']) and !task['auto_create_table'] raise ConfigError.new "`mode: #{task['mode']}` requires `auto_create_table: true`" end if task['mode'] == 'replace_backup' task['table_old'] ||= task['table_name_old'] # for lower version compatibility if task['dataset_old'].nil? and task['table_old'].nil? raise ConfigError.new "`mode: replace_backup` requires either of `dataset_old` or `table_old`" end task['dataset_old'] ||= task['dataset'] task['table_old'] ||= task['table'] end if task['table_old'] task['table_old'] = now.strftime(task['table_old']) end if task['table'] task['table'] = now.strftime(task['table']) end task['auth_method'] = task['auth_method'].downcase unless %w[json_key service_account authorized_user compute_engine application_default].include?(task['auth_method']) raise ConfigError.new "`auth_method` must be one of service_account (or json_key), authorized_user, compute_engine, application_default" end if (task['auth_method'] == 'service_account' or task['auth_method'] == 'json_key') and task['json_keyfile'].nil? raise ConfigError.new "`json_keyfile` is required for auth_method: service_account (or json_key)" end if task['json_keyfile'] begin json_key = JSON.parse(task['json_keyfile']) task['project'] ||= json_key['project_id'] rescue => e raise ConfigError.new "Parsing 'json_keyfile' failed with error: #{e.class} #{e.}" end end if task['project'].nil? raise ConfigError.new "Required field \"project\" is not set" end task['destination_project'] ||= task['project'] if (task['payload_column'] or task['payload_column_index']) and task['auto_create_table'] if task['schema_file'].nil? and task['template_table'].nil? raise ConfigError.new "Cannot guess table schema from Embulk schema with `payload_column` or `payload_column_index`. Either of `schema_file` or `template_table` is required for auto_create_table true" end end if task['payload_column_index'] if task['payload_column_index'] < 0 || schema.size <= task['payload_column_index'] raise ConfigError.new "payload_column_index #{task['payload_column_index']} is out of schema size" end elsif task['payload_column'] task['payload_column_index'] = schema.find_index {|c| c[:name] == task['payload_column'] } if task['payload_column_index'].nil? raise ConfigError.new "payload_column #{task['payload_column']} does not exist in schema" end end if task['schema_file'] unless File.exist?(task['schema_file']) raise ConfigError.new "schema_file #{task['schema_file']} is not found" end begin JSON.parse(File.read(task['schema_file'])) rescue => e raise ConfigError.new "Parsing 'schema_file' #{task['schema_file']} failed with error: #{e.class} #{e.}" end end if task['path_prefix'].nil? task['path_prefix'] = Tempfile.create('embulk_output_bigquery_') {|fp| fp.path } end task['source_format'] = task['source_format'].upcase if task['source_format'] == 'JSONL' task['source_format'] = 'NEWLINE_DELIMITED_JSON' end unless %w[CSV NEWLINE_DELIMITED_JSON].include?(task['source_format']) raise ConfigError.new "`source_format` must be CSV or NEWLINE_DELIMITED_JSON (JSONL)" end task['compression'] = task['compression'].upcase unless %w[GZIP NONE].include?(task['compression']) raise ConfigError.new "`compression` must be GZIP or NONE" end if task['file_ext'].nil? case task['source_format'] when 'CSV' file_ext = '.csv' else # newline_delimited_json file_ext = '.jsonl' end case task['compression'] when 'GZIP' file_ext << '.gz' end task['file_ext'] = file_ext end unique_name = SecureRandom.uuid.gsub('-', '_') if %w[replace replace_backup append].include?(task['mode']) task['temp_table'] ||= "LOAD_TEMP_#{unique_name}_#{task['table']}" else task['temp_table'] = nil end if task['with_rehearsal'] task['rehearsal_table'] ||= "LOAD_REHEARSAL_#{unique_name}_#{task['table']}" end if task['sdk_log_level'] Google::Apis.logger.level = eval("::Logger::#{task['sdk_log_level'].upcase}") end if task['abort_on_error'].nil? task['abort_on_error'] = (task['max_bad_records'] == 0) end if task['time_partitioning'] unless task['time_partitioning']['type'] raise ConfigError.new "`time_partitioning` must have `type` key" end elsif Helper.has_partition_decorator?(task['table']) task['time_partitioning'] = {'type' => 'DAY'} end if task['clustering'] unless task['clustering']['fields'] raise ConfigError.new "`clustering` must have `fields` key" end end if task['schema_update_options'] task['schema_update_options'].each do |schema_update_option| unless %w[ALLOW_FIELD_ADDITION ALLOW_FIELD_RELAXATION].include?(schema_update_option) raise ConfigError.new "`schema_update_options` must contain either of ALLOW_FIELD_ADDITION or ALLOW_FIELD_RELAXATION or both" end end end task end |
.converters ⇒ Object
259 260 261 |
# File 'lib/embulk/output/bigquery.rb', line 259 def self.converters @converters end |
.file_writers ⇒ Object
444 445 446 |
# File 'lib/embulk/output/bigquery.rb', line 444 def self.file_writers @file_writers end |
.rehearsal_thread ⇒ Object
263 264 265 |
# File 'lib/embulk/output/bigquery.rb', line 263 def self.rehearsal_thread @rehearsal_thread end |
.rehearsal_thread=(rehearsal_thread) ⇒ Object
267 268 269 |
# File 'lib/embulk/output/bigquery.rb', line 267 def self.rehearsal_thread=(rehearsal_thread) @rehearsal_thread = rehearsal_thread end |
.reset_file_writers ⇒ Object
440 441 442 |
# File 'lib/embulk/output/bigquery.rb', line 440 def self.reset_file_writers @file_writers = Array.new end |
.transaction(config, schema, task_count, &control) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/embulk/output/bigquery.rb', line 334 def self.transaction(config, schema, task_count, &control) task = self.configure(config, schema, task_count) @task = task @schema = schema @bigquery = BigqueryClient.new(task, schema) @converters = ValueConverterFactory.create_converters(task, schema) self.auto_create(@task, @bigquery) begin paths = [] if task['skip_file_generation'] yield(task) # does nothing, but seems it has to be called path_pattern = "#{task['path_prefix']}*#{task['file_ext']}" Embulk.logger.info { "embulk-output-bigquery: Skip file generation. Get paths from `#{path_pattern}`" } paths = Dir.glob(path_pattern) else task_reports = yield(task) # generates local files ios = file_writers.map(&:io) paths = ios.map(&:path) ios.each do |io| Embulk.logger.debug { "close #{io.path}" } io.close rescue nil end end if rehearsal_thread rehearsal_thread.join end if task['skip_load'] # only for debug Embulk.logger.info { "embulk-output-bigquery: Skip load" } else if !paths.empty? target_table = task['temp_table'] ? task['temp_table'] : task['table'] if bucket = task['gcs_bucket'] gcs = GcsClient.new(task) gcs.insert_temporary_bucket(bucket) if task['auto_create_gcs_bucket'] objects = paths.size.times.map { SecureRandom.uuid.to_s } gcs.insert_objects(paths, objects: objects, bucket: bucket) object_uris = objects.map {|object| URI.join("gs://#{bucket}", object).to_s } responses = bigquery.load_from_gcs(object_uris, target_table) objects.each {|object| gcs.delete_object(object, bucket: bucket) } else responses = bigquery.load_in_parallel(paths, target_table) end else responses = [] end transaction_report = self.transaction_report(task, responses) Embulk.logger.info { "embulk-output-bigquery: transaction_report: #{transaction_report.to_json}" } if task['abort_on_error'] && !task['is_skip_job_result_check'] if transaction_report['num_input_rows'] != transaction_report['num_output_rows'] raise Error, "ABORT: `num_input_rows (#{transaction_report['num_input_rows']})` and " \ "`num_output_rows (#{transaction_report['num_output_rows']})` does not match" end end if task['mode'] == 'replace_backup' begin bigquery.get_table_or_partition(task['table']) bigquery.copy(task['table'], task['table_old'], task['dataset_old']) rescue NotFoundError end end if task['temp_table'] if task['mode'] == 'append' bigquery.copy(task['temp_table'], task['table'], write_disposition: 'WRITE_APPEND') else # replace or replace_backup bigquery.copy(task['temp_table'], task['table'], write_disposition: 'WRITE_TRUNCATE') end end end ensure begin if task['temp_table'] # append or replace or replace_backup bigquery.delete_table(task['temp_table']) end ensure if task['delete_from_local_when_job_end'] paths.each do |path| Embulk.logger.info { "embulk-output-bigquery: delete #{path}" } File.unlink(path) rescue nil end else paths.each do |path| if File.exist?(path) Embulk.logger.info { "embulk-output-bigquery: keep #{path}" } end end end end end # this is for -c next_config option, add some paramters for next execution if wants next_config_diff = {} return next_config_diff end |
.transaction_report(task, responses) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/embulk/output/bigquery.rb', line 271 def self.transaction_report(task, responses) num_input_rows = file_writers.empty? ? 0 : file_writers.map(&:num_rows).inject(:+) return {'num_input_rows' => num_input_rows} if task['is_skip_job_result_check'] num_response_rows = responses.inject(0) do |sum, response| sum + (response ? response.statistics.load.output_rows.to_i : 0) end if task['temp_table'] num_output_rows = bigquery.get_table_or_partition(task['temp_table']).num_rows.to_i else num_output_rows = num_response_rows end num_rejected_rows = num_input_rows - num_output_rows transaction_report = { 'num_input_rows' => num_input_rows, 'num_response_rows' => num_response_rows, 'num_output_rows' => num_output_rows, 'num_rejected_rows' => num_rejected_rows, } end |
Instance Method Details
#abort ⇒ Object
524 525 |
# File 'lib/embulk/output/bigquery.rb', line 524 def abort end |
#add(page) ⇒ Object
called for each page in each task
485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/embulk/output/bigquery.rb', line 485 def add(page) return if task['skip_file_generation'] num_rows = file_writer.add(page) if task['with_rehearsal'] and @index == 0 and !@rehearsaled if num_rows >= task['rehearsal_counts'] load_rehearsal @rehearsaled = true end end end |
#close ⇒ Object
called for each page in each task
481 482 |
# File 'lib/embulk/output/bigquery.rb', line 481 def close end |
#commit ⇒ Object
called after processing all pages in each task, returns a task_report
528 529 530 |
# File 'lib/embulk/output/bigquery.rb', line 528 def commit {} end |
#file_writer ⇒ Object
Create one FileWriter object for one output thread, that is, share among tasks. Close theses shared objects in transaction. This is mainly to suppress (or control by -X max_threads) number of files, which equals to number of concurrency to load in parallel, when number of input tasks is many
#file_writer must be called at only #add because threads in other methods are different (called from non-output threads). Note also that #add method of the same task instance would be called in different output threads
464 465 466 467 468 469 |
# File 'lib/embulk/output/bigquery.rb', line 464 def file_writer return Thread.current[FILE_WRITER_KEY] if Thread.current[FILE_WRITER_KEY] file_writer = FileWriter.new(@task, @schema, @index, self.class.converters) self.class.add_file_writer(file_writer) Thread.current[FILE_WRITER_KEY] = file_writer end |
#finish ⇒ Object
521 522 |
# File 'lib/embulk/output/bigquery.rb', line 521 def finish end |
#load_rehearsal ⇒ Object
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/embulk/output/bigquery.rb', line 497 def load_rehearsal bigquery = self.class.bigquery Embulk.logger.info { "embulk-output-bigquery: Rehearsal started" } io = file_writer.close # need to close once for gzip rehearsal_path = "#{io.path}.rehearsal" Embulk.logger.debug { "embulk_output_bigquery: cp #{io.path} #{rehearsal_path}" } FileUtils.cp(io.path, rehearsal_path) file_writer.reopen self.class.rehearsal_thread = Thread.new do begin bigquery.create_table_if_not_exists(task['rehearsal_table']) response = bigquery.load(rehearsal_path, task['rehearsal_table']) num_output_rows = response ? response.statistics.load.output_rows.to_i : 0 Embulk.logger.info { "embulk-output-bigquery: Loaded rehearsal #{num_output_rows}" } ensure Embulk.logger.debug { "embulk_output_bigquery: delete #{rehearsal_path}" } File.unlink(rehearsal_path) rescue nil bigquery.delete_table(task['rehearsal_table']) end end end |