python-3.x - 旁加载静态数据

标签 python-3.x google-cloud-dataflow apache-beam

在 ParDo 中处理数据时,我需要使用存储在 Google Cloud Storage 上的 JSON 架构。我想这可能是侧面加载?我阅读了他们称为文档的页面( https://beam.apache.org/releases/pydoc/2.16.0/apache_beam.pvalue.html ),其中包含有关 apache_beam.pvalue.AsSingletonapache_beam.pvalue.AsSideInput 的内容,但如果我 Google 搜索,结果为零关于这些的用法,我找不到任何 Python 的示例。

如何从 ParDo 内的存储中读取文件?或者我是否在 ParDo 之前旁加载到管道,但如何在 ParDo 内利用第二个源?

[编辑]

我的主要数据来自BQ:beam.io.Read(beam.io.BigQuerySource(...
侧面输入也来自 BQ,使用相同的 BigQuerySource

当我在主数据侧输入其他数据之后添加一个步骤时,我收到一些奇怪的错误。我注意到,当我对侧面输入执行 beam.Map(lambda x: x) 时,它会起作用。

侧边输入

schema_data = (p | "read schema data" >> beam.io.Read(beam.io.BigQuerySource(query=f"select * from `{schema_table}` limit 1", use_standard_sql=True, flatten_results=True))
                         | beam.Map(lambda x: x)
                       )

主要数据

    source_data = (p | "read source data" >> beam.io.Read(beam.io.BigQuerySource(query=f"select {columns} from `{source_table}` limit 10", use_standard_sql=True, flatten_results=True)))  

组合

validated_records = source_data | 'record validation' >> beam.ParDo(Validate(), pvalue.AsList(schema_data))

最佳答案

我将使用您提到的文档作为库引用,并浏览 Beam 编程指南以获取更详细的演练:side input section 。我将尝试提供几个示例,其中我们将从公共(public)表下载 BigQuery 架构并将其上传到 GCS:

bq show --schema bigquery-public-data:usa_names.usa_1910_current > schema.json
gsutil cp schema.json gs://$BUCKET

我们的数据将是一些没有标题的 csv 行,因此我们必须使用 GCS 架构:

data = [('NC', 'F', 2020, 'Hello', 3200),
        ('NC', 'F', 2020, 'World', 3180)]

使用侧面输入

我们将 JSON 文件读入 schema PCollection:

schema = (p 
  | 'Read Schema from GCS' >> ReadFromText('gs://{}/schema.json'.format(BUCKET)))

然后我们将其作为辅助输入传递给 ParDo,以便将其广播给执行 DoFn 的每个工作线程。在这种情况下,我们可以使用 AsSingleton,因为我们只想将架构作为单个值提供:

(p
  | 'Create Events' >> beam.Create(data) \
  | 'Enrich with side input' >> beam.ParDo(EnrichElementsFn(), pvalue.AsSingleton(schema)) \
  | 'Log elements' >> beam.ParDo(LogElementsFn()))

现在我们可以在 EnrichElementsFnprocess 方法中访问schema:

class EnrichElementsFn(beam.DoFn):
  """Zips data with schema stored in GCS"""
  def process(self, element, schema):
    field_names = [x['name'] for x in json.loads(schema)]
    yield zip(field_names, element)

请注意,最好在将其保存为单例之前进行架构处理(构造 field_names)以避免重复工作,但这只是一个说明性示例。


使用启动包

在这种情况下,我们不会将任何额外的输入传递给 ParDo:

(p
  | 'Create Events' >> beam.Create(data) \
  | 'Enrich with start bundle' >> beam.ParDo(EnrichElementsFn()) \
  | 'Log elements' >> beam.ParDo(LogElementsFn()))

现在,我们使用 Python 客户端库(我们需要安装 google-cloud-storage)在每次工作人员初始化包时读取架构:

class EnrichElementsFn(beam.DoFn):
  """Zips data with schema stored in GCS"""
  def start_bundle(self):
    from google.cloud import storage

    client = storage.Client()
    blob = client.get_bucket(BUCKET).get_blob('schema.json')
    self.schema = blob.download_as_string()

  def process(self, element):
    field_names = [x['name'] for x in json.loads(self.schema)]
    yield zip(field_names, element)

这两种情况的输出是相同的:

INFO:root:[(u'state', 'NC'), (u'gender', 'F'), (u'year', 2020), (u'name', 'Hello'), (u'number', 3200)]
INFO:root:[(u'state', 'NC'), (u'gender', 'F'), (u'year', 2020), (u'name', 'World'), (u'number', 3180)]

使用 2.16.0 SDK 和 DirectRunner 进行测试。

两个示例的完整代码 here .

关于python-3.x - 旁加载静态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59458599/

相关文章:

python - Pyspark - Python3 使用 configparser 从文件中获取变量

google-bigquery - Cloud Dataflow 中的 'side inputs' 是否支持从 BigQuery View 读取?

google-bigquery - 来自 Apache Beam 的 BigQuery 授权 View

java - 当前测试使用 MapState 的 DoFn 的最佳实践是什么

python - apache-beam 从 GCS 桶的多个文件夹中读取多个文件并加载它 bigquery python

java - 在 Apache Beam 中使用 defaultNaming 进行动态窗口写入

Python:如何使用 Apache Beam 连接到 Snowflake?

python - "Exception"是否捕获所有其他 "Concrete Exceptions"

python-3.x - 将文件写入azure函数

python - 将发散颜色居中至零