-
Notifications
You must be signed in to change notification settings - Fork 125
FEATURE: TMA-1630 Support Snowflake, BigQuery as input source #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.gooddata.lcm</groupId> | ||
| <artifactId>lcm-bigquery-driver</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.auth</groupId> | ||
| <artifactId>google-auth-library-oauth2-http</artifactId> | ||
| <version>0.16.2</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.commons</groupId> | ||
| <artifactId>commons-text</artifactId> | ||
| <version>1.7</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-bigquery</artifactId> | ||
| <version>1.102.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>binary-packaging</id> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-dependency-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>copy-dependencies</goal> | ||
| </goals> | ||
| <configuration> | ||
| <outputDirectory>${project.build.directory}</outputDirectory> | ||
| <!-- compile scope gives runtime and compile dependencies (skips test deps) --> | ||
| <includeScope>runtime</includeScope> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </profile> | ||
| </profiles> | ||
| </project> |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.gooddata.lcm</groupId> | ||
| <artifactId>lcm-snowflake-driver</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>net.snowflake</groupId> | ||
| <artifactId>snowflake-jdbc</artifactId> | ||
| <version>3.6.22</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| <version>1.7.2</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>binary-packaging</id> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-dependency-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>copy-dependencies</goal> | ||
| </goals> | ||
| <configuration> | ||
| <outputDirectory>${project.build.directory}</outputDirectory> | ||
| <!-- compile scope gives runtime and compile dependencies (skips test deps) --> | ||
| <includeScope>runtime</includeScope> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </profile> | ||
| </profiles> | ||
|
|
||
| <repositories> | ||
| <repository> | ||
| <id>my-repo1</id> | ||
| <name>my custom repo</name> | ||
| <url>https://repository.mulesoft.org/nexus/content/repositories/public/</url> | ||
| </repository> | ||
| </repositories> | ||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # encoding: UTF-8 | ||
| # | ||
| # Copyright (c) 2010-2019 GoodData Corporation. All rights reserved. | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| require 'securerandom' | ||
| require 'java' | ||
| require 'pathname' | ||
| require_relative '../cloud_resource_client' | ||
|
|
||
| base = Pathname(__FILE__).dirname.expand_path | ||
| Dir.glob(base + 'drivers/*.jar').each do |file| | ||
| require file unless file.start_with?('lcm-bigquery-driver') | ||
| end | ||
|
|
||
| java_import 'com.google.auth.oauth2.ServiceAccountCredentials' | ||
| java_import 'com.google.cloud.bigquery.BigQuery' | ||
| java_import 'com.google.cloud.bigquery.BigQueryOptions' | ||
| java_import 'com.google.cloud.bigquery.FieldList' | ||
| java_import 'com.google.cloud.bigquery.FieldValue' | ||
| java_import 'com.google.cloud.bigquery.FieldValueList' | ||
| java_import 'com.google.cloud.bigquery.QueryJobConfiguration' | ||
| java_import 'com.google.cloud.bigquery.TableResult' | ||
| java_import 'org.apache.commons.text.StringEscapeUtils' | ||
|
|
||
| module GoodData | ||
| module CloudResources | ||
| class BigQueryClient < CloudResourceClient | ||
| class << self | ||
| def accept?(type) | ||
| type == 'bigquery' | ||
| end | ||
| end | ||
|
|
||
| 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 | ||
|
|
||
| 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 > 0 | ||
| result = table_result.iterateAll | ||
| field_list = table_result.getSchema.getFields | ||
| col_count = field_list.size | ||
| CSV.open(filename, 'wb', :force_quotes => true) 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).getStringValue } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| GoodData.gd_logger.info("Realize SQL query: type=redshift status=finished duration=#{measure.real}") | ||
| filename | ||
| end | ||
|
|
||
| private | ||
|
|
||
| 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 | ||
| end | ||
| end | ||
| end | ||
Empty file.
Empty file.
83 changes: 83 additions & 0 deletions
83
lib/gooddata/cloud_resources/snowflake/snowflake_client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # encoding: UTF-8 | ||
| # | ||
| # Copyright (c) 2010-2019 GoodData Corporation. All rights reserved. | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| require 'securerandom' | ||
| require 'java' | ||
| require 'pathname' | ||
| require_relative '../cloud_resource_client' | ||
|
|
||
| base = Pathname(__FILE__).dirname.expand_path | ||
| Dir.glob(base + 'drivers/*.jar').each do |file| | ||
| require file unless file.start_with?('lcm-snowflake-driver') | ||
| end | ||
|
|
||
| module GoodData | ||
| module CloudResources | ||
| class SnowflakeClient < CloudResourceClient | ||
| class << self | ||
| def accept?(type) | ||
| type == 'snowflake' | ||
| end | ||
| end | ||
|
|
||
| def initialize(options = {}) | ||
| raise("Data Source needs a client to Snowflake to be able to query the storage but 'snowflake_client' is empty.") unless options['snowflake_client'] | ||
|
|
||
| if options['snowflake_client']['connection'].is_a?(Hash) | ||
| @database = options['snowflake_client']['connection']['database'] | ||
| @schema = options['snowflake_client']['connection']['schema'] || 'public' | ||
| @warehouse = options['snowflake_client']['connection']['warehouse'] | ||
| @url = options['snowflake_client']['connection']['url'] | ||
| @authentication = options['snowflake_client']['connection']['authentication'] | ||
| else | ||
| raise('Missing connection info for Snowflake client') | ||
|
|
||
| end | ||
|
|
||
| Java.net.snowflake.client.jdbc.SnowflakeDriver | ||
| end | ||
|
|
||
| def realize_query(query, _params) | ||
| GoodData.gd_logger.info("Realize SQL query: type=snowflake status=started") | ||
|
|
||
| connect | ||
| filename = "#{SecureRandom.urlsafe_base64(6)}_#{Time.now.to_i}.csv" | ||
| measure = Benchmark.measure do | ||
| statement = @connection.create_statement | ||
|
|
||
| has_result = statement.execute(query) | ||
| if has_result | ||
| result = statement.get_result_set | ||
| metadata = result.get_meta_data | ||
| col_count = metadata.column_count | ||
| CSV.open(filename, 'wb', :force_quotes => true) do |csv| | ||
| csv << Array(1..col_count).map { |i| metadata.get_column_name(i) } # build the header | ||
| csv << Array(1..col_count).map { |i| result.get_string(i) } while result.next | ||
| end | ||
| end | ||
| end | ||
| GoodData.gd_logger.info("Realize SQL query: type=snowflake status=finished duration=#{measure.real}") | ||
| filename | ||
| ensure | ||
| @connection.close unless @connection.nil? | ||
| @connection = nil | ||
| end | ||
|
|
||
| def connect | ||
| GoodData.logger.info "Setting up connection to Snowflake #{@url}" | ||
|
|
||
| prop = java.util.Properties.new | ||
| prop.setProperty('user', @authentication['basic']['userName']) | ||
| prop.setProperty('password', @authentication['basic']['password']) | ||
| prop.setProperty('schema', @schema) | ||
| prop.setProperty('warehouse', @warehouse) | ||
| prop.setProperty('db', @database) | ||
|
|
||
| @connection = java.sql.DriverManager.getConnection(@url, prop) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| "EmployeeID","Fullname","Salary","DOB","Departments","x__client_id","x__timestamp","x__deleted" | ||
| "E101","Name 01","4000","1970-01-20","101","mtt_testing_01","1557396000.001","false" | ||
| "E102","Name 02","3000","1918-05-26","102","mtt_testing_01","1557396000.002","false" | ||
| "E103","Name 03","5000","1960-02-03","103","mtt_testing_01","1557396000.003","false" | ||
| "E104","Name 04","5000","1960-02-03","104","MTT-Client02","1557396000.004","false" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| "CP__CUSTKEY","A__NAME","D__DATEADDED","X__TIMESTAMP" | ||
| "cus3","phong","2019-03-05","" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type=redshift --> type=bigquery