无法将数据插入BigQuery表中

问题描述 投票:0回答:1

我正在尝试使用我的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

我将在CET 2019年11月7日星期四13:23:10插入值,并在2019年11月7日11:02:48(2小时后)创建了表格。我有没有任何理由找不到表,即使该表在GUI和CLI中都可见。

google-bigquery google-oauth google-api-python-client google-apis-explorer
1个回答
0
投票

从您共享的代码中,我发现BigQuery API call table = bigquery_client.get_table(table_ref)丢失了。您可以使用以下脚本将值插入在数据集TABLE

中创建的名为DATASET的现有表中
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
© www.soinside.com 2019 - 2024. All rights reserved.