module Fluent::BigQueryOutput::InsertImplementation

Public Instance Methods

_write(chunk, table_format, template_suffix_format) click to toggle source
# File lib/fluent/plugin/out_bigquery.rb, line 409
def _write(chunk, table_format, template_suffix_format)
  rows = []
  chunk.msgpack_each do |row_object|
    # TODO: row size limit
    rows << row_object.deep_symbolize_keys
  end

  now = Time.at(Fluent::Engine.now)
  group = rows.group_by do |row|
    [
      generate_table_id(table_format, now, row, chunk),
      template_suffix_format ? generate_table_id(template_suffix_format, now, row, chunk) : nil,
    ]
  end
  group.each do |(table_id, template_suffix), group_rows|
    insert(table_id, group_rows, template_suffix)
  end
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_bigquery.rb, line 388
def format(tag, time, record)
  fetch_schema if @template_suffix

  if @replace_record_key
    record = replace_record_key(record)
  end

  if @convert_hash_to_json
    record = convert_hash_to_json(record)
  end

  buf = String.new
  row = @fields.format(@add_time_field.call(record, time))
  unless row.empty?
    row = {"json" => row}
    row['insert_id'] = @get_insert_id.call(record) if @get_insert_id
    buf << row.to_msgpack
  end
  buf
end
insert(table_id, rows, template_suffix) click to toggle source
# File lib/fluent/plugin/out_bigquery.rb, line 428
def insert(table_id, rows, template_suffix)
  writer.insert_rows(@project, @dataset, table_id, rows, skip_invalid_rows: @skip_invalid_rows, ignore_unknown_values: @ignore_unknown_values, template_suffix: template_suffix, allow_retry_insert_errors: @allow_retry_insert_errors)
rescue Fluent::BigQuery::Error => e
  if @auto_create_table && e.status_code == 404 && /Not Found: Table/i =~ e.message
    # Table Not Found: Auto Create Table
    writer.create_table(@project, @dataset, table_id, @fields, time_partitioning_type: @time_partitioning_type, time_partitioning_expiration: @time_partitioning_expiration)
    raise "table created. send rows next time."
  end

  if e.retryable?
    raise e # TODO: error class
  else
    log.warn "do not retry insert", project_id: @project, dataset: @dataset, table: table_id, code: e.status_code, message: e.message, reason: e.reason
    if @secondary
      flush_secondary(@secondary)
    end
  end
end