2013年1月4日金曜日

Node.js:Apache ThriftでTwitter

Apache Thriftを使ってNode.js用のTwitter APIを作成します。
引用元はこちら

1.Thriftのインストール

今回、環境はMacなので、homebrewを使ってインストールします。
$ brew install thrift
==> Installing thrift dependency: boost
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/boost-1.49.0.lion.bottle.tar.gz
######################################################################## 100.0%
==> Pouring boost-1.49.0.lion.bottle.tar.gz
/usr/local/Cellar/boost/1.49.0: 9053 files, 226M
==> Installing thrift
==> Downloading http://www.apache.org/dyn/closer.cgi?path=thrift/0.8.0/thrift-0.8.0.tar.gz
==> Best Mirror http://ftp.meisei-u.ac.jp/mirror/apache/dist/thrift/0.8.0/thrift-0.8.0.tar.gz
######################################################################## 100.0%
==> ./configure --disable-debug --prefix=/usr/local/Cellar/thrift/0.8.0 --libdir=/usr/local/Cellar/thrift/0.8.0/lib --without-haskell --without-java --without-python --without-ruby --without-pe
==> make
==> make install
==> Caveats
Most language bindings were not installed. You may like to do the
following:

    gem install thrift
    easy_install thrift

If anyone figures out the steps to reliably build a set of bindings, please
open a pull request.
==> Summary
/usr/local/Cellar/thrift/0.8.0: 72 files, 5.3M, built in 3.0 minutes

2.thriftファイル作成

自動生成に必要なthriftファイルを新規作成します。
ファイル名をtweet.thriftとします。
struct Tweet {
    1: string text,             // 本文
    2: optional i32 id,         // ユニークなID
    3: optional i32 created_at  // 投稿日時(UNIXタイム)
}

service TweetService {
    i32 post(1: Tweet tweet),
    Tweet get(1: i32 id)
}

3.定義ファイルからコードを生成

Thriftを使って定義ファイルからコードを生成します。
公式ドキュメントには下記形式でジェネレートするよう書かれています。
thrift --gen <language> <Thrift filename>

言語はNode.jsで、先程のthriftファイルを指定します。
$ thrift --gen js:node tweet.thrift

gen-nodejsディレクトリ配下にTweetService.jsとtweet_type.jsというファイルが生成されました。
$ tree gen-nodejs
gen-nodejs
├── TweetService.js
└── tweet_types.js

0 directories, 2 files

4.Node.js用のThriftモジュールインストール

npmコマンドからインストールします。
$ npm install thrift
npm WARN package.json node@0.0.0 No README.md file found!
npm http GET https://registry.npmjs.org/thrift
npm http 200 https://registry.npmjs.org/thrift
npm http GET https://registry.npmjs.org/thrift/-/thrift-0.7.0.tgz
npm http 200 https://registry.npmjs.org/thrift/-/thrift-0.7.0.tgz
thrift@0.7.0 ../../../node_modules/thrift

5.サーバー実装

server.jsというファイル名にします。
var thrift = require('thrift');

var TweetService = require('./gen-nodejs/TweetService');

// Tweet ID
var id = 0;
// Tweet ストレージ
var tweets = {};

// Thrift サーバーの生成
var server = thrift.createServer(TweetService, {
    // post コールバック
    post: function(tweet, result) {
        // Tweet IDを生成
        tweet.id = ++id;
        // created_at を UNIX タイムから生成
        tweet.created_at = parseInt(new Date() / 1000);
        // Tweet を保存
        tweets[tweet.id] = tweet;

        console.log("[Post]", tweet);
        result(tweet.id);
    },

    // get コールバック
    get: function(id, result) {
        console.log("[Get]", id);
        result(tweets[id]);
    }
});

// Thrift サーバーを起動
server.listen(8888);

6.クライアント実装

client.jsというファイル名にします。
var thrift = require('thrift');

var TweetService = require('./gen-nodejs/TweetService'),
    Tweet        = require('./gen-nodejs/tweet_types').Tweet;

// Thrift サーバーに接続
var connection = thrift.createConnection('localhost', 8888);
connection.on("error", function(err) {
    console.error(err);
});

// クライアントを生成
var client = thrift.createClient(TweetService, connection);

// Tweet を生成
var tweet = new Tweet({ text: "<3 Node.js!" });

// Tweet を投稿
client.post(tweet, function(err, id) {
    // エラー処理
    if(err) {
        return console.error(err);
    }

    // Tweet ID を表示
    console.log("[Post]", id);

    // Tweet を取得
    client.get(id, function(err, posted_tweet) {
        // エラー処理
        if(err) {
            return console.error(err);
        }

        // 投稿済み Tweet を表示
        console.log("[Get]", posted_tweet);
        // 接続を解除
        connection.end();
    });
});

7.実行

①サーバー起動
②クライアント実行
の順に実行します。
$ node server.js > server.log &
[1] 53557
$ node client.js 
{Post} 1
{Get} { text: '<3 Node.js!', id: 1, created_at: 1357202427 }

Tweetが投稿され、Tweet情報が出力されました。

0 件のコメント:

コメントを投稿