2017-09-28 3 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中のSO、私がやった:

クラスグーグル:

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の文書によると、私はこれを使用することができるはずです。

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() 

その後、私はエラーメッセージを得ました。 datalab.bigquery.Query(sql、env =なし、udfs =なし、data_sources =なし、サブクエリ=なし)

どうしたのですか?助言がありますか?

答えて

1

あなただけの配列を渡す必要があります。

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

おかげでyelsayed。しかし、サブクエリのリストを使用しようとすると、別のエラーメッセージが表示されます。そこで、私はその文書に戻った。次に、サブクエリを辞書に入れようとしました。最後に、私は成功したクエリ結果を得ました! – Choppy

関連する問題