如何在Datalab的API中使用Bigquery中的子查询?

时间:2017-09-28 04:47:42

标签: python google-bigquery google-cloud-datalab

我有一个关于在Datalab中使用BigQuery中的子查询的问题。这是google.datalab.bigquery.Query特有的。我可以在Datalab中使用%bq命令行的子查询就好了。

假设我在Datalab中使用%bq作为:

%bq query --name my_table1
select col1, col2 from dataset1.table1

%bq query --subqueries my_table1
select count(col1) as some_count from my_table1 where col1 is null

以上%bq命令行代码正常工作。但是,我想使用python Datalab API以更加编程的方式完成它。所以在Datalab中,我做了:

sql_str1 = '''select col1, col2 from dataset1.table1'''

my_table1 = bq.Query(sql_str1)

sql_str2 = '''select count(col1) as some_count from my_table1 where col1 is null'''

bq.Query(sql_str2, subqueries= my_table1).execute().result()

然后,我收到了错误消息:

TypeErrorTraceback (most recent call last)
<ipython-input-6-c625b8e326b9> in <module>()
----> 1 bq.Query(sql_command, subqueries=web_activity).execute().result()

/usr/local/lib/python2.7/dist-packages/google/datalab/bigquery/_query.pyc in __init__(self, sql, env, udfs, data_sources, subqueries)
     79 
     80     if subqueries:
---> 81       _expand_objects(subqueries, Query, self._subqueries)
     82     if udfs:
     83       _expand_objects(udfs, _udf.UDF, self._udfs)

/usr/local/lib/python2.7/dist-packages/google/datalab/bigquery/_query.pyc in _expand_objects(obj_container, obj_type, target_list)
     59     # and add them to the target dictionary
     60     def _expand_objects(obj_container, obj_type, target_list):
---> 61       for item in obj_container:
     62         # for a list of objects, we should find these objects in the given environment
     63         if isinstance(obj_container, list):

TypeError: 'Query' object is not iterable

但是根据http://googledatalab.github.io/pydatalab/google.datalab.bigquery.html的文件,我应该可以使用这个:

class google.datalab.bigquery.Query(sql,env = None,udfs = None,data_sources = None,subqueries = None)

我做错了什么?有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你只需要传入一个数组:

bq.Query(sql_str2, subqueries=[my_table1]).execute().result()