无法将数据插入 BigQuery Table
Unable to insert data into BigQuery Table
我正在尝试使用我的 Python 代码将记录插入 BigQuery。我总是收到 Table not found 错误,即使 table 存在。
from google.cloud import bigquery
from google.oauth2 import service_account
key_path = service-account.json"
credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
bigquery_client = bigquery.Client(
credentials=credentials,
project=credentials.project_id,
)
dataset_ref = bigquery_client.dataset('mydataset')
table_ref = dataset_ref.table('mytable3')
rows_to_insert = [(u'Adam', 32),(u'Eve', 29)]
errors = bigquery_client.insert_rows(table, rows_to_insert)
assert errors == []
错误:
Traceback (most recent call last):
File "./insert.py", line 24, in <module>
bigquery_client.get_table(table_ref)
File "/Users/adam/env/lib/python3.7/site-packages/google/cloud/bigquery/client.py", line 581, in get_table
api_response = self._call_api(retry, method="GET", path=table_ref.path)
File "/Users/adam/env/lib/python3.7/site-packages/google/cloud/bigquery/client.py", line 476, in _call_api
return call()
File "/Users/adam/env/lib/python3.7/site-packages/google/api_core/retry.py", line 277, in retry_wrapped_func
on_error=on_error,
File "/Users/adam/env/lib/python3.7/site-packages/google/api_core/retry.py", line 182, in retry_target
return target()
File "/Users/adam/env/lib/python3.7/site-packages/google/cloud/_http.py", line 393, in api_request
raise exceptions.from_http_response(response)
google.api_core.exceptions.NotFound: 404 GET
https://bigquery.googleapis.com/bigquery/v2/projects/myproject/datasets/mydataset/tables/mytable3: Not found: Table myproject:mydataset.mytable3
我在 2019 年 11 月 7 日星期四 13:23:10 CET 插入值,table 创建于 2019 年 11 月 7 日,11:02:48(2 小时后)。有什么原因让我找不到 table,即使 table 在 GUI 和 CLI 中都可见。
从您分享的代码中,我发现缺少 BigQuery API call table = bigquery_client.get_table(table_ref)
。您可以使用以下脚本在数据集 DATASET
中创建的名为 TABLE
的现有 table 中插入值
from google.cloud import bigquery
from google.oauth2 import service_account
key_path = "./service-account.json"
credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
def insert_to_bigquery(rows_to_insert, dataset_name="DATASET", table_name="TABLE"):
# Instantiates a client
bigquery_client = bigquery.Client()
# Prepares a reference to the dataset and table
dataset_ref = bigquery_client.dataset(dataset_name)
table_ref = dataset_ref.table(table_name)
# API call
table = bigquery_client.get_table(table_ref)
# API request to insert the rows_to_insert
errors = bigquery_client.insert_rows(table, rows_to_insert)
assert errors == []
rows_to_insert = [( u'Jason', 32),\
( u'Paula', 29),\
(u'Hellen', 55)]
insert_to_bigquery(rows_to_insert)
可以通过 运行 以下 bq
命令测试值的插入是否成功:
bq query --nouse_legacy_sql 'SELECT * FROM `PROJECT.DATASET.TABLE`'
我正在尝试使用我的 Python 代码将记录插入 BigQuery。我总是收到 Table not found 错误,即使 table 存在。
from google.cloud import bigquery
from google.oauth2 import service_account
key_path = service-account.json"
credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
bigquery_client = bigquery.Client(
credentials=credentials,
project=credentials.project_id,
)
dataset_ref = bigquery_client.dataset('mydataset')
table_ref = dataset_ref.table('mytable3')
rows_to_insert = [(u'Adam', 32),(u'Eve', 29)]
errors = bigquery_client.insert_rows(table, rows_to_insert)
assert errors == []
错误:
Traceback (most recent call last):
File "./insert.py", line 24, in <module>
bigquery_client.get_table(table_ref)
File "/Users/adam/env/lib/python3.7/site-packages/google/cloud/bigquery/client.py", line 581, in get_table
api_response = self._call_api(retry, method="GET", path=table_ref.path)
File "/Users/adam/env/lib/python3.7/site-packages/google/cloud/bigquery/client.py", line 476, in _call_api
return call()
File "/Users/adam/env/lib/python3.7/site-packages/google/api_core/retry.py", line 277, in retry_wrapped_func
on_error=on_error,
File "/Users/adam/env/lib/python3.7/site-packages/google/api_core/retry.py", line 182, in retry_target
return target()
File "/Users/adam/env/lib/python3.7/site-packages/google/cloud/_http.py", line 393, in api_request
raise exceptions.from_http_response(response)
google.api_core.exceptions.NotFound: 404 GET
https://bigquery.googleapis.com/bigquery/v2/projects/myproject/datasets/mydataset/tables/mytable3: Not found: Table myproject:mydataset.mytable3
我在 2019 年 11 月 7 日星期四 13:23:10 CET 插入值,table 创建于 2019 年 11 月 7 日,11:02:48(2 小时后)。有什么原因让我找不到 table,即使 table 在 GUI 和 CLI 中都可见。
从您分享的代码中,我发现缺少 BigQuery API call table = bigquery_client.get_table(table_ref)
。您可以使用以下脚本在数据集 DATASET
TABLE
的现有 table 中插入值
from google.cloud import bigquery
from google.oauth2 import service_account
key_path = "./service-account.json"
credentials = service_account.Credentials.from_service_account_file(
key_path,
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
def insert_to_bigquery(rows_to_insert, dataset_name="DATASET", table_name="TABLE"):
# Instantiates a client
bigquery_client = bigquery.Client()
# Prepares a reference to the dataset and table
dataset_ref = bigquery_client.dataset(dataset_name)
table_ref = dataset_ref.table(table_name)
# API call
table = bigquery_client.get_table(table_ref)
# API request to insert the rows_to_insert
errors = bigquery_client.insert_rows(table, rows_to_insert)
assert errors == []
rows_to_insert = [( u'Jason', 32),\
( u'Paula', 29),\
(u'Hellen', 55)]
insert_to_bigquery(rows_to_insert)
可以通过 运行 以下 bq
命令测试值的插入是否成功:
bq query --nouse_legacy_sql 'SELECT * FROM `PROJECT.DATASET.TABLE`'