【发布时间】:2019-02-19 18:33:32
【问题描述】:
我正在尝试读取管道中的 JSON 文件(多行),但 beam.io.ReadFromText(somefile.json 一次读取一行。
我正在尝试以 JSON 格式读取文件的内容,以便我可以在每个类别上应用 map 以下载相关产品文件。
这就是我的JSON 文件 (productindex.json) 的样子:
{
"productcategories" : {
"category1" : {
"productfile" : "http://products.somestore.com/category1/products.json"
},
"category2" : {
"productfile" : "http://products.somestore.com/category2/products.json"
},
"category3" : {
"productfile" : "http://products.somestore.com/category3/products.json"
},
"category4" : {
"productfile" : "http://products.somestore.com/category4/products.json"
}
}
这是我的管道开始的样子:
with beam.Pipeline(options=pipeline_options) as p:
rows = (
p | beam.io.ReadFromText(
"http://products.somestore.com/allproducts/productindex.json")
)
我正在使用apache-beam[gcp] 模块。
我如何做到这一点?
【问题讨论】:
-
我目前正在使用 Apache Beam 的 Java SDK,但遇到了同样的问题。我用
jsonString.replaceAll("\\R", " ")解决了我的问题。该正则表达式将检测换行符并返回字符。此替换会将您的 json 扁平化为一行。在 Python 中,它类似于jsonString.replace("\n\r", " ")。
标签: python google-cloud-platform google-cloud-dataflow apache-beam