-
Notifications
You must be signed in to change notification settings - Fork 1.6k
refactor(bigquery): update code samples to use strings for table and dataset IDs #9495
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
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b22b4ed
refactor(bigquery): update code samples
emar-kar 44c3e65
kms_key_name update
emar-kar 45d90de
refactor(bigquery): kms_key_name update
emar-kar 5631419
Merge branch 'new-samples' of https://github.com/q-logic/google-cloud…
emar-kar 5af72c8
samples update
emar-kar 1ab9eb7
docs session fix
emar-kar 14fbab4
docs session fix
emar-kar c1c2c7c
Merge branch 'new-samples' of https://github.com/q-logic/google-cloud…
emar-kar f15033c
update docs
emar-kar bfa3ec2
assertion update with re
emar-kar 9326f90
Merge branch 'master' into new-samples
emar-kar 0e50ead
docs(bigquery): remove location parameter from samples
plamut 04555f9
Pass location where auto-detection not supported
plamut b2cb5a1
update QueryJobConfig
emar-kar 08a1a2f
Merge branch 'master' into new-samples
emar-kar 1c135d7
unify undelete_table sample
emar-kar 1eb6a25
refactor: update test files with the new conditions
emar-kar 16dd3f8
refactor: remove extra blank line in test files
emar-kar 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,52 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_add_column(client, table_id): | ||
|
|
||
| # [START bigquery_add_column_query_append] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| # TODO(developer): Set table_id to the ID of the destination table. | ||
| # table_id = "your-project.your_dataset.your_table_name" | ||
|
|
||
| # Retrieves the destination table and checks the length of the schema. | ||
| table = client.get_table(table_id) # Make an API request. | ||
| print("Table {} contains {} columns".format(table_id, len(table.schema))) | ||
|
|
||
| # Configures the query to append the results to a destination table, | ||
| # allowing field addition. | ||
| job_config = bigquery.QueryJobConfig(destination=table_id) | ||
| job_config.schema_update_options = [ | ||
| bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION | ||
| ] | ||
| job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query( | ||
| # In this example, the existing table contains only the 'full_name' and | ||
| # 'age' columns, while the results of this query will contain an | ||
| # additional 'favorite_color' column. | ||
| 'SELECT "Timmy" as full_name, 85 as age, "Blue" as favorite_color;', | ||
| job_config=job_config, | ||
| ) # Make an API request. | ||
| query_job.result() # Wait for the job to complete. | ||
|
|
||
| # Checks the updated length of the schema. | ||
| table = client.get_table(table_id) # Make an API request. | ||
| print("Table {} now contains {} columns".format(table_id, len(table.schema))) | ||
| # [END bigquery_add_column_query_append] |
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,46 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_batch(client): | ||
|
|
||
| # [START bigquery_query_batch] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| job_config = bigquery.QueryJobConfig( | ||
| # Run at batch priority, which won't count toward concurrent rate limit. | ||
| priority=bigquery.QueryPriority.BATCH | ||
| ) | ||
|
|
||
| sql = """ | ||
| SELECT corpus | ||
| FROM `bigquery-public-data.samples.shakespeare` | ||
| GROUP BY corpus; | ||
| """ | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query(sql, job_config=job_config) # Make an API request. | ||
|
|
||
| # Check on the progress by getting the job's updated state. Once the state | ||
| # is `DONE`, the results are ready. | ||
| query_job = client.get_job( | ||
| query_job.job_id, location=query_job.location | ||
| ) # Make an API request. | ||
|
|
||
| print("Job {} is currently in state {}".format(query_job.job_id, query_job.state)) | ||
| # [END bigquery_query_batch] | ||
| return query_job |
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,40 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_destination_table(client, table_id): | ||
|
|
||
| # [START bigquery_query_destination_table] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| # TODO(developer): Set table_id to the ID of the destination table. | ||
| # table_id = "your-project.your_dataset.your_table_name" | ||
|
|
||
| job_config = bigquery.QueryJobConfig(destination=table_id) | ||
|
|
||
| sql = """ | ||
| SELECT corpus | ||
| FROM `bigquery-public-data.samples.shakespeare` | ||
| GROUP BY corpus; | ||
| """ | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query(sql, job_config=job_config) # Make an API request. | ||
| query_job.result() # Wait for the job to complete. | ||
|
|
||
| print("Query results loaded to the table {}".format(table_id)) | ||
| # [END bigquery_query_destination_table] |
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,49 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_destination_table_cmek(client, table_id, kms_key_name): | ||
|
|
||
| # [START bigquery_query_destination_table_cmek] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| # TODO(developer): Set table_id to the ID of the destination table. | ||
| # table_id = "your-project.your_dataset.your_table_name" | ||
|
|
||
| # Set the encryption key to use for the destination. | ||
| # TODO(developer): Replace this key with a key you have created in KMS. | ||
| # kms_key_name = "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}".format( | ||
| # your-project, location, your-ring, your-key | ||
| # ) | ||
emar-kar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| job_config = bigquery.QueryJobConfig( | ||
| destination=table_id, | ||
| destination_encryption_configuration=bigquery.EncryptionConfiguration( | ||
| kms_key_name=kms_key_name | ||
| ), | ||
| ) | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query( | ||
| "SELECT 17 AS my_col;", job_config=job_config | ||
| ) # Make an API request. | ||
| query_job.result() # Wait for the job to complete. | ||
|
|
||
| table = client.get_table(table_id) # Make an API request. | ||
| if table.encryption_configuration.kms_key_name == kms_key_name: | ||
| print("The destination table is written using the encryption configuration") | ||
| # [END bigquery_query_destination_table_cmek] | ||
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,44 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_destination_table_legacy(client, table_id): | ||
|
|
||
| # [START bigquery_query_legacy_large_results] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| # TODO(developer): Set table_id to the ID of the destination table. | ||
| # table_id = "your-project.your_dataset.your_table_name" | ||
|
|
||
| # Set the destination table and use_legacy_sql to True to use | ||
| # legacy SQL syntax. | ||
| job_config = bigquery.QueryJobConfig( | ||
| allow_large_results=True, destination=table_id, use_legacy_sql=True | ||
| ) | ||
|
|
||
| sql = """ | ||
| SELECT corpus | ||
| FROM [bigquery-public-data:samples.shakespeare] | ||
| GROUP BY corpus; | ||
| """ | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query(sql, job_config=job_config) # Make an API request. | ||
| query_job.result() # Wait for the job to complete. | ||
|
|
||
| print("Query results loaded to the table {}".format(table_id)) | ||
| # [END bigquery_query_legacy_large_results] |
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,40 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_dry_run(client): | ||
|
|
||
| # [START bigquery_query_dry_run] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| job_config = bigquery.QueryJobConfig(dry_run=True, use_query_cache=False) | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query( | ||
| ( | ||
| "SELECT name, COUNT(*) as name_count " | ||
| "FROM `bigquery-public-data.usa_names.usa_1910_2013` " | ||
| "WHERE state = 'WA' " | ||
| "GROUP BY name" | ||
| ), | ||
| job_config=job_config, | ||
| ) # Make an API request. | ||
|
|
||
| # A dry run query completes immediately. | ||
| print("This query will process {} bytes.".format(query_job.total_bytes_processed)) | ||
| # [END bigquery_query_dry_run] | ||
| return query_job |
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,39 @@ | ||
| # Copyright 2019 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def client_query_legacy_sql(client): | ||
|
|
||
| # [START bigquery_query_legacy] | ||
| from google.cloud import bigquery | ||
|
|
||
| # TODO(developer): Construct a BigQuery client object. | ||
| # client = bigquery.Client() | ||
|
|
||
| query = ( | ||
| "SELECT name FROM [bigquery-public-data:usa_names.usa_1910_2013] " | ||
| 'WHERE state = "TX" ' | ||
| "LIMIT 100" | ||
| ) | ||
|
|
||
| # Set use_legacy_sql to True to use legacy SQL syntax. | ||
| job_config = bigquery.QueryJobConfig(use_legacy_sql=True) | ||
|
|
||
| # Start the query, passing in the extra configuration. | ||
| query_job = client.query(query, job_config=job_config) # Make an API request. | ||
|
|
||
| print("The query data:") | ||
| for row in query_job: | ||
| print(row) | ||
| # [END bigquery_query_legacy] |
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.
Uh oh!
There was an error while loading. Please reload this page.