From 1d8cfa816b28ab4f8183b24fcdfcec7e0c1fa03b Mon Sep 17 00:00:00 2001 From: jmgreger <43762185+jmgreger@users.noreply.github.com> Date: Mon, 31 Aug 2020 12:51:21 -0400 Subject: [PATCH 1/2] Update table_insert_rows.py Adding information to this example for more efficient streaming. Reduce calls to client.get_table() --- samples/table_insert_rows.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/samples/table_insert_rows.py b/samples/table_insert_rows.py index 130f9dbbd..e8234a0f6 100644 --- a/samples/table_insert_rows.py +++ b/samples/table_insert_rows.py @@ -23,12 +23,28 @@ def table_insert_rows(table_id): 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. + table_id = "your-project.your_dataset.your_table" + + # The client converts Python objects to JSON-serialization, but this requires a schema. + # A schema can be fetched by calling `client.get_table` as shown below. + # schema = client.get_table(table_id).schema + + # If inserting many rows, it is suggested that you cache the schema. + # The backend API for `client.insert_rows` supports a much higher QPS + # than the backend API for `client.get_table`. + + schema = [ + bigquery.SchemaField("col_1", "STRING"), + bigquery.SchemaField("col_2", "INTEGER"), + ] + + # Populate data for entry 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 == []: + try: + # Stream data to BQ + client.insert_rows(table_id, selected_fields=schema, rows_to_insert) print("New rows have been added.") # [END bigquery_table_insert_rows] + + From af8fc60180735aa196514577276c696d34dec0b4 Mon Sep 17 00:00:00 2001 From: jmgreger <43762185+jmgreger@users.noreply.github.com> Date: Mon, 31 Aug 2020 12:56:16 -0400 Subject: [PATCH 2/2] Delete empty lines --- samples/table_insert_rows.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/table_insert_rows.py b/samples/table_insert_rows.py index e8234a0f6..ce22328e4 100644 --- a/samples/table_insert_rows.py +++ b/samples/table_insert_rows.py @@ -46,5 +46,3 @@ def table_insert_rows(table_id): client.insert_rows(table_id, selected_fields=schema, rows_to_insert) print("New rows have been added.") # [END bigquery_table_insert_rows] - -