Class: GoodData::CloudResources::RedshiftClient
- Inherits:
-
CloudResourceClient
- Object
- CloudResourceClient
- GoodData::CloudResources::RedshiftClient
- Defined in:
- lib/gooddata/cloud_resources/redshift/redshift_client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(options = {}) ⇒ RedshiftClient
constructor
A new instance of RedshiftClient.
- #realize_query(query, _params) ⇒ Object
Methods inherited from CloudResourceClient
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 43 |
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 27 def initialize( = {}) raise("Data Source needs a client to Redshift to be able to query the storage but 'redshift_client' is empty.") unless ['redshift_client'] if ['redshift_client']['connection'].is_a?(Hash) @database = ['redshift_client']['connection']['database'] @schema = ['redshift_client']['connection']['schema'] || 'public' @url = ['redshift_client']['connection']['url'] @authentication = ['redshift_client']['connection']['authentication'] else raise('Missing connection info for Redshift client') end @debug = ['debug'] == true || ['debug'] == 'true' # When update driver class then also updating driver class using in connection(..) method below Java.com.amazon.redshift.jdbc42.Driver end |
Class Method Details
.accept?(type) ⇒ Boolean
22 23 24 |
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 22 def accept?(type) type == 'redshift' end |
Instance Method Details
#connect ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 73 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 = com.amazon.redshift.jdbc42.Driver.new.connect(full_url, prop) end |
#realize_query(query, _params) ⇒ Object
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 |
# File 'lib/gooddata/cloud_resources/redshift/redshift_client.rb', line 45 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 |