Class: Embulk::Input::InputBigquery
- Inherits:
-
InputPlugin
- Object
- InputPlugin
- Embulk::Input::InputBigquery
- Defined in:
- lib/embulk/input/bigquery.rb
Defined Under Namespace
Classes: LocalFile
Class Method Summary collapse
- .determine_columns_by_query_results(sql, option, bigquery_client) ⇒ Object
- .embulk_column_type(bq_data_type) ⇒ Object
- .resume(task, columns, count, &control) ⇒ Object
- .transaction(config, &control) ⇒ Object
Instance Method Summary collapse
- #as_serializable(v) ⇒ Object
- #keys_to_sym(hash) ⇒ Object
- #run ⇒ Object
- #values_to_sym(hashs, key) ⇒ Object
Class Method Details
.determine_columns_by_query_results(sql, option, bigquery_client) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/embulk/input/bigquery.rb', line 99 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' filtered_option = option.dup filtered_option.delete(:max) job = bigquery_client.query_job(sql, **filtered_option) 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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/embulk/input/bigquery.rb', line 123 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
65 66 67 68 69 |
# File 'lib/embulk/input/bigquery.rb', line 65 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 |
# 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] } } 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
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/embulk/input/bigquery.rb', line 157 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
142 143 144 145 146 147 148 |
# File 'lib/embulk/input/bigquery.rb', line 142 def keys_to_sym(hash) ret = {} hash.each do |key, value| ret[key.to_sym] = value end ret end |
#run ⇒ Object
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 |
# File 'lib/embulk/input/bigquery.rb', line 71 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? bq.query(@task[:sql], **option) else bq.job(@task[:job_id]).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
150 151 152 153 154 155 |
# File 'lib/embulk/input/bigquery.rb', line 150 def values_to_sym(hashs, key) hashs.map do |h| h[key] = h[key].to_sym h end end |