-
Notifications
You must be signed in to change notification settings - Fork 139
ISSUE-41: Support storage api #61
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| *.egg | ||
| /.env/ | ||
| /.vscode/ | ||
| /.idea/ | ||
| /.mypy_cache/ | ||
| *.pyc | ||
| .cache/ | ||
| *.iml | ||
|
|
||
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| sqlalchemy>=1.1.9 | ||
| google-cloud-bigquery>=1.6.0 | ||
| google-cloud-bigquery>=1.12.0 | ||
| future==0.16.0 | ||
| packaging>=20.0 | ||
|
|
||
| pytest==3.2.2 | ||
| pytz==2017.2 | ||
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 |
|---|---|---|
|
|
@@ -3,12 +3,21 @@ | |
| from __future__ import absolute_import | ||
| from __future__ import unicode_literals | ||
|
|
||
| import re | ||
| import warnings | ||
| from packaging.version import Version, parse as parse_version | ||
|
|
||
| from google import auth | ||
| from google.cloud import bigquery | ||
| from google.cloud.bigquery import dbapi, QueryJobConfig | ||
|
|
||
| try: | ||
| from google.cloud import bigquery_storage_v1 | ||
| except ImportError: | ||
| pass | ||
|
|
||
| from google.cloud.bigquery import dbapi | ||
| from google.cloud.bigquery.schema import SchemaField | ||
| from google.cloud.bigquery.table import EncryptionConfiguration | ||
| from google.cloud.bigquery.dataset import DatasetReference | ||
| from google.cloud.bigquery.table import EncryptionConfiguration, TableReference | ||
| from google.oauth2 import service_account | ||
| from google.api_core.exceptions import NotFound | ||
| from sqlalchemy.exc import NoSuchTableError | ||
|
|
@@ -18,7 +27,6 @@ | |
| from sqlalchemy.engine.base import Engine | ||
| from sqlalchemy.sql.schema import Column | ||
| from sqlalchemy.sql import elements | ||
| import re | ||
|
|
||
| from .parse_url import parse_url | ||
|
|
||
|
|
@@ -294,53 +302,67 @@ def _add_default_dataset_to_job_config(job_config, project_id, dataset_id): | |
|
|
||
| job_config.default_dataset = '{}.{}'.format(project_id, dataset_id) | ||
|
|
||
| @staticmethod | ||
| def create_credentials(credentials_path, credentials_info): | ||
| """ | ||
| Create a service account credentials object if possible. | ||
| Using a file is preffered over using info, | ||
| return None if both are not available. | ||
| """ | ||
| credentials = None | ||
|
|
||
| def _create_client_from_credentials(self, credentials, default_query_job_config, project_id): | ||
| if project_id is None: | ||
| project_id = credentials.project_id | ||
| if credentials_path: | ||
| credentials = service_account.Credentials.from_service_account_file(credentials_path) | ||
|
|
||
| scopes = ( | ||
| elif credentials_info: | ||
| credentials = service_account.Credentials.from_service_account_info(credentials_info) | ||
|
|
||
| if credentials: | ||
| scopes = ( | ||
| 'https://www.googleapis.com/auth/bigquery', | ||
| 'https://www.googleapis.com/auth/cloud-platform', | ||
| 'https://www.googleapis.com/auth/drive' | ||
| ) | ||
| credentials = credentials.with_scopes(scopes) | ||
|
|
||
| self._add_default_dataset_to_job_config(default_query_job_config, project_id, self.dataset_id) | ||
| credentials = credentials.with_scopes(scopes) | ||
|
|
||
| return bigquery.Client( | ||
| project=project_id, | ||
| credentials=credentials, | ||
| location=self.location, | ||
| default_query_job_config=default_query_job_config, | ||
| ) | ||
| return credentials | ||
|
|
||
| def create_connect_args(self, url): | ||
| project_id, location, dataset_id, arraysize, credentials_path, default_query_job_config = parse_url(url) | ||
| project_id, location, dataset_id, arraysize, credentials_path, use_bqstorage_api, default_query_job_config = parse_url(url) | ||
|
|
||
| self.arraysize = self.arraysize or arraysize | ||
| self.location = location or self.location | ||
| self.credentials_path = credentials_path or self.credentials_path | ||
| self.dataset_id = dataset_id | ||
|
|
||
| if self.credentials_path: | ||
| credentials = service_account.Credentials.from_service_account_file(self.credentials_path) | ||
| client = self._create_client_from_credentials(credentials, default_query_job_config, project_id) | ||
| credentials = self.create_credentials(self.credentials_path, self.credentials_info) | ||
|
|
||
| elif self.credentials_info: | ||
| credentials = service_account.Credentials.from_service_account_info(self.credentials_info) | ||
| client = self._create_client_from_credentials(credentials, default_query_job_config, project_id) | ||
| # Use the credentials project as a default if no project was specefied | ||
| if (credentials is not None) and (project_id is None): | ||
| project_id = credentials.project_id | ||
|
|
||
| else: | ||
| self._add_default_dataset_to_job_config(default_query_job_config, project_id, dataset_id) | ||
| self._add_default_dataset_to_job_config(default_query_job_config, project_id, dataset_id) | ||
|
|
||
| client = bigquery.Client( | ||
| project=project_id, | ||
| location=self.location, | ||
| default_query_job_config=default_query_job_config | ||
| ) | ||
| client = bigquery.Client( | ||
| project=project_id, | ||
| credentials=credentials, | ||
| location=self.location, | ||
| default_query_job_config=default_query_job_config | ||
| ) | ||
|
|
||
| clients = [client] | ||
|
|
||
| if use_bqstorage_api: | ||
| if parse_version(bigquery.__version__) >= Version("1.26.0"): | ||
| try: | ||
| storage_client = bigquery_storage_v1.BigQueryReadClient(credentials=credentials) | ||
| clients.append(storage_client) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| except NameError: | ||
| warnings.warn("It is not possible to use the bqstorage api without installing the bqstorage extra requirement") | ||
| else: | ||
| warnings.warn('It is not possible to use the bqstorage api with google-cloud-bigquery < 1.26.0') | ||
|
|
||
| return ([client], {}) | ||
| return (clients, {}) | ||
|
|
||
| def _json_deserializer(self, row): | ||
| """JSON deserializer for RECORD types. | ||
|
|
||
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
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.
the module doesn't work with anything below that ( also in master)