从Dataflow到BigQuerySource的简单查询抛出异常

时间:2017-04-25 21:39:44

标签: google-bigquery google-cloud-platform google-cloud-dataflow

我正在尝试编写一个简单的Dataflow作业,该作业利用query类中的BigQuerySource参数。

简单来说,我可以使用BigQuerySource类访问BigQuery表,然后对其进行过滤。我无法使用BigQuerySource直接查询/过滤BigQuery表。

这是一些代码。在我的Dataflow管道中内联过滤可以正常工作:

import argparse
import apache_beam as beam

parser = argparse.ArgumentParser()
parser.add_argument('--output', required=True)
known_args, pipeline_args = parser.parse_known_args(None)    
p = beam.Pipeline(argv=pipeline_args)

source = 'bigquery-public-data:samples.shakespeare'
rows = p | 'read'>>beam.io.Read(beam.io.BigQuerySource(source))
f = rows | 'filter' >> beam.Map(lambda row: 1 if (row['word_count'] > 1) else 0) 

f | 'write' >> beam.io.WriteToText(known_args.output)    
p.run()

用单行查询替换该中间节会产生错误。

f = p | 'read' >> beam.io.Read(beam.io.BigQuerySource('SELECT 1 FROM ' \
    + 'bigquery-public-data:samples.shakespeare where word_count > 1'))

返回的错误看起来像语法错误。

(a29eabc394a38f62): Workflow failed. Causes: 
(a29eabc394a38cfa): S04:read+write/Write/WriteImpl/WriteBundles+write/Write/WriteImpl/Pair+write/Write/WriteImpl/WindowInto(WindowIntoFn)+write/Write/WriteImpl/GroupByKey/Reify+write/Write/WriteImpl/GroupByKey/Write failed.,
(fb6d0643d7f13886): BigQuery execution failed., 
(fb6d0643d7f13b03): Error:  Message: Encountered " "-" "- "" at line 1, column 59. Was expecting: <EOF>

我是否需要转义BigQuery项目名称中的-个字符?

1 个答案:

答案 0 :(得分:4)

在BigQuery Legacy SQL中 - 您应该使用[]转义整个表格引用 对于标准SQL,出于同样的原因,您应该使用back-ticks

另见Escaping reserved keywords and invalid identifiers