1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで`.avro`を`.json`に変換するツールを雑にまとめる記事

Posted at

この記事は?

ApacheAvroとは?

業務で.avro.jsonに変換するツールを作る機会があったのでまとめてみました。

対象者

私のようなこれからavroを触っていく人

環境

  • macOS Monterey M1チップ
  • Docker Desktop

ツール

source

File tree
pj-root
  ┣ docker/
  ┃   ┗ tools/
  ┃         ┗ Dockerfile
  ┣ tools/
  ┃   ┣ avro_converter.py
  ┃   ┣ avro_input/
  ┃   ┃     ┗ input.avro # 変換したいファイルを配置
  ┃   ┃ avro_output/
  ┃   ┗ requirements.txt
  ┗ docker-compose.yml
docker/tools/Dockerfile
FROM python:3.8
ENV WORK_DIR /tools
WORKDIR $WORK_DIR
COPY /tools .

RUN pip install --no-cache-dir -r requirements.txt
ENTRYPOINT ["/bin/sh", "-c", "while :; do sleep 10; done"]
tools/avro_converter.py
import avro.datafile
import avro.io
import json

with open('avro_input/input.avro', 'rb') as f:
  reader = avro.datafile.DataFileReader(f, avro.io.DatumReader())
  records = [r for r in reader]

with open('avro_output/output.json', 'w') as f:
  f.write(json.dumps(records, indent=2))

変換後は1行のjsonとなり見づらい&サイズによってはエディターが重くなるので、json.dumpsはインデントつけた方が良いです。

requirements.txt
avro-python3
docker-compose.yml
version: '3'
services:
  tools:
    build:
      context: .
      dockerfile: docker/tools/Dockerfile
    volumes:
      - ./tools:/tools

実行

Terminal
docker-compose build
docker-compose up &
docker-compose exec tools python avro_converter.py

実行するとavro_output/配下に.jsonファイルが出来てるかと思います。

最後に

気になる点があればコメントお待ちしてます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?