Skip to content

Commit ae647eb

Browse files
authored
docs: recommend insert_rows_json to avoid call to tables.get (#258)
* 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. * update comments
1 parent d24b5bb commit ae647eb

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

samples/table_insert_rows.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616
def table_insert_rows(table_id):
1717

1818
# [START bigquery_table_insert_rows]
19-
2019
from google.cloud import bigquery
2120

2221
# Construct a BigQuery client object.
2322
client = bigquery.Client()
2423

25-
# TODO(developer): Set table_id to the ID of the model to fetch.
24+
# TODO(developer): Set table_id to the ID of table to append to.
2625
# table_id = "your-project.your_dataset.your_table"
2726

28-
table = client.get_table(table_id) # Make an API request.
29-
rows_to_insert = [(u"Phred Phlyntstone", 32), (u"Wylma Phlyntstone", 29)]
27+
rows_to_insert = [
28+
{u"full_name": u"Phred Phlyntstone", u"age": 32},
29+
{u"full_name": u"Wylma Phlyntstone", u"age": 29},
30+
]
3031

31-
errors = client.insert_rows(table, rows_to_insert) # Make an API request.
32+
errors = client.insert_rows_json(table_id, rows_to_insert) # Make an API request.
3233
if errors == []:
3334
print("New rows have been added.")
35+
else:
36+
print("Encountered errors while inserting rows: {}".format(errors))
3437
# [END bigquery_table_insert_rows]

samples/table_insert_rows_explicit_none_insert_ids.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@
1616
def table_insert_rows_explicit_none_insert_ids(table_id):
1717

1818
# [START bigquery_table_insert_rows_explicit_none_insert_ids]
19-
2019
from google.cloud import bigquery
2120

2221
# Construct a BigQuery client object.
2322
client = bigquery.Client()
2423

25-
# TODO(developer): Set table_id to the ID of the model to fetch.
24+
# TODO(developer): Set table_id to the ID of table to append to.
2625
# table_id = "your-project.your_dataset.your_table"
2726

28-
table = client.get_table(table_id) # Make an API request.
29-
rows_to_insert = [(u"Phred Phlyntstone", 32), (u"Wylma Phlyntstone", 29)]
27+
rows_to_insert = [
28+
{u"full_name": u"Phred Phlyntstone", u"age": 32},
29+
{u"full_name": u"Wylma Phlyntstone", u"age": 29},
30+
]
3031

31-
errors = client.insert_rows(
32-
table, rows_to_insert, row_ids=[None] * len(rows_to_insert)
32+
errors = client.insert_rows_json(
33+
table_id, rows_to_insert, row_ids=[None] * len(rows_to_insert)
3334
) # Make an API request.
3435
if errors == []:
3536
print("New rows have been added.")
37+
else:
38+
print("Encountered errors while inserting rows: {}".format(errors))
3639
# [END bigquery_table_insert_rows_explicit_none_insert_ids]

0 commit comments

Comments
 (0)