在 Python 中使用 Apache Beam 读取多行 JSON

问题描述 投票:0回答:0

我无法正确从 Google Cloud Storage 读取 JSON 文件。 输入的格式在他的基本结构中看起来像这样:

[
 {
   "id": "CANT14",
   "entity": "Comunità del Collio",
  },
]

如果我将文件保存在我的计算机上并将其缩小,使其结构在一行中,管道工作:

[{"id": "CANT14","entity": "Comunità del Collio"}]

问题是我需要直接从谷歌云存储中读取文件,而不需要任何进一步的步骤。

流水线长这样:

with beam.Pipeline() as p:
    # Read JSON file from Google Storage bucket
    content = (p | "Read file" >> beam.io.ReadFromText(bucket))

     # Parse JSON
    json = content | 'Parse JSON' >> beam.Map(json.loads)

    # Print result
    json | 'Print content' >> beam.Map(print)
    

这里的错误:

RuntimeError: json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

我已经通过各种在线工具检查了 JSON,它是有效的。唯一的问题是它只是一个多行 JSON。

这是我第一次使用 Apache Beam,我可能遗漏了一些东西。 有什么办法可以解决吗?

谢谢!

python json google-cloud-storage google-cloud-dataflow apache-beam
© www.soinside.com 2019 - 2024. All rights reserved.