无法在python

时间:2017-07-19 02:16:33

标签: python google-bigquery

我试图通过Python脚本查询Bigquery中的表。但是我已经将查询编写为标准的sql查询。为此,我需要用'#standardsq'开始我的查询。但是,当我这样做时,它会注释掉我的其余查询。我曾尝试使用多行编写查询,但它也不允许我这样做。有没有人处理这样的问题,并找到了解决方案?下面是我的第一个代码,其中查询被注释掉了。

client = bigquery.Client('dataworks-356fa')

query = ("#standardsql SELECT count(distinct serial) FROM `dataworks-356fa.FirebaseArchive.test2`  Where (PeripheralType = 1 or PeripheralType = 2 or PeripheralType = 12) AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1 AND serial != 'null'")

dataset = client.dataset('FirebaseArchive')
table = dataset.table('test2')
tbl = dataset.table('Count_BB_Serial_weekly')
job = client.run_async_query(str(uuid.uuid4()), query)
job.destination = tbl
job.write_disposition= 'WRITE_TRUNCATE'
job.begin()

当我尝试编写这样的查询时,python不会读取第二行上的任何内容作为查询。

query = ("#standardsql 
         SELECT count(distinct serial) FROM `dataworks-356fa.FirebaseArchive.test2`  Where (PeripheralType = 1 or PeripheralType = 2 or PeripheralType = 12) AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1 AND serial != 'null'")

运行的查询选择上周内生成的值。如果有一个变体,使用standardsql不需要我也愿意切换我的其他查询,但我无法弄清楚如何做到这一点。我宁愿这是最后的手段。谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

如果您希望标记您将在查询本身内使用标准SQL,则可以将其构建为:

query = """#standardSQL
           SELECT count(distinct serial) FROM `dataworks-356fa.FirebaseArchive.test2`  Where (PeripheralType = 1 or PeripheralType = 2 or PeripheralType = 12) AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1 AND serial != 'null' 
        """

您可以使用的另一个选项是将use_legacy_sql创建的属性job设置为False,例如:

job = client.run_async_query(job_name, query)
job.use_legacy_sql = False # -->this also makes the API use Standard SQL
job.begin()