如何使用python将本地CSV上传到Google大查询

编程入门 行业动态 更新时间:2024-10-28 16:28:43
本文介绍了如何使用python将本地CSV上传到Google大查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用python将本地CSV上传到Google大查询

I'm trying to upload a local CSV to google big query using python

def uploadCsvToGbq(self,table_name): load_config = { 'destinationTable': { 'projectId': self.project_id, 'datasetId': self.dataset_id, 'tableId': table_name } } load_config['schema'] = { 'fields': [ {'name':'full_name', 'type':'STRING'}, {'name':'age', 'type':'INTEGER'}, ] } load_config['sourceFormat'] = 'CSV' upload = MediaFileUpload('sample.csv', mimetype='application/octet-stream', # This enables resumable uploads. resumable=True) start = time.time() job_id = 'job_%d' % start # Create the job. result = bigquery.jobs.insert( projectId=self.project_id, body={ 'jobReference': { 'jobId': job_id }, 'configuration': { 'load': load_config } }, media_body=upload).execute() return result

当我运行此命令时,它会引发类似

when I run this it throws error like

"NameError:未定义全局名称'MediaFileUpload'"

"NameError: global name 'MediaFileUpload' is not defined"

是否需要任何模块,请帮助.

whether any module is needed please help.

推荐答案

pip install --upgrade google-api-python-client

然后在您的python文件顶部写入:

Then on top of your python file write:

from googleapiclient.http import MediaFileUpload

但是请注意,您会错过一些括号.更好地写:

But care you miss some parenthesis. Better write:

result = bigquery.jobs().insert(projectId=PROJECT_ID, body={'jobReference': {'jobId': job_id},'configuration': {'load': load_config}}, media_body=upload).execute(num_retries=5)

顺便说一句,您将上载所有CSV行,包括定义列的第一行.

And by the way, you are going to upload all your CSV rows, including the top one that defines columns.

更多推荐

如何使用python将本地CSV上传到Google大查询

本文发布于:2023-10-26 10:18:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   python   Google   CSV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!