Module: PatriotGCP::Ext::BigQuery

Included in:
Command::BQCommand, Command::LoadToBigQueryCommand
Defined in:
lib/patriot_gcp/ext/bigquery.rb

Defined Under Namespace

Classes: BigQueryException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



9
10
11
# File 'lib/patriot_gcp/ext/bigquery.rb', line 9

def self.included(cls)
  cls.send(:include, Patriot::Util::System)
end

Instance Method Details

#bq(bigquery_keyfile, project_id, statement) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/patriot_gcp/ext/bigquery.rb', line 85

def bq(bigquery_keyfile, project_id, statement)
  ENV['BIGQUERY_KEYFILE'] = bigquery_keyfile

  bigquery = Google::Cloud::Bigquery.new(
    project: project_id,
    retries: 3
  )

  job = bigquery.query_job statement

  job.wait_until_done!

  if job.failed?
    raise BigQueryException, "statement execution failed: #{job.errors}"
  else
    return job.statistics
  end
end

#bq_load(filename, bigquery_keyfile, project_id, dataset_id, table_id, schema, options = nil, polling_interval = nil) ⇒ Object



15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/patriot_gcp/ext/bigquery.rb', line 15

def bq_load(filename,
            bigquery_keyfile,
            project_id,
            dataset_id,
            table_id,
            schema,
            options=nil,
            polling_interval=nil)

  options ||= {}
  polling_interval ||= 60

  ENV['BIGQUERY_KEYFILE'] = bigquery_keyfile

  bigquery = Google::Cloud::Bigquery.new(
    project: project_id,
    retries: 3,
    timeout: polling_interval * 60
  )

  # exclude partition string
  # table_name$YYYYMMDD -> table_name
  original_table_id = table_id.split('$')[0]

  dataset = bigquery.dataset dataset_id
  table = dataset.table original_table_id

  if table.nil?
    # TODO: 
    # schemaとoptionがメソッドやその引数で指定されるようになっており、
    # 大幅な仕様変更となっているが、旧ライブラリ同様の設定を読み込めるよう
    # 議論されている。
    # https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/1919
    # 
    # こちらが対応された場合は下記ソースを変更する。
    dataset.create_table original_table_id do |updater|
      updater.schema do |scm|
        schema['fields'].each do |row|
          name = row['name']
          type = row['type'].downcase.to_sym
          mode = row['mode'].downcase.to_sym if row['mode']

          scm.method(type).call(name, mode: mode)
        end
      end
      # 取り込み時間分割テーブルに設定
      updater.time_partitioning_type = "DAY"
    end
  end

  job = dataset.load_job(
    table_id,
    filename,
    format: options['format'] || nil,
    quote: options['quote'] || nil,
    skip_leading: options['skipLeadingRows'] || nil,
    write: options['writeDisposition'] || nil,
    delimiter: options['fieldDelimiter'] || nil,
    null_marker: options['nullMarker'] || nil,
  )

  job.wait_until_done!

  if job.failed?
    raise BigQueryException, "upload failed: #{job.errors}"
  else
    return job.statistics
  end
end