class GoodData::CloudResources::BigQueryClient

Public Class Methods

accept?(type) click to toggle source
# File lib/gooddata/cloud_resources/bigquery/bigquery_client.rb, line 32
def accept?(type)
  type == 'bigquery'
end
new(options = {}) click to toggle source
# 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

Public Instance Methods

realize_query(query, _params) click to toggle source
# 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

Private Instance Methods

create_client() click to toggle source
# File lib/gooddata/cloud_resources/bigquery/bigquery_client.rb, line 77
def create_client
  GoodData.logger.info "Setting up connection to BigQuery"
  client_email = @authentication['serviceAccount']['clientEmail']
  private_key = @authentication['serviceAccount']['privateKey']
  credentials = ServiceAccountCredentials.fromPkcs8(nil, client_email, StringEscapeUtils.unescapeJson(private_key), nil, nil)
  BigQueryOptions.newBuilder.setProjectId(@project).setCredentials(credentials).build.getService
end