我正在尝试转换一个txt。卷进csv。并通过Google函数使用所有数据填充BigQuery表。
TXT文件看起来非常类似于CSV文件,如下所示。整个文件权重约为35 is,超过350 k行。
我试着应用上面共享的python脚本,但是它没有工作.我的函数运行正常,但它没有将任何数据填充到BigQuery。
我跟踪了这个stackoverflow胎面。
我的main.py函数:
import pandas as pd
from google.cloud import bigquery
def txt_to_csv(event, context):
fileName = "gs://Bucket_name/file.txt"
bigqueryClient = bigquery.Client("project-name")
tableRef = bigqueryClient.dataset("Dataset").table("07_02_2021")
dataFrame = pd.read_csv(fileName, sep=",")
bigqueryJob = bigqueryClient.load_table_from_dataframe(dataFrame, tableRef)
bigqueryJob.result()
我的requirements.txt文件:
google-api-core==1.16.0
google-api-python-client==1.8.0
google-auth==1.12.0
google-auth-httplib2==0.0.3
google-cloud-bigquery==1.24.0
google-cloud-core==1.3.0
google-cloud-storage==1.26.0
google-resumable-media==0.5.0
googleapis-common-protos==1.51.0
pandas==1.0.3
pyarrow
有谁能帮我吗?,我觉得我错过了一步让它正常工作.比如我的桌子上的创建特定的数据schema/架构?或者我应该把熊猫的数据放在桌子上,还是走错路了?
发布于 2021-03-25 08:04:26
我成功地使用您的代码从云存储桶中的文件中填充了数据:
import pandas as pd
from google.cloud import bigquery
def txt_to_csv(event, context):
fileName = "gs://Bucket_NAME/File.txt"
bigqueryClient = bigquery.Client("PROJECT_ID")
tableRef = bigqueryClient.dataset("DATASET_NAME").table("TABLE_NAME")
dataFrame = pd.read_csv(fileName, sep=",")
bigqueryJob = bigqueryClient.load_table_from_dataframe(dataFrame, tableRef)
bigqueryJob.result()
我遵循了接下来的步骤:
1)我对以下数据使用了一个.txt文件:
full_name,birth_year
"Lea",1996
"Jose",1995
"John",1997
"Berta",2001
"Marta",2005
2)我在BigQuery中创建了一个包含以下字段的表:
3)我使用您提供的requirements.txt部署云函数,但是当我测试云函数 "By going to your Cloud Function --> Testing Tab and click on **Test the Function **button
时,它成功地部署了",我得到了以下错误:
Missing optional dependency 'gcsfs'. The gcsfs library is required
to handle GCS files Use pip or conda to install gcsfs.
google-api-core==1.16.0
google-api-python-client==1.8.0
google-auth==1.12.0
google-auth-httplib2==0.0.3
google-cloud-bigquery==1.24.0
google-cloud-core==1.3.0
google-cloud-storage==1.26.0
google-resumable-media==0.5.0
googleapis-common-protos==1.51.0
pandas==1.0.3
pyarrow
gcsfs==0.7.2
4)我再次部署了这个函数并进行了测试。这一次函数正确地将数据添加到BigQuery表.中。
溶液
https://stackoverflow.com/questions/66326218
复制