Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions samples/table_insert_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,26 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positional arguments cannot follow keyword arguments, please change the call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, still need to check for errors. The API call returns success on parial failure, such as unable to insert a particular row.

print("New rows have been added.")
# [END bigquery_table_insert_rows]