Class: GoodData::CloudResources::RedshiftClient

Inherits:
CloudResourceClient show all
Defined in:
lib/gooddata/cloud_resources/redshift/redshift_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CloudResourceClient

descendants

Constructor Details

#initialize(options = {}) ⇒ RedshiftClient

Returns a new instance of RedshiftClient.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 27

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

  if options['redshift_client']['connection'].is_a?(Hash)
    @database = options['redshift_client']['connection']['database']
    @schema = options['redshift_client']['connection']['schema'] || 'public'
    @url = options['redshift_client']['connection']['url']
    @authentication = options['redshift_client']['connection']['authentication']
  else
    raise('Missing connection info for Redshift client')

  end
  @debug = options['debug'] == true || options['debug'] == 'true'

  Java.com.amazon.redshift.jdbc42.Driver
end

Class Method Details

.accept?(type) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 22

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

Instance Method Details

#connectObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 72

def connect
  full_url = build_url(@url, @database)
  GoodData.logger.info "Setting up connection to Redshift #{full_url}"

  prop = java.util.Properties.new
  if @authentication['basic']
    prop.setProperty('UID', @authentication['basic']['userName'])
    prop.setProperty('PWD', @authentication['basic']['password'])
  else
    prop.setProperty('AccessKeyID', @authentication['iam']['accessKeyId'])
    prop.setProperty('SecretAccessKey', @authentication['iam']['secretAccessKey'])
    prop.setProperty('DbUser', @authentication['iam']['dbUser'])
  end

  @connection = java.sql.DriverManager.getConnection(full_url, prop)
end

#realize_query(query, _params) ⇒ Object



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
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 44

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

  connect
  filename = "#{SecureRandom.urlsafe_base64(6)}_#{Time.now.to_i}.csv"
  measure = Benchmark.measure do
    statement = @connection.create_statement
    schema_sql = "set search_path to #{@schema}"
    statement.execute(schema_sql)

    has_result = statement.execute(query)
    if has_result
      result = statement.get_result_set
       = result.
      col_count = .column_count
      CSV.open(filename, 'wb') do |csv|
        csv << Array(1..col_count).map { |i| .get_column_name(i) } # build the header
        csv << Array(1..col_count).map { |i| result.get_string(i)&.to_s } while result.next
      end
    end
  end
  GoodData.gd_logger.info("Realize SQL query: type=redshift status=finished duration=#{measure.real}")
  filename
ensure
  @connection.close unless @connection.nil?
  @connection = nil
end