简体   繁体   English

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

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

I'm trying to adapt the asynch_query.py script found at https://github.com/GoogleCloudPlatform/bigquery-samples-python/tree/master/python/samples for use in executing a query and having the output go to a BigQuery table. 我正在尝试调整在https://github.com/GoogleCloudPlatform/bigquery-samples-python/tree/master/python/samples中找到的asynch_query.py脚本,以用于执行查询并将输出发送到BigQuery表。 The JSON section of the script as I've created it for seting the parameters is as follows: 我为设置参数而创建的脚本的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'
                                        }]
                                    } 
                            }
                    }
            }
    }
    }

When I run my script I get an error message that a "Required parameter is missing". 运行脚本时,出现错误消息,提示“缺少必需参数”。 I've been through the documentation at https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query trying to figure out what is missing, but attempts at various configurations have failed. 我浏览了https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query上的文档,试图找出缺少的内容,但是尝试进行各种配置均失败了。 Can anyone identify what is missing and how I would fix this error? 谁能识别出丢失的内容以及如何解决此错误?

Not sure what's going on. 不知道发生了什么。 To insert the results of a query into another table I use this code: 要将查询结果插入另一个表中,请使用以下代码:

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

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

Maybe you could start from there and keep adding new fields as you wish (notice that you don't have to define the schema of the table as it's already contained in the results of the query). 也许您可以从那里开始,并根据需要继续添加新字段(请注意,您不必定义表的架构,因为该表的架构已包含在查询结果中)。

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

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