【问题标题】:Unable to insert data into BigQuery Table无法将数据插入 BigQuery 表
【发布时间】:2020-03-04 01:00:50
【问题描述】:

我正在尝试使用我的 Python 代码将记录插入 BigQuery。即使表存在,我总是得到表未找到错误。

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 插入值,表格是在 2019 年 11 月 7 日 11:02:48(2 小时后)创建的。是否有任何原因我找不到表,即使该表在 GUI 和 CLI 中都可见。

【问题讨论】:

标签: google-bigquery google-oauth google-api-python-client google-apis-explorer


【解决方案1】:

从您共享的代码中,我看到 BigQuery API call table = bigquery_client.get_table(table_ref) 丢失了。您可以使用以下脚本在数据集DATASET 中创建的名为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`'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2019-03-02
    • 2020-08-29
    • 2020-05-14
    • 2019-01-07
    • 1970-01-01
    • 2014-06-17
    相关资源
    最近更新 更多