From 012934c7567e97ec0f097b86e94f817dda957fe9 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Fri, 11 Sep 2020 09:41:02 -0500 Subject: [PATCH 1/2] docs: recommend insert_rows_json to avoid call to tables.get Since tables.get has a much lower QPS than tabledata.insertAll, we want to avoid recommending a pattern that requires fetching a table schema. If developers convert to a dictionary of the correct JSON format, no table schema is required. --- samples/table_insert_rows.py | 11 +++++++---- .../table_insert_rows_explicit_none_insert_ids.py | 13 ++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/samples/table_insert_rows.py b/samples/table_insert_rows.py index 130f9dbbd..6a38513de 100644 --- a/samples/table_insert_rows.py +++ b/samples/table_insert_rows.py @@ -16,7 +16,6 @@ def table_insert_rows(table_id): # [START bigquery_table_insert_rows] - from google.cloud import bigquery # Construct a BigQuery client object. @@ -25,10 +24,14 @@ def table_insert_rows(table_id): # 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)] + rows_to_insert = [ + {u"full_name": u"Phred Phlyntstone", u"age": 32}, + {u"full_name": u"Wylma Phlyntstone", u"age": 29}, + ] - errors = client.insert_rows(table, rows_to_insert) # Make an API request. + errors = client.insert_rows_json(table_id, rows_to_insert) # Make an API request. if errors == []: print("New rows have been added.") + else: + print("Encountered errors while inserting rows: {}".format(errors)) # [END bigquery_table_insert_rows] diff --git a/samples/table_insert_rows_explicit_none_insert_ids.py b/samples/table_insert_rows_explicit_none_insert_ids.py index 2410ba176..147573d4a 100644 --- a/samples/table_insert_rows_explicit_none_insert_ids.py +++ b/samples/table_insert_rows_explicit_none_insert_ids.py @@ -16,7 +16,6 @@ def table_insert_rows_explicit_none_insert_ids(table_id): # [START bigquery_table_insert_rows_explicit_none_insert_ids] - from google.cloud import bigquery # Construct a BigQuery client object. @@ -25,12 +24,16 @@ def table_insert_rows_explicit_none_insert_ids(table_id): # 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)] + rows_to_insert = [ + {u"full_name": u"Phred Phlyntstone", u"age": 32}, + {u"full_name": u"Wylma Phlyntstone", u"age": 29}, + ] - errors = client.insert_rows( - table, rows_to_insert, row_ids=[None] * len(rows_to_insert) + errors = client.insert_rows_json( + table_id, rows_to_insert, row_ids=[None] * len(rows_to_insert) ) # Make an API request. if errors == []: print("New rows have been added.") + else: + print("Encountered errors while inserting rows: {}".format(errors)) # [END bigquery_table_insert_rows_explicit_none_insert_ids] From f356a870c37e42b032f7a2d5c3e6ebc4b605c427 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 14 Sep 2020 16:50:42 -0500 Subject: [PATCH 2/2] update comments --- samples/table_insert_rows.py | 2 +- samples/table_insert_rows_explicit_none_insert_ids.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/table_insert_rows.py b/samples/table_insert_rows.py index 6a38513de..24d739871 100644 --- a/samples/table_insert_rows.py +++ b/samples/table_insert_rows.py @@ -21,7 +21,7 @@ def table_insert_rows(table_id): # Construct a BigQuery client object. client = bigquery.Client() - # TODO(developer): Set table_id to the ID of the model to fetch. + # TODO(developer): Set table_id to the ID of table to append to. # table_id = "your-project.your_dataset.your_table" rows_to_insert = [ diff --git a/samples/table_insert_rows_explicit_none_insert_ids.py b/samples/table_insert_rows_explicit_none_insert_ids.py index 147573d4a..d91792b82 100644 --- a/samples/table_insert_rows_explicit_none_insert_ids.py +++ b/samples/table_insert_rows_explicit_none_insert_ids.py @@ -21,7 +21,7 @@ def table_insert_rows_explicit_none_insert_ids(table_id): # Construct a BigQuery client object. client = bigquery.Client() - # TODO(developer): Set table_id to the ID of the model to fetch. + # TODO(developer): Set table_id to the ID of table to append to. # table_id = "your-project.your_dataset.your_table" rows_to_insert = [