Class: Embulk::Input::InputBigquery

Inherits:
InputPlugin
  • Object
show all
Defined in:
lib/embulk/input/bigquery.rb

Defined Under Namespace

Classes: LocalFile

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.determine_columns_by_query_results(sql, option, bigquery_client) ⇒ Object



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
# File 'lib/embulk/input/bigquery.rb', line 108

def self.determine_columns_by_query_results(sql, option, bigquery_client)
  Embulk.logger.info 'determine columns using the getQueryResults API instead of the config.yml'

  query_option = option.dup
  query_option.delete(:max)
  query_option.delete(:location)
  job = bigquery_client.query_job(sql, **query_option) do |query|
    query.location = option[:location] if option[:location]
  end

  Embulk.logger.info 'waiting for the query job to complete to get schema from query results'
  job.wait_until_done!

  Embulk.logger.info "completed: job_id=#{job.job_id}"
  result = job.query_results(max: 0)

  columns = result.fields.map do |f|
    {
      'name' => f.name,
      'type' => embulk_column_type(f.type)
    }
  end
  Embulk.logger.info "determined columns: #{columns.inspect}"

  [job.job_id, columns]
end

.embulk_column_type(bq_data_type) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/embulk/input/bigquery.rb', line 135

def self.embulk_column_type(bq_data_type)
  case bq_data_type
  when 'BOOLEAN', 'BOOL'
    :boolean
  when 'INTEGER', 'INT64'
    :long
  when 'FLOAT', 'FLOAT64'
    :double
  when 'STRING', 'DATETIME', 'DATE', 'TIME'
    :string
  when 'TIMESTAMP'
    :timestamp
  when 'RECORD', 'BYTES'
    raise "unsupported type #{bq_data_type.inspect}"
  else
    raise "unknown type #{bq_data_type.inspect}"
  end
end

.resume(task, columns, count, &control) ⇒ Object



66
67
68
69
70
# File 'lib/embulk/input/bigquery.rb', line 66

def self.resume(task, columns, count, &control)
  task_reports = yield(task, columns, count)

  next_config_diff = {}
end

.transaction(config, &control) ⇒ Object



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
63
64
# File 'lib/embulk/input/bigquery.rb', line 23

def self.transaction(config, &control)
  sql = config[:sql]
  params = {}
  unless sql
    sql_erb = config[:sql_erb]
    erb = ERB.new(sql_erb)
    erb_params = config[:erb_params] || {}
    erb_params.each do |k, v|
      params[k] = eval(v)
    end

    sql = erb.result(binding)
  end

  task = {
    project: config[:project],
    keyfile: config.param(:keyfile, LocalFile, nil),
    sql: sql,
    params: params,
    option: {
      max: config[:max],
      cache: config[:cache],
      standard_sql: config[:standard_sql],
      legacy_sql: config[:legacy_sql],
      location: config[:location],
    }
  }

  if config[:columns]
    task[:columns] = config[:columns]
  else
    bq = Google::Cloud::Bigquery.new(project: task[:project], keyfile: task[:keyfile])
    task[:job_id], task[:columns] = determine_columns_by_query_results(sql, task[:option], bq)
  end

  columns = []
  task[:columns].each_with_index do |c, i|
    columns << Column.new(i, c['name'], c['type'].to_sym)
  end

  resume(task, columns, 1, &control)
end

Instance Method Details

#as_serializable(v) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/embulk/input/bigquery.rb', line 169

def as_serializable(v)
  case v
  when ::Google::Cloud::Bigquery::Time
    v.value
  when DateTime
    v.strftime('%Y-%m-%d %H:%M:%S.%6N')
  when Date
    v.strftime('%Y-%m-%d')
  else
    v
  end
end

#keys_to_sym(hash) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/embulk/input/bigquery.rb', line 154

def keys_to_sym(hash)
  ret = {}
  hash.each do |key, value|
    ret[key.to_sym] = value
  end
  ret
end

#runObject



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
# File 'lib/embulk/input/bigquery.rb', line 72

def run
  bq = Google::Cloud::Bigquery.new(project: task[:project], keyfile: task[:keyfile])
  params = @task[:params]
  option = keys_to_sym(@task[:option])

  rows = if @task[:job_id].nil?
           query_option = option.dup
           query_option.delete(:location)

           bq.query(@task[:sql], **query_option) do |job_updater|
             job_updater.location = option[:location] if option[:location]
           end
         else
           job_option = {}
           job_option[:location] = option[:location] if option[:location]

           bq.job(@task[:job_id], **job_option).query_results(max: option[:max])
         end

  @task[:columns] = values_to_sym(@task[:columns], 'name')

  rows.all do |row|
    columns = []
    @task[:columns].each do |c|
      val = row[c['name'].to_sym]
      val = eval(c['eval'], binding) if c['eval']

      columns << as_serializable(val)
    end

    @page_builder.add(columns)
  end
  @page_builder.finish
  {}
end

#values_to_sym(hashs, key) ⇒ Object



162
163
164
165
166
167
# File 'lib/embulk/input/bigquery.rb', line 162

def values_to_sym(hashs, key)
  hashs.map do |h|
    h[key] = h[key].to_sym
    h
  end
end