【问题标题】:GCP Cloud Function to write data to BigQuery runs with success but data doesn't appear in BigQuery table将数据写入 BigQuery 的 GCP 云函数运行成功,但数据未显示在 BigQuery 表中
【发布时间】:2020-03-25 07:41:47
【问题描述】:

我正在运行以下云功能。它运行成功并指示数据已加载到表中。但是当我查询 BigQuery 时,没有添加任何数据。我没有收到任何错误,也没有任何迹象表明它不起作用。

from google.cloud import bigquery
import pandas as pd


def download_data(event, context):

     df = pd.read_csv('https://covid.ourworldindata.org/data/ecdc/full_data.csv')

     # Create an empty list 
     Row_list =[] 

     # Iterate over each row 
     for index, rows in df.iterrows(): 
          # Create list for the current row 
          my_list =[rows.date, rows.location, rows.new_cases, rows.new_deaths, rows.total_cases, rows.total_deaths] 
          #print(my_list)     
     # append the list to the final list 
     Row_list.append(my_list) 


     ## Get Biq Query Set up
     client = bigquery.Client()
     table_id = "<project_name>.raw.daily_load"
     table = client.get_table(table_id)

     print(client)
     print(table_id)
     print(table)


     errors = client.insert_rows(table, Row_list)  # Make an API request.
     if errors == []:
          print("New rows have been added.")

目前尝试过;

  1. 正在提取检查数据 -> PASSED,我打印出 row_list 和 数据在那里
  2. 从我的机器本地运行 -> PASSED,当我从 python 终端运行时出现数据
  3. 打印出表格详细信息 -> PASSED,请参阅随附的屏幕截图,它都显示在日志中
  4. 确认可以找到表 -> PASSED,我更改了名称 将表转换为不存在且失败的表

不知道下一步是什么,任何建议将不胜感激

【问题讨论】:

  • 运行这个pyhton脚本的机器服务账号和查询数据的机器服务账号一样吗?
  • 我从 GUI 查询,所以我自己的凭据和云功能是应用引擎默认服务帐户
  • 如何查询 BigQuery?您是否在桌子上执行“预览”?还是执行真正的 SQL 查询?

标签: google-cloud-platform google-bigquery google-cloud-functions


【解决方案1】:

假设 App Engine 默认服务帐号已分配默认编辑者角色,并且您有一个非常简单的 BigQuery 表架构。例如:

Field name      Type          Mode       Policy tags    Description
date            STRING        NULLABLE  
location        STRING        NULLABLE  
new_cases       INTEGER       NULLABLE  
new_deaths      INTEGER       NULLABLE  
total_cases     INTEGER       NULLABLE  
total_deaths    INTEGER       NULLABLE

您的代码的以下修改应该适用于 HTTP 触发的函数。请注意,您没有在 for 循环中包含 Row_list.append(my_list) 以使用元素填充列表,并且根据 samples on the documentation 您应该使用元组列表:

from google.cloud import bigquery
import pandas as pd

client = bigquery.Client()
table_id = "[PROJECT-ID].[DATASET].[TABLE]"

def download_data(request):

     df = pd.read_csv('https://covid.ourworldindata.org/data/ecdc/full_data.csv')

     # Create an empty list 
     Row_list =[] 

     # Iterate over each row 
     for index, rows in df.iterrows(): 
          # Create list for the current row 
          my_list =(rows.date, rows.location, rows.new_cases, rows.new_deaths, rows.total_cases, rows.total_deaths)  
          # append the list to the final list 
          Row_list.append(my_list) 


     ## Get Biq Query Set up
     table = client.get_table(table_id)

     errors = client.insert_rows(table, Row_list)  # Make an API request.
     if errors == []:
          print("New rows have been added.")

使用非常简单的 requirements.txt 文件:

# Function dependencies, for example:
# package>=version
pandas
google-cloud-bigquery

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 2022-01-27
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2020-11-30
    • 2015-04-18
    • 2020-06-29
    相关资源
    最近更新 更多