0
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?

DockerでEmbulkの環境を作ってCSV/スプレッドシートを読み込ませてみる

Posted at

Dockerの中でEmbulk環境を構築

docker run -it --rm openjdk:8-slim bash

実行するとdockerの中に入ります。
javaが入っているか下記のコマンドで確認。

java -version

Embulkのインストール

curlをインストール

apt-get update
apt-get install -y curl

今回はEmbulkの公式サイトでPast stableになっているv0.9.25を指定します。
インストールして権限を付与するコマンドを実行。

curl --create-dirs -o /usr/local/bin/embulk -L "https://dl.embulk.org/embulk-0.9.25.jar"
chmod +x /usr/local/bin/embulk

Embulkのバージョンを確認すると無事入っているのが確認できます。

embulk --version

Dockerの設定

下記のようにフォルダを設定します。

files
├── embulk
│   └── Dockerfile
└── compose.yaml
compose.yaml
services:
  embulk:
    container_name: embulk
    build: ./embulk
    tty: true
Dockerfile
FROM openjdk:8-slim

# Install embulk
RUN apt-get update \
    && apt-get install -y curl \
    && curl --create-dirs -o /usr/local/bin/embulk -L "https://dl.embulk.org/embulk-0.9.25.jar" \
    && chmod +x /usr/local/bin/embulk

用意できたら次のコマンドでDockerを立ち上げます。

docker compose up -d
docker exec -it embulk /bin/bash

dockerの中でEmbulkが入っているかを確認

embulk --version

DockerでCSVファイルを読み込ませてみる

フォルダ構成は次のように変更します。

files
├── embilk
│   ├── sample
│   │   └── customers.csv
│   ├── seed.yaml
│   └── Dockerfile
└── compose.yaml

下記を変更・追加します。

Dockerfile
FROM openjdk:8-slim

# Install embulk
RUN apt-get update \
    && apt-get install -y curl \
    && curl --create-dirs -o /usr/local/bin/embulk -L "https://dl.embulk.org/embulk-0.9.25.jar" \
    && chmod +x /usr/local/bin/embulk
WORKDIR /work
COPY ./ ./
customers.csv
CustomerID,CustomerName,Email
1,Alice Johnson,alice@example.com
2,Bob Smith,bob@example.com
3,Charlie Lee,charlie@example.comcp
seed.yaml
in:
  type: file
  path_prefix: "./sample/customers.csv"
out:
  type: stdout

再度Dockerを作成して入ります。

docker compose down --rmi all
docker compose up -d
docker exec -it embulk /bin/bash

seed.yamlを元にEmbulkでCSVを読み込んでコンソールに出力するためのyamlファイルを作成します。

embulk guess seed.yaml -o config.yaml

実行すると下記が入った config.yaml が作成されます。
これがCSVで取得してコンソールに出力するための設定ファイルになります。

config.yaml
in:
  type: file
  path_prefix: ./sample/customers.csv
  parser:
    charset: UTF-8
    newline: LF
    type: csv
    delimiter: ','
    quote: '"'
    escape: '"'
    trim_if_not_quoted: false
    skip_header_lines: 1
    allow_extra_columns: false
    allow_optional_columns: false
    columns:
    - {name: CustomerID, type: long}
    - {name: CustomerName, type: string}
    - {name: Email, type: string}
out: {type: stdout}

これに対して下記のコマンドで実行できます。

embulk run config.yaml

下記でプレビューも実行できます。

embulk preview config.yaml

スプレッドシート情報取得

[作成途中]

サービスアカウントの作成

files
├── embilk
│   ├── sample
│   │   └── customers.csv
│   ├── ss.yaml
│   └── Dockerfile
└── compose.yaml
in:
    type: google_spreadsheets
    auth_method: service_account
    json_keyfile: service_account.json
    spreadsheets_url: https://docs.google.com/spreadsheets/d/************/edit#gid=0
    worksheet_title: シート1
    start_row: 2
    default_timezone: 'Asia/Tokyo'
    null_string: '\N'
    default_typecast: strict
    columns:
      - {name: CustomerID, type: long}
      - {name: CustomerName, type: string}
      - {name: Email, type: string}
out: {type: stdout}
docker compose down --rmi all
docker compose up -d
docker exec -it embulk /bin/bash

Dockerに入って下記を実行

embulk gem install embulk-input-google_spreadsheets
embulk gem install embulk-output-bigquery

digdagの環境構築

Dockerの中に入って、公式サイトどおりに試してみまたら実行でいると思います。
https://docs.digdag.io/getting_started.html

0
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
0
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?