首页 文章

Python gcloud BigQuery中的run_async_query使用标准SQL而不是旧版SQL

提问于
浏览
0

我需要使用gcloud python BigQuery库运行异步查询 . 此外,我需要使用beta standard sql而不是默认legacy sql来运行查询 .

根据文档hereherehere我相信我应该能够将作业上的 use_legacy_sql 属性设置为 False . 但是,由于针对旧版SQL处理查询,这仍然会导致错误 . How do I successfully use this property to indicate which SQL standard I want the query to be processed with?

示例Python代码如下:

stdz_table = stdz_dataset.table('standardized_table1')
job_name = 'asyncjob-test'
query = """
    SELECT TIMESTAMP('2016-03-30 10:32:15', 'America/Chicago') AS special_date 
    FROM my_dataset.my_table_20160331;
    """
stdz_job = bq_client.run_async_query(job_name,query)
stdz_job.use_legacy_sql = False
stdz_job.allow_large_results = True
stdz_job.create_disposition = 'CREATE_IF_NEEDED'
stdz_job.destination = stdz_table
stdz_job.write_disposition = 'WRITE_TRUNCATE'
stdz_job.begin()

# wait for job to finish
while True:
    stdz_job.reload()
    if stdz_job.state == 'DONE':
        # print use_legacy_sql value, and any errors (will be None if job executed successfully)
        print stdz_job.use_legacy_sql
        print json.dumps(stdz_job.errors)
        break
    time.sleep(1)

这输出:

False
[{"reason": "invalidQuery", "message": "2.20 - 2.64: Bad number of arguments. Expected 1 arguments.", "location": "query"}]

如果您使用旧版SQL在BigQuery控制台中运行它,则会出现同样的错误 . 当我在BigQuery控制台中复制粘贴查询并使用标准SQL运行它时,它执行正常 . 注意:错误位置(2.20 - 2.64)可能不完全正确的上述查询,因为它是一个示例,我已经混淆了我的一些个人信息 .

1 回答

  • 1

    use_legacy_sql property从版本0.17.0开始不存在,因此您需要检查当前的主分支 . 然而它现在确实存在作为版本0.18.0所以在通过pip升级gcloud-python后你应该很高兴 .

相关问题