如何在Google云存储中将avro文件读取为json文本

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

我希望能够使用类似于“hdfs -text *.avro”但在 Google 云存储中的内容将 avro 文件读取为 json。我将在 python 脚本中解析 json

我正在寻找一种 gsutil 命令方式来将 avro 文件读取为 json,类似于我们在 hdfs 中的做法

json google-cloud-platform google-cloud-storage cloud avro
1个回答
0
投票

没有直接的方法来读取 Avro 文件

如何在 Google Cloud Storage 中读取 Avro 文件作为 JSON 文本:

  1. 在本地计算机上安装
    avro-tools
    命令。
  2. 运行以下 gsutil 命令:
gsutil cat gs://<bucket>/<path/to/avro/file.avro> | avro-tools tojson > <path/to/json/file.json>

这将从 Google Cloud Storage 读取 Avro 文件,将其转换为 JSON,并将 JSON 输出写入指定文件。

  1. 使用 JSON 解析器(例如
    json
    模块)解析 Python 脚本中的 JSON 文件。

这是解析 JSON 文件的 Python 脚本示例

my-json-file.json
:

import json

with open("my-json-file.json", "r") as f:
    json_data = json.load(f)

# Iterate over the JSON data and do something with it
for record in json_data:
    print(record)
© www.soinside.com 2019 - 2024. All rights reserved.