繁体   English   中英

通过Python脚本编写用于执行BigQuery的参数的脚本编写时出错

[英]Error when scripting the parameters for executing BigQuery via a Python script

我正在尝试调整在https://github.com/GoogleCloudPlatform/bigquery-samples-python/tree/master/python/samples中找到的asynch_query.py脚本,以用于执行查询并将输出发送到BigQuery表。 我为设置参数而创建的脚本的JSON部分如下:

    job_data = {
    'jobReference': {
            'projectId': project_id,
            'job_id': str(uuid.uuid4())
            },
    'configuration': {
            'query': {
                    'query': queryString,
                    'priority': 'BATCH' if batch else 'INTERACTIVE',
                    'createDisposition': 'CREATE_IF_NEEDED',
                    'defaultDataset': {
                            'datasetId': 'myDataset'
                            },
                    'destinationTable': {
                            'datasetID': 'myDataset',
                            'projectId': project_id,
                            'tableId': 'testTable'
                            },
                    'tableDefinitions': {
                            '(key)': {
                                    'schema': {
                                        'fields': [
                                        {
                                            'description': 'eventLabel',
                                            'fields': [],
                                            'mode': 'NULLABLE',
                                            'name': 'eventLabel',
                                            'type': 'STRING'
                                        }]
                                    } 
                            }
                    }
            }
    }
    }

运行脚本时,出现错误消息,提示“缺少必需参数”。 我浏览了https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query上的文档,试图找出缺少的内容,但是尝试进行各种配置均失败了。 谁能识别出丢失的内容以及如何解决此错误?

不知道发生了什么。 要将查询结果插入另一个表中,请使用以下代码:

def create_table_from_query(connector, query,dest_table):
body = {
    'configuration': {
        'query': {
            'destinationTable': {
                'projectId': your_project_id,
                'tableId': dest_table,
                'datasetId': your_dataset_id
            },
            'writeDisposition': 'WRITE_TRUNCATE',
            'query': query,
        },
    }
}

response = connector.jobs().insert(projectId=self._project_id,
                                        body=body).execute()
wait_job_completion(response['jobReference']['jobId'])

def wait_job_completion(connector, job_id):
    while True:
        response = connector.jobs().get(projectId=self._project_id,
                                             jobId=job_id).execute()
        if response['status']['state'] == 'DONE':
            return

connector在哪里build('bigquery', 'v2', http=authorization)

也许您可以从那里开始,并根据需要继续添加新字段(请注意,您不必定义表的架构,因为该表的架构已包含在查询结果中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM