【发布时间】:2021-06-18 08:30:54
【问题描述】:
我有 2 个 json 配置文件要读取并希望将值分配给变量。我正在使用 apache Beam 创建数据流作业,但无法解析这些文件并将值分配给变量。
config1.json - { "bucket_name": "mybucket"}
config2.json - { "dataset_name": "mydataset"}
这是管道语句 ---- 我首先尝试使用一个 JSON 文件,但即使这样也不起作用
with beam.Pipeline(options=pipeline_options) as pipeline:
steps = (pipeline
| "Getdata" >> beam.io.ReadFromText(custom_options.configfile)
| "CUSTOM JSON PARSE" >> beam.ParDo(custom_json_parser(custom_options.configfile))
| "write to GCS" >> beam.io.WriteToText('gs://mynewbucket/outputfile.txt')
)
result = pipeline.run()
result.wait_until_finish()
我还尝试创建一个函数来解析至少一个文件。这是我创建的示例方法,但它不起作用。
class custom_json_parser(beam.DoFn):
import apache_beam as beam
from apache_beam.io.gcp import gcsio
import logging
def __init__(self, configfile):
self.configfile = configfile
def process(self, configfile):
logging.info("JSON PARSING STARTED")
with beam.io.gcp.gcsio.GcsIO().open(self.configfile, 'r') as f:
for line in f:
data = json.loads(line)
bucket = data.get('bucket_name')
dataset = data.get('dataset_name') ```
Can someone please suggest the best method to resolve this issue in apache beam?
Thanks in Advance
【问题讨论】:
-
管道是否多次读取这些文件?或者您可以在开始时只读取一次并使用这些参数执行整个管道吗?
-
我只想在开始时读取它们并执行整个管道
标签: json google-cloud-platform apache-beam dataflow apache-beam-io