Google Cloud datalab 查询 BIgQuery 表时出错

Posted

技术标签:

【中文标题】Google Cloud datalab 查询 BIgQuery 表时出错【英文标题】:Google Cloud datalab error querying BIgQuery tables 【发布时间】:2016-10-05 18:56:30 【问题描述】:

我想我在这里遗漏了一些基本的东西,似乎无法弄清楚它是什么..

从 Google 云数据实验室查询 BigQuery 日期分区表。大多数其他查询都按预期获取数据,不知道为什么在这个特定的表中,select 不起作用,但是 count(1) 查询起作用。

%%sql
select * from Mydataset.sample_sales_yearly_part limit 10

我得到以下错误:

KeyErrorTraceback (most recent call last) /usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in
__call__(self, obj)
    305                 pass
    306             else:
--> 307                 return printer(obj)
    308             # Finally look for special method names
    309             method = get_real_method(obj, self.print_method)

/usr/local/lib/python2.7/dist-packages/datalab/bigquery/commands/_bigquery.pyc in _repr_html_query_results_table(results)
    999     1000 def _repr_html_query_results_table(results):
-> 1001   return _table_viewer(results)    1002     1003 

/usr/local/lib/python2.7/dist-packages/datalab/bigquery/commands/_bigquery.pyc in _table_viewer(table, rows_per_page, fields)
    969     meta_time = ''
    970 
--> 971   data, total_count = datalab.utils.commands.get_data(table, fields, first_row=0, count=rows_per_page)
    972 
    973   if total_count < 0:

/usr/local/lib/python2.7/dist-packages/datalab/utils/commands/_utils.pyc in get_data(source, fields, env, first_row, count, schema)
    226     return _get_data_from_table(source.results(), fields, first_row, count, schema)
    227   elif isinstance(source, datalab.bigquery.Table):
--> 228     return _get_data_from_table(source, fields, first_row, count, schema)
    229   else:
    230     raise Exception("Cannot chart %s; unsupported object type" % source)

/usr/local/lib/python2.7/dist-packages/datalab/utils/commands/_utils.pyc in _get_data_from_table(source, fields, first_row, count, schema)
    174   gen = source.range(first_row, count) if count >= 0 else source
    175   rows = ['c': ['v': row[c] if c in row else  for c in fields] for row in gen]
--> 176   return 'cols': _get_cols(fields, schema), 'rows': rows, source.length
    177 
    178 

/usr/local/lib/python2.7/dist-packages/datalab/utils/commands/_utils.pyc in _get_cols(fields, schema)
    108     if schema:
    109       f = schema[col]
--> 110       cols.append('id': f.name, 'label': f.name, 'type': typemap[f.data_type])
    111     else:
    112       # This will only happen if we had no rows to infer a schema from, so the type

KeyError: u'DATE'

QueryResultsTable job_Ckq91E5HuI8GAMPteXKeHYWMwMo

【问题讨论】:

【参考方案1】:

您可能遇到了刚刚在 https://github.com/googledatalab/pydatalab/pull/68 中修复的问题(但尚未包含在 Datalab 版本中)。

背景是 BigQuery 中新的“标准 SQL”支持添加了可以显示在结果架构中的新数据类型,而 Datalab 尚未更新以处理这些。

Datalab 的下一个版本应该会解决此问题,但与此同时,您可以通过将日期字段包装在显式转换为 TIMESTAMP 作为查询的一部分来解决此问题。

例如,如果您在以下代码单元中看到该错误:

%%sql SELECT COUNT(*) as count, d FROM <mytable>

(其中“d”是“DATE”类型的字段),那么您可以通过将该字段转换为 TIMESTAMP 来解决此问题,如下所示:

%%sql SELECT COUNT(*) as count, TIMESTAMP(d) FROM <mytable>

对于您的特定查询,您必须将“*”更改为字段列表,以便您可以将带有日期的查询转换为时间戳

【讨论】:

感谢 Omar,我能够通过转换日期字段字符串来克服这个问题。我会寻找新版本的cloud datalab。 如何更改设置以在 cloud datalab 中默认使用标准 sql? 新版本现已发布。至于选择标准 SQL,现在有一个“-d 标准”标志,您可以将其添加到 %%sql 单元魔法中……在捆绑教程中查找“BigQuery Standard SQL.ipynb”笔记本以获取示例。跨度> 谢谢奥马尔。这有帮助。

以上是关于Google Cloud datalab 查询 BIgQuery 表时出错的主要内容,如果未能解决你的问题,请参考以下文章