LoginSignup
10
9

More than 5 years have passed since last update.

Flumeを使いMac上でTwitterのツイートを取得する

Last updated at Posted at 2015-05-26

Apache Flume 1.5.0 で Twitter Source が入ったので、Twitter から簡単にデータを取得することができる。(注: Experimental のため利用には十分注意すること)
この記事ではMac上で簡単に試すための方法を紹介する。

環境

  • Flume バージョン: CDH 5.4.2 (1.5.0 ベース)
  • OS: Mac OS X 10.10.3

準備

ファイルのダウンロード

Cloudera のリポジトリからFlumeのtar.gzを取得する。 最新版へのリンク
適当な場所に Flume を設置し、展開する。私は ~/lib/flume にしている。

次に avro-tools をインストールする。

$ brew install avro-tools

Twitter API キーとアクセストークンの取得

https://apps.twitter.com/ から取得する。取得の仕方はググってください。
以下の4つが必要。

  • consumer key
  • consumer secret
  • access token
  • access token secret

設定ファイルの作成

適当なファイルを作る。ここでは ~/etc/flume/twitter.confとする。
以下の情報を入力する。

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = org.apache.flume.source.twitter.TwitterSource
a1.sources.r1.channels = c1
a1.sources.r1.consumerKey = YOUR_CONSUMER_KEY
a1.sources.r1.consumerSecret = YOUR_CONSUMER_KEY_SECRET
a1.sources.r1.accessToken = YOUR_ACCESS_TOKEN
a1.sources.r1.accessTokenSecret = YOUR_ACCESS_TOKEN_SECRET
a1.sources.r1.maxBatchSize = 50000
a1.sources.r1.maxBatchDurationMillis = 100000

# Describe the sink
# a1.sinks.k1.type = logger
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /tmp/flume-log
a1.sinks.k1.sink.rollInterval = 120

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.transactionCapacity = 1000

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

データの保存ディレクトリの作成

この例では /tmp/flume-log にデータを保存するのでディレクトリを作っておく。

ログの取得と閲覧

Flume の起動

~/lib/flume に移動して以下のコマンドを実行すればログの収集が始まる。

$ bin/flume-ng agent -n a1 -c conf -f ~/etc/flume/twitter.conf -Dflume.root.logger=INFO,console -Xmx1g

avro-tools によるログの閲覧

Avroはスキーマつきのシリアライザ。Avro形式のままHiveにスキーマを認識させてデータをロードさせたりすることも可能。
ローカルで内容を閲覧するにはデシリアライズするための何らかのツールが必要となる。
ここでは avro-tools を使う。

/tmp/flume-log の下に移動し、生成されたデータを avro-tools で読む。

$ avro-tools tojson --pretty 生成されたファイル | less

これで中身をjson形式で読むことができる。

スキーマ情報を見たい場合は avro-tools getschema コマンドを使う。

$ avro-tools getschema 生成されたファイル

スキーマはjson形式で保持されているので閲覧は簡単。

参考

Flumeユーザガイド: Twitter 1% firehose Source

10
9
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
10
9