2017-09-28 31 views
0

我有一個關於在Datalab中使用BigQuery中的子查詢的問題。這是特定於google.datalab.bigquery.Query。我可以在Datalab中使用帶有%bq命令行的子查詢。如何在Datalab的api中使用BigQuery中的子查詢?

%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命令行代碼正常工作:

假設,我用BQ%象這些在Datalab。但是,我想用更多的編程方式來使用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文件,我應該能夠使用:

類谷歌。 datalab.bigquery.Query(sql,env = None,udfs = None,data_sources = None,subqueries = None)

我做錯了什麼?有什麼建議麼?

回答

1

你只需要在一個數組來傳遞:

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

感謝yelsayed。但是,當我嘗試使用子查詢列表時,我收到了另一個錯誤消息。所以,我回到了文檔。然後,我試圖將子查詢放入字典中。最後,我獲得了成功的查詢結果! – Choppy

相關問題