LoginSignup
16
15

More than 5 years have passed since last update.

embulk-input-httpの使い方

Last updated at Posted at 2015-03-07

Embulkを使って、データの入力元にHTTPを指定するシーンが出てきたので、pluginを作ってみました。

既存のparser pluginと組み合わせることで、例えば、HTTP-APIから取得したjson(or XML)をparseして、各レコードをRTBやelasticsearchに登録、みたいなシーンで、かなり有用だと思います。

embulk-input-http

以下で公開しています

https://github.com/takumakanari/embulk-input-http

https://rubygems.org/gems/embulk-input-http

取得したデータのparseに別途parser pluginの指定が必要です。
最近のHTTP APIで普遍的になりつつあるjson/xml向けに、以下のpluginを作成しています。

json: https://github.com/takumakanari/embulk-parser-json

xml: https://github.com/takumakanari/embulk-parser-xml

もちろん、Embulk本体のbuiltin-parserや、既存のpluginを利用することも可能です。

使い方

実際にHeartRailsのjson apiを使って、データを取得してみます。

インストール

$ embulk gem install embulk-input-http embulk-parser-json

config.yml

in:
  type: http
  url: http://express.heartrails.com/api/json
  params:
    - {name: method, value: getStations}
    - {name: x, value: 135.0}
    - {name: y, value: 35.0}
  parser:
    type: jsonpath
    root: $.response.station
    schema:
      - {name: name, type: string}
      - {name: next, type: string}
      - {name: prev, type: string}
      - {name: distance, type: string}
      - {name: lat, type: double, path: x}
      - {name: lng, type: double, path: y}
      - {name: line, type: string}
      - {name: postal, type: string}
  method: get
  • type: httpを指定します
  • url: 取得元のURL (必須)
  • params: クエリパラメータ(POSTの場合はbody)に指定するkey/valueを設定(任意)
  • parser: parser pluginを指定します
  • method: HTTP method (任意、デフォルトはGET)

embulk-parser-jsonの利用方法

ここでは、parserにembulk-parser-jsonを利用して、レスポンスからデータを取り出しています。

以下がparserの設定部分です。

parser:
  type: json
  root: $.response.station
  schema:
    - {name: name, type: string}
    - {name: next, type: string}
    - {name: prev, type: string}
    - {name: distance, type: string}
    - {name: lat, type: double, path: x}
    - {name: lng, type: double, path: y}
    - {name: line, type: string}
    - {name: postal, type: string}

取得したデータをどう解釈し、どの部分をschemaにバインドするかを指定します。

  • type: jsonを指定します
  • root: データの読み出し位置です(jsonpath形式)
  • schema: データ構造です

この設定で、次のjsonから、配列stationが持つobjectをレコードとして読み出すことができます。

{
  "response": {
    "station": [
      {
        "x": 134.997666,
        "next": "黒田庄",
        "prev": "比延",
        "distance": "310m",
        "y": 35.002054,
        "line": "JR加古川線",
        "postal": "6770039",
        "name": "日本へそ公園",
        "prefecture": "兵庫県"
      },
      {
        "x": 134.99574,
        "next": "日本へそ公園",
        "prev": "新西脇",
        "distance": "1310m",
        "y": 34.988777,
        "line": "JR加古川線",
        "postal": "6770033",
        "name": "比延",
        "prefecture": "兵庫県"
      },
      ....
    ]
  }
}

実行

outputにstdoutを指定して実行してみます。

$ embulk run config.yml
GET http://express.heartrails.com/api/json?method=getStations&x=135.0&y=35.0
日本へそ公園,黒田庄,比延,310m,134.997666,35.002054,JR加古川線,6770039
比延,日本へそ公園,新西脇,1310m,134.99574,34.988777,JR加古川線,6770033
黒田庄,本黒田,日本へそ公園,2620m,134.992545,35.02274,JR加古川線,6790313
2015-03-07 03:39:00.821 -0500 [INFO] (transaction): {done:  1 / 1, running: 0}
2015-03-07 03:39:00.840 -0500 [INFO] (main): Committed.
2015-03-07 03:39:00.842 -0500 [INFO] (main): Next config diff: {"out":{}}

TODO

Proxyなど、いろいろ未対応なので、順次取り込んで行く予定

16
15
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
16
15