Class: GoodData::CloudResources::BigQueryClient

Inherits:
CloudResourceClient show all
Defined in:
lib/gooddata/cloud_resources/bigquery/bigquery_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CloudResourceClient

descendants

Constructor Details

#initialize(options = {}) ⇒ BigQueryClient

Returns a new instance of BigQueryClient.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gooddata/cloud_resources/bigquery/bigquery_client.rb', line 37

def initialize(options = {})
  raise("Data Source needs a client to BigQuery to be able to query the storage but 'bigquery_client' is empty.") unless options['bigquery_client']

  if options['bigquery_client']['connection'].is_a?(Hash)
    @project = options['bigquery_client']['connection']['project']
    @schema = options['bigquery_client']['connection']['schema'] || 'public'
    @authentication = options['bigquery_client']['connection']['authentication']
  else
    raise('Missing connection info for BigQuery client')

  end
end

Class Method Details

.accept?(type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/gooddata/cloud_resources/bigquery/bigquery_client.rb', line 32

def accept?(type)
  type == 'bigquery'
end

Instance Method Details

#realize_query(query, _params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gooddata/cloud_resources/bigquery/bigquery_client.rb', line 50

def realize_query(query, _params)
  GoodData.gd_logger.info("Realize SQL query: type=bigquery status=started")

  client = create_client
  filename = "#{SecureRandom.urlsafe_base64(6)}_#{Time.now.to_i}.csv"
  measure = Benchmark.measure do
    query_config = QueryJobConfiguration.newBuilder(query).setDefaultDataset(@schema).build
    table_result = client.query(query_config)

    if table_result.getTotalRows.positive?
      result = table_result.iterateAll
      field_list = table_result.getSchema.getFields
      col_count = field_list.size
      CSV.open(filename, 'wb') do |csv|
        csv << Array(1..col_count).map { |i| field_list.get(i - 1).getName } # build the header
        result.each do |row|
          csv << Array(1..col_count).map { |i| row.get(i - 1).getValue&.to_s }
        end
      end
    end
  end
  GoodData.gd_logger.info("Realize SQL query: type=bigquery status=finished duration=#{measure.real}")
  filename
end