diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 4c39ff912230..cc25db138d35 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -13,11 +13,9 @@ # limitations under the License. """Testable usage examples for Google BigQuery API wrapper - Each example function takes a ``client`` argument (which must be an instance of :class:`google.cloud.bigquery.client.Client`) and uses it to perform a task with the API. - To facilitate running the examples as system tests, each example is also passed a ``to_delete`` list; the function adds to the list any objects created which need to be deleted during teardown. @@ -303,47 +301,6 @@ def test_load_and_query_partitioned_table(client, to_delete): assert len(rows) == 29 -# [START bigquery_table_exists] -def table_exists(client, table_reference): - """Return if a table exists. - - Args: - client (google.cloud.bigquery.client.Client): - A client to connect to the BigQuery API. - table_reference (google.cloud.bigquery.table.TableReference): - A reference to the table to look for. - - Returns: - bool: ``True`` if the table exists, ``False`` otherwise. - """ - from google.cloud.exceptions import NotFound - - try: - client.get_table(table_reference) - return True - except NotFound: - return False - - -# [END bigquery_table_exists] - - -def test_table_exists(client, to_delete): - """Determine if a table exists.""" - DATASET_ID = "get_table_dataset_{}".format(_millis()) - TABLE_ID = "get_table_table_{}".format(_millis()) - dataset = bigquery.Dataset(client.dataset(DATASET_ID)) - dataset = client.create_dataset(dataset) - to_delete.append(dataset) - - table_ref = dataset.table(TABLE_ID) - table = bigquery.Table(table_ref, schema=SCHEMA) - table = client.create_table(table) - - assert table_exists(client, table_ref) - assert not table_exists(client, dataset.table("i_dont_exist")) - - @pytest.mark.skip( reason=( "update_table() is flaky " @@ -698,36 +655,6 @@ def test_manage_views(client, to_delete): # [END bigquery_grant_view_access] -def test_table_insert_rows(client, to_delete): - """Insert / fetch table data.""" - dataset_id = "table_insert_rows_dataset_{}".format(_millis()) - table_id = "table_insert_rows_table_{}".format(_millis()) - dataset = bigquery.Dataset(client.dataset(dataset_id)) - dataset = client.create_dataset(dataset) - dataset.location = "US" - to_delete.append(dataset) - - table = bigquery.Table(dataset.table(table_id), schema=SCHEMA) - table = client.create_table(table) - - # [START bigquery_table_insert_rows] - # TODO(developer): Uncomment the lines below and replace with your values. - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_id = 'my_dataset' # replace with your dataset ID - # For this sample, the table must already exist and have a defined schema - # table_id = 'my_table' # replace with your table ID - # table_ref = client.dataset(dataset_id).table(table_id) - # table = client.get_table(table_ref) # API request - - rows_to_insert = [(u"Phred Phlyntstone", 32), (u"Wylma Phlyntstone", 29)] - - errors = client.insert_rows(table, rows_to_insert) # API request - - assert errors == [] - # [END bigquery_table_insert_rows] - - def test_load_table_from_file(client, to_delete): """Upload table data from a CSV file.""" dataset_id = "load_table_from_file_dataset_{}".format(_millis()) @@ -993,12 +920,10 @@ def test_load_table_from_uri_orc(client, to_delete, capsys): def test_load_table_from_uri_autodetect(client, to_delete, capsys): """Load table from a GCS URI using various formats and auto-detected schema - Each file format has its own tested load from URI sample. Because most of the code is common for autodetect, append, and truncate, this sample includes snippets for all supported formats but only calls a single load job. - This code snippet is made up of shared code, then format-specific code, followed by more shared code. Note that only the last format in the format-specific code section will be tested in this test. @@ -1058,12 +983,10 @@ def test_load_table_from_uri_autodetect(client, to_delete, capsys): def test_load_table_from_uri_truncate(client, to_delete, capsys): """Replaces table data with data from a GCS URI using various formats - Each file format has its own tested load from URI sample. Because most of the code is common for autodetect, append, and truncate, this sample includes snippets for all supported formats but only calls a single load job. - This code snippet is made up of shared code, then format-specific code, followed by more shared code. Note that only the last format in the format-specific code section will be tested in this test. @@ -1303,38 +1226,6 @@ def test_load_table_relax_column(client, to_delete): assert table.num_rows > 0 -def test_copy_table(client, to_delete): - dataset_id = "copy_table_dataset_{}".format(_millis()) - dest_dataset = bigquery.Dataset(client.dataset(dataset_id)) - dest_dataset.location = "US" - dest_dataset = client.create_dataset(dest_dataset) - to_delete.append(dest_dataset) - - # [START bigquery_copy_table] - # from google.cloud import bigquery - # client = bigquery.Client() - - source_dataset = client.dataset("samples", project="bigquery-public-data") - source_table_ref = source_dataset.table("shakespeare") - - # dataset_id = 'my_dataset' - dest_table_ref = client.dataset(dataset_id).table("destination_table") - - job = client.copy_table( - source_table_ref, - dest_table_ref, - # Location must match that of the source and destination tables. - location="US", - ) # API request - - job.result() # Waits for job to complete. - - assert job.state == "DONE" - dest_table = client.get_table(dest_table_ref) # API request - assert dest_table.num_rows > 0 - # [END bigquery_copy_table] - - def test_copy_table_multiple_source(client, to_delete): dest_dataset_id = "dest_dataset_{}".format(_millis()) dest_dataset = bigquery.Dataset(client.dataset(dest_dataset_id)) @@ -1601,31 +1492,6 @@ def test_undelete_table(client, to_delete): # [END bigquery_undelete_table] -def test_client_query(client): - """Run a simple query.""" - - # [START bigquery_query] - # from google.cloud import bigquery - # client = bigquery.Client() - - query = ( - "SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` " - 'WHERE state = "TX" ' - "LIMIT 100" - ) - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - ) # API request - starts the query - - for row in query_job: # API request - fetches results - # Row values can be accessed by field name or index - assert row[0] == row.name == row["name"] - print(row) - # [END bigquery_query] - - def test_client_query_legacy_sql(client): """Run a query with Legacy SQL explicitly set""" # [START bigquery_query_legacy] @@ -2462,42 +2328,6 @@ def test_ddl_create_view(client, to_delete, capsys): assert len(df) == 0 -def test_client_list_jobs(client): - """List jobs for a project.""" - - # [START bigquery_list_jobs] - # TODO(developer): Uncomment the lines below and replace with your values. - # from google.cloud import bigquery - # project = 'my_project' # replace with your project ID - # client = bigquery.Client(project=project) - import datetime - - # List the 10 most recent jobs in reverse chronological order. - # Omit the max_results parameter to list jobs from the past 6 months. - print("Last 10 jobs:") - for job in client.list_jobs(max_results=10): # API request(s) - print(job.job_id) - - # The following are examples of additional optional parameters: - - # Use min_creation_time and/or max_creation_time to specify a time window. - print("Jobs from the last ten minutes:") - ten_mins_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) - for job in client.list_jobs(min_creation_time=ten_mins_ago): - print(job.job_id) - - # Use all_users to include jobs run by all users in the project. - print("Last 10 jobs run by all users:") - for job in client.list_jobs(max_results=10, all_users=True): - print("{} run by user: {}".format(job.job_id, job.user_email)) - - # Use state_filter to filter by job state. - print("Jobs currently running:") - for job in client.list_jobs(state_filter="RUNNING"): - print(job.job_id) - # [END bigquery_list_jobs] - - @pytest.mark.skipif(pandas is None, reason="Requires `pandas`") def test_query_results_as_dataframe(client): # [START bigquery_query_results_dataframe] diff --git a/bigquery/docs/usage/jobs.rst b/bigquery/docs/usage/jobs.rst index 914d1d459ee7..c3dd71031bfc 100644 --- a/bigquery/docs/usage/jobs.rst +++ b/bigquery/docs/usage/jobs.rst @@ -1,9 +1,6 @@ Managing Jobs ~~~~~~~~~~~~~ -List jobs for a project -^^^^^^^^^^^^^^^^^^^^^^^ - Jobs describe actions performed on data in BigQuery tables: - Load data into a table @@ -11,7 +8,13 @@ Jobs describe actions performed on data in BigQuery tables: - Extract data from a table - Copy a table -.. literalinclude:: ../snippets.py +Listing jobs +^^^^^^^^^^^^ + +List jobs for a project with the +:func:`~google.cloud.bigquery.client.Client.list_jobs` method: + +.. literalinclude:: ../samples/client_list_jobs.py :language: python :dedent: 4 :start-after: [START bigquery_list_jobs] diff --git a/bigquery/docs/usage/queries.rst b/bigquery/docs/usage/queries.rst index fc77bb5b80cd..1f0720e47f1a 100644 --- a/bigquery/docs/usage/queries.rst +++ b/bigquery/docs/usage/queries.rst @@ -4,9 +4,10 @@ Running Queries Querying data ^^^^^^^^^^^^^ -Run a query and wait for it to finish: +Run a query and wait for it to finish with the +:func:`~google.cloud.bigquery.client.Client.query` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/client_query.py :language: python :dedent: 4 :start-after: [START bigquery_query] diff --git a/bigquery/docs/usage/tables.rst b/bigquery/docs/usage/tables.rst index 458c5b0009ba..6a6cbd356639 100644 --- a/bigquery/docs/usage/tables.rst +++ b/bigquery/docs/usage/tables.rst @@ -28,6 +28,15 @@ Get a table resource with the :start-after: [START bigquery_get_table] :end-before: [END bigquery_get_table] +Determine if a table exists with the +:func:`~google.cloud.bigquery.client.Client.get_table` method: + +.. literalinclude:: ../samples/table_exists.py + :language: python + :dedent: 4 + :start-after: [START bigquery_table_exists] + :end-before: [END bigquery_table_exists] + Browse data rows in a table with the :func:`~google.cloud.bigquery.client.Client.list_rows` method: @@ -107,7 +116,7 @@ Update a property in a table's metadata with the Insert rows into a table's data with the :func:`~google.cloud.bigquery.client.Client.insert_rows` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/table_insert_rows.py :language: python :dedent: 4 :start-after: [START bigquery_table_insert_rows] @@ -128,7 +137,7 @@ Copying a Table Copy a table with the :func:`~google.cloud.bigquery.client.Client.copy_table` method: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/copy_table.py :language: python :dedent: 4 :start-after: [START bigquery_copy_table] diff --git a/bigquery/samples/add_empty_column.py b/bigquery/samples/add_empty_column.py index 4f0b971e577a..bd531898eb29 100644 --- a/bigquery/samples/add_empty_column.py +++ b/bigquery/samples/add_empty_column.py @@ -28,7 +28,7 @@ def add_empty_column(client, table_id): table = client.get_table(table_id) # Make an API request. original_schema = table.schema - new_schema = original_schema[:] # creates a copy of the schema + new_schema = original_schema[:] # Creates a copy of the schema. new_schema.append(bigquery.SchemaField("phone", "STRING")) table.schema = new_schema diff --git a/bigquery/samples/browse_table_data.py b/bigquery/samples/browse_table_data.py index bba8dc434dd9..78d1d351a7a7 100644 --- a/bigquery/samples/browse_table_data.py +++ b/bigquery/samples/browse_table_data.py @@ -39,7 +39,7 @@ def browse_table_data(client, table_id): # Specify selected fields to limit the results to certain columns. table = client.get_table(table_id) # Make an API request. - fields = table.schema[:2] # first two columns + fields = table.schema[:2] # First two columns. rows_iter = client.list_rows(table_id, selected_fields=fields, max_results=10) rows = list(rows_iter) print("Selected {} columns from table {}.".format(len(rows_iter.schema), table_id)) diff --git a/bigquery/samples/client_list_jobs.py b/bigquery/samples/client_list_jobs.py new file mode 100644 index 000000000000..08eb4fbd99ef --- /dev/null +++ b/bigquery/samples/client_list_jobs.py @@ -0,0 +1,50 @@ +# 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_list_jobs(client): + + # [START bigquery_list_jobs] + # TODO(developer): Import the client library. + # from google.cloud import bigquery + + import datetime + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # List the 10 most recent jobs in reverse chronological order. + # Omit the max_results parameter to list jobs from the past 6 months. + print("Last 10 jobs:") + for job in client.list_jobs(max_results=10): # API request(s) + print("{}".format(job.job_id)) + + # The following are examples of additional optional parameters: + + # Use min_creation_time and/or max_creation_time to specify a time window. + print("Jobs from the last ten minutes:") + ten_mins_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) + for job in client.list_jobs(min_creation_time=ten_mins_ago): + print("{}".format(job.job_id)) + + # Use all_users to include jobs run by all users in the project. + print("Last 10 jobs run by all users:") + for job in client.list_jobs(max_results=10, all_users=True): + print("{} run by user: {}".format(job.job_id, job.user_email)) + + # Use state_filter to filter by job state. + print("Last 10 jobs done:") + for job in client.list_jobs(max_results=10, state_filter="DONE"): + print("{}".format(job.job_id)) + # [END bigquery_list_jobs] diff --git a/bigquery/samples/client_query.py b/bigquery/samples/client_query.py new file mode 100644 index 000000000000..9dccfd38cbcf --- /dev/null +++ b/bigquery/samples/client_query.py @@ -0,0 +1,41 @@ +# 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(client): + + # [START bigquery_query] + # TODO(developer): Import the client library. + # from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + query = """ + SELECT name, SUM(number) as total_people + FROM `bigquery-public-data.usa_names.usa_1910_2013` + WHERE state = 'TX' + GROUP BY name, state + ORDER BY total_people DESC + LIMIT 20 + """ + query_job = client.query( + query, location="US" # Must match the destination dataset(s) location. + ) # Make an API request. + + print("The query data:") + for row in query_job: + # Row values can be accessed by field name or index. + print("name={}, count={}".format(row[0], row["total_people"])) + # [END bigquery_query] diff --git a/bigquery/samples/copy_table.py b/bigquery/samples/copy_table.py new file mode 100644 index 000000000000..f6ebd91470eb --- /dev/null +++ b/bigquery/samples/copy_table.py @@ -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 copy_table(client, source_table_id, destination_table_id): + + # [START bigquery_copy_table] + # TODO(developer): Import the client library. + # from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # TODO(developer): Set source_table_id to the ID of the original table. + # source_table_id = "your-project.source_dataset.source_table" + + # TODO(developer): Set destination_table_id to the ID of the destination table. + # destination_table_id = "your-project.destination_dataset.destination_table" + + job = client.copy_table( + source_table_id, + destination_table_id, + location="US", # Must match the source and destination tables location. + ) + job.result() # Waits for job to complete. + + print("A copy of the table created.") + # [END bigquery_copy_table] diff --git a/bigquery/samples/create_routine_ddl.py b/bigquery/samples/create_routine_ddl.py index 836e0cdde34a..eb5af0388503 100644 --- a/bigquery/samples/create_routine_ddl.py +++ b/bigquery/samples/create_routine_ddl.py @@ -34,12 +34,8 @@ def create_routine_ddl(client, routine_id): """.format( routine_id ) - - # Initiate the query to create the routine. query_job = client.query(sql) # Make an API request. - - # Wait for the query to complete. - query_job.result() # Waits for the job to complete. + query_job.result() # Wait for the job to complete. print("Created routine {}".format(query_job.ddl_target_routine)) # [END bigquery_create_routine_ddl] diff --git a/bigquery/samples/create_table.py b/bigquery/samples/create_table.py index b77812f7e0ce..ae26c57fed00 100644 --- a/bigquery/samples/create_table.py +++ b/bigquery/samples/create_table.py @@ -21,7 +21,7 @@ def create_table(client, table_id): # TODO(developer): Construct a BigQuery client object. # client = bigquery.Client() - # TODO(developer): Set table_id to the ID of the table to create + # TODO(developer): Set table_id to the ID of the table to create. # table_id = "your-project.your_dataset.your_table_name" schema = [ diff --git a/bigquery/samples/delete_dataset.py b/bigquery/samples/delete_dataset.py index 29302f099998..8ce95d953392 100644 --- a/bigquery/samples/delete_dataset.py +++ b/bigquery/samples/delete_dataset.py @@ -25,7 +25,7 @@ def delete_dataset(client, dataset_id): # TODO(developer): Set model_id to the ID of the model to fetch. # dataset_id = 'your-project.your_dataset' - # Use the delete_contents parameter to delete a dataset and its contents + # Use the delete_contents parameter to delete a dataset and its contents. # Use the not_found_ok parameter to not receive an error if the dataset has already been deleted. client.delete_dataset( dataset_id, delete_contents=True, not_found_ok=True diff --git a/bigquery/samples/delete_dataset_labels.py b/bigquery/samples/delete_dataset_labels.py index 425bc98dd96e..9e6493694ddc 100644 --- a/bigquery/samples/delete_dataset_labels.py +++ b/bigquery/samples/delete_dataset_labels.py @@ -27,7 +27,7 @@ def delete_dataset_labels(client, dataset_id): dataset = client.get_dataset(dataset_id) # Make an API request. - # To delete a label from a dataset, set its value to None + # To delete a label from a dataset, set its value to None. dataset.labels["color"] = None dataset = client.update_dataset(dataset, ["labels"]) # Make an API request. diff --git a/bigquery/samples/delete_table.py b/bigquery/samples/delete_table.py index 4c4377418556..b83a92890b09 100644 --- a/bigquery/samples/delete_table.py +++ b/bigquery/samples/delete_table.py @@ -26,7 +26,7 @@ def delete_table(client, table_id): # table_id = 'your-project.your_dataset.your_table' # If the table does not exist, delete_table raises - # google.api_core.exceptions.NotFound unless not_found_ok is True + # google.api_core.exceptions.NotFound unless not_found_ok is True. client.delete_table(table_id, not_found_ok=True) # Make an API request. print("Deleted table '{}'.".format(table_id)) # [END bigquery_delete_table] diff --git a/bigquery/samples/get_dataset.py b/bigquery/samples/get_dataset.py index cd35745c0dc5..bb3d4a0d4c40 100644 --- a/bigquery/samples/get_dataset.py +++ b/bigquery/samples/get_dataset.py @@ -35,7 +35,7 @@ def get_dataset(client, dataset_id): ) ) - # View dataset properties + # View dataset properties. print("Description: {}".format(dataset.description)) print("Labels:") labels = dataset.labels @@ -45,9 +45,9 @@ def get_dataset(client, dataset_id): else: print("\tDataset has no labels defined.") - # View tables in dataset + # View tables in dataset. print("Tables:") - tables = list(client.list_tables(dataset)) # API request(s) + tables = list(client.list_tables(dataset)) # Make an API request(s). if tables: for table in tables: print("\t{}".format(table.table_id)) diff --git a/bigquery/samples/get_dataset_labels.py b/bigquery/samples/get_dataset_labels.py index 46e38a3a9a56..411607f84664 100644 --- a/bigquery/samples/get_dataset_labels.py +++ b/bigquery/samples/get_dataset_labels.py @@ -27,7 +27,7 @@ def get_dataset_labels(client, dataset_id): dataset = client.get_dataset(dataset_id) # Make an API request. - # View dataset labels + # View dataset labels. print("Dataset ID: {}".format(dataset_id)) print("Labels:") if dataset.labels: diff --git a/bigquery/samples/load_table_dataframe.py b/bigquery/samples/load_table_dataframe.py index f08712d4dc32..ea6fe5d02384 100644 --- a/bigquery/samples/load_table_dataframe.py +++ b/bigquery/samples/load_table_dataframe.py @@ -66,7 +66,7 @@ def load_table_dataframe(client, table_id): job_config=job_config, location="US", # Must match the destination dataset location. ) # Make an API request. - job.result() # Waits for the job to complete. + job.result() # Wait for the job to complete. table = client.get_table(table_id) # Make an API request. print( diff --git a/bigquery/samples/table_exists.py b/bigquery/samples/table_exists.py new file mode 100644 index 000000000000..a011e6e2915d --- /dev/null +++ b/bigquery/samples/table_exists.py @@ -0,0 +1,29 @@ +# 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 table_exists(client, table_id): + + # [START bigquery_table_exists] + from google.cloud.exceptions import NotFound + + # TODO(developer): Set table_id to the ID of the table to determine existence. + # table_id = "your-project.your_dataset.your_table" + + try: + client.get_table(table_id) # Make an API request. + print("Table {} already exists.".format(table_id)) + except NotFound: + print("Table {} is not found.".format(table_id)) + # [END bigquery_table_exists] diff --git a/bigquery/samples/table_insert_rows.py b/bigquery/samples/table_insert_rows.py new file mode 100644 index 000000000000..e2f949b635a6 --- /dev/null +++ b/bigquery/samples/table_insert_rows.py @@ -0,0 +1,34 @@ +# 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 table_insert_rows(client, table_id): + + # [START bigquery_table_insert_rows] + # TODO(developer): Import the client library. + # 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 model to fetch. + # table_id = "your-project.your_dataset.your_table" + + table = client.get_table(table_id) # Make an API request. + rows_to_insert = [(u"Phred Phlyntstone", 32), (u"Wylma Phlyntstone", 29)] + + errors = client.insert_rows(table, rows_to_insert) # Make an API request. + if errors == []: + print("New rows have been added.") + # [END bigquery_table_insert_rows] diff --git a/bigquery/samples/tests/conftest.py b/bigquery/samples/tests/conftest.py index f2bb93112a22..32b23931aa91 100644 --- a/bigquery/samples/tests/conftest.py +++ b/bigquery/samples/tests/conftest.py @@ -57,7 +57,7 @@ def random_routine_id(client, dataset_id): @pytest.fixture def dataset_id(client): now = datetime.datetime.now() - dataset_id = "python_samples_{}_{}".format( + dataset_id = "python_dataset_sample_{}_{}".format( now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] ) dataset = client.create_dataset(dataset_id) @@ -68,7 +68,7 @@ def dataset_id(client): @pytest.fixture def table_id(client, dataset_id): now = datetime.datetime.now() - table_id = "python_samples_{}_{}".format( + table_id = "python_table_sample_{}_{}".format( now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] ) @@ -86,7 +86,7 @@ def table_with_data_id(client): @pytest.fixture def routine_id(client, dataset_id): now = datetime.datetime.now() - routine_id = "python_samples_{}_{}".format( + routine_id = "python_routine_sample_{}_{}".format( now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8] ) diff --git a/bigquery/samples/tests/test_client_list_jobs.py b/bigquery/samples/tests/test_client_list_jobs.py new file mode 100644 index 000000000000..011e081fdee4 --- /dev/null +++ b/bigquery/samples/tests/test_client_list_jobs.py @@ -0,0 +1,31 @@ +# 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. + + +from .. import client_list_jobs +from .. import create_job + + +def test_client_list_jobs(capsys, client): + + job = create_job.create_job(client) + client.cancel_job(job.job_id) + job.cancel() + client_list_jobs.client_list_jobs(client) + out, err = capsys.readouterr() + assert "Started job: {}".format(job.job_id) in out + assert "Last 10 jobs:" in out + assert "Jobs from the last ten minutes:" in out + assert "Last 10 jobs run by all users:" in out + assert "Last 10 jobs done:" in out diff --git a/bigquery/samples/tests/test_client_query.py b/bigquery/samples/tests/test_client_query.py new file mode 100644 index 000000000000..fd5b8e7edd97 --- /dev/null +++ b/bigquery/samples/tests/test_client_query.py @@ -0,0 +1,24 @@ +# 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. + + +from .. import client_query + + +def test_client_query(capsys, client): + + client_query.client_query(client) + out, err = capsys.readouterr() + assert "The query data:" in out + assert "name=James, count=272793" in out diff --git a/bigquery/samples/tests/test_copy_table.py b/bigquery/samples/tests/test_copy_table.py new file mode 100644 index 000000000000..6d7de2d9132c --- /dev/null +++ b/bigquery/samples/tests/test_copy_table.py @@ -0,0 +1,27 @@ +# 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. + + +from .. import copy_table + + +def test_copy_table(capsys, client, table_with_data_id, random_table_id): + + copy_table.copy_table(client, table_with_data_id, random_table_id) + out, err = capsys.readouterr() + assert "A copy of the table created." in out + assert ( + client.get_table(random_table_id).num_rows + == client.get_table(table_with_data_id).num_rows + ) diff --git a/bigquery/samples/tests/test_create_dataset.py b/bigquery/samples/tests/test_create_dataset.py index dfadc67d8468..e52e9ddfdced 100644 --- a/bigquery/samples/tests/test_create_dataset.py +++ b/bigquery/samples/tests/test_create_dataset.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import create_dataset diff --git a/bigquery/samples/tests/test_create_job.py b/bigquery/samples/tests/test_create_job.py index fce005ae8236..5ead51156606 100644 --- a/bigquery/samples/tests/test_create_job.py +++ b/bigquery/samples/tests/test_create_job.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import create_job diff --git a/bigquery/samples/tests/test_create_routine_ddl.py b/bigquery/samples/tests/test_create_routine_ddl.py index cecda2f654ec..bcb3249d26ef 100644 --- a/bigquery/samples/tests/test_create_routine_ddl.py +++ b/bigquery/samples/tests/test_create_routine_ddl.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from google.cloud import bigquery from google.cloud import bigquery_v2 diff --git a/bigquery/samples/tests/test_create_table.py b/bigquery/samples/tests/test_create_table.py index 093ee6e94277..f9ebc0e5d70d 100644 --- a/bigquery/samples/tests/test_create_table.py +++ b/bigquery/samples/tests/test_create_table.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import create_table diff --git a/bigquery/samples/tests/test_dataset_label_samples.py b/bigquery/samples/tests/test_dataset_label_samples.py index 94a2092407b0..1e526f2339ac 100644 --- a/bigquery/samples/tests/test_dataset_label_samples.py +++ b/bigquery/samples/tests/test_dataset_label_samples.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import delete_dataset_labels from .. import get_dataset_labels from .. import label_dataset diff --git a/bigquery/samples/tests/test_delete_dataset.py b/bigquery/samples/tests/test_delete_dataset.py index 2b1b6ad06195..836b3aebb272 100644 --- a/bigquery/samples/tests/test_delete_dataset.py +++ b/bigquery/samples/tests/test_delete_dataset.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import delete_dataset diff --git a/bigquery/samples/tests/test_delete_table.py b/bigquery/samples/tests/test_delete_table.py index 8f4796623a83..f76ad8624cc6 100644 --- a/bigquery/samples/tests/test_delete_table.py +++ b/bigquery/samples/tests/test_delete_table.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import delete_table diff --git a/bigquery/samples/tests/test_get_dataset.py b/bigquery/samples/tests/test_get_dataset.py index dedec1d7b29e..8682be7ee3e9 100644 --- a/bigquery/samples/tests/test_get_dataset.py +++ b/bigquery/samples/tests/test_get_dataset.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import get_dataset diff --git a/bigquery/samples/tests/test_get_table.py b/bigquery/samples/tests/test_get_table.py index efbd464d54ad..8adaa6557954 100644 --- a/bigquery/samples/tests/test_get_table.py +++ b/bigquery/samples/tests/test_get_table.py @@ -12,7 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. + from google.cloud import bigquery + from .. import get_table diff --git a/bigquery/samples/tests/test_list_datasets.py b/bigquery/samples/tests/test_list_datasets.py index 4c66a24f9b1a..d8c32e91ee20 100644 --- a/bigquery/samples/tests/test_list_datasets.py +++ b/bigquery/samples/tests/test_list_datasets.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import list_datasets diff --git a/bigquery/samples/tests/test_list_tables.py b/bigquery/samples/tests/test_list_tables.py index ec1621ac7579..61ac04ea26ce 100644 --- a/bigquery/samples/tests/test_list_tables.py +++ b/bigquery/samples/tests/test_list_tables.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import list_tables diff --git a/bigquery/samples/tests/test_model_samples.py b/bigquery/samples/tests/test_model_samples.py index d7b06a92a3e1..99d838533917 100644 --- a/bigquery/samples/tests/test_model_samples.py +++ b/bigquery/samples/tests/test_model_samples.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import delete_model from .. import get_model from .. import list_models diff --git a/bigquery/samples/tests/test_query_to_arrow.py b/bigquery/samples/tests/test_query_to_arrow.py index dd9b3ab508cc..2fbed807ece4 100644 --- a/bigquery/samples/tests/test_query_to_arrow.py +++ b/bigquery/samples/tests/test_query_to_arrow.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import pyarrow from .. import query_to_arrow diff --git a/bigquery/samples/tests/test_table_exists.py b/bigquery/samples/tests/test_table_exists.py new file mode 100644 index 000000000000..232d77fbcb60 --- /dev/null +++ b/bigquery/samples/tests/test_table_exists.py @@ -0,0 +1,30 @@ +# 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. + + +from google.cloud import bigquery + +from .. import table_exists + + +def test_table_exists(capsys, client, random_table_id): + + table_exists.table_exists(client, random_table_id) + out, err = capsys.readouterr() + assert "Table {} is not found.".format(random_table_id) in out + table = bigquery.Table(random_table_id) + table = client.create_table(table) + table_exists.table_exists(client, random_table_id) + out, err = capsys.readouterr() + assert "Table {} already exists.".format(random_table_id) in out diff --git a/bigquery/samples/tests/test_table_insert_rows.py b/bigquery/samples/tests/test_table_insert_rows.py new file mode 100644 index 000000000000..95d119dbdc93 --- /dev/null +++ b/bigquery/samples/tests/test_table_insert_rows.py @@ -0,0 +1,33 @@ +# 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. + + +from google.cloud import bigquery + +from .. import table_insert_rows + + +def test_table_insert_rows(capsys, client, random_table_id): + + schema = [ + bigquery.SchemaField("full_name", "STRING", mode="REQUIRED"), + bigquery.SchemaField("age", "INTEGER", mode="REQUIRED"), + ] + + table = bigquery.Table(random_table_id, schema=schema) + table = client.create_table(table) + + table_insert_rows.table_insert_rows(client, random_table_id) + out, err = capsys.readouterr() + assert "New rows have been added." in out diff --git a/bigquery/samples/tests/test_update_dataset_access.py b/bigquery/samples/tests/test_update_dataset_access.py index ae33dbfe4a4c..679b700731e3 100644 --- a/bigquery/samples/tests/test_update_dataset_access.py +++ b/bigquery/samples/tests/test_update_dataset_access.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import update_dataset_access diff --git a/bigquery/samples/tests/test_update_dataset_default_table_expiration.py b/bigquery/samples/tests/test_update_dataset_default_table_expiration.py index 46e9654209ed..a97de11a2f1a 100644 --- a/bigquery/samples/tests/test_update_dataset_default_table_expiration.py +++ b/bigquery/samples/tests/test_update_dataset_default_table_expiration.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import update_dataset_default_table_expiration diff --git a/bigquery/samples/tests/test_update_dataset_description.py b/bigquery/samples/tests/test_update_dataset_description.py index c6f8889f50da..63826077b976 100644 --- a/bigquery/samples/tests/test_update_dataset_description.py +++ b/bigquery/samples/tests/test_update_dataset_description.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from .. import update_dataset_description diff --git a/bigquery/samples/update_dataset_default_table_expiration.py b/bigquery/samples/update_dataset_default_table_expiration.py index 7b68ede8d2be..8de354b1f21b 100644 --- a/bigquery/samples/update_dataset_default_table_expiration.py +++ b/bigquery/samples/update_dataset_default_table_expiration.py @@ -26,7 +26,7 @@ def update_dataset_default_table_expiration(client, dataset_id): # dataset_id = 'your-project.your_dataset' dataset = client.get_dataset(dataset_id) # Make an API request. - dataset.default_table_expiration_ms = 24 * 60 * 60 * 1000 # in milliseconds. + dataset.default_table_expiration_ms = 24 * 60 * 60 * 1000 # In milliseconds. dataset = client.update_dataset( dataset, ["default_table_expiration_ms"]