[intlink id=”2999″ type=”post” /]の続きです。
TokyoTyrantのRDBTableにJavaからアクセスする方法を調べたのでメモ。
TokyoTyrantのJavaバインディングはtokyotyrant-javaが有名なようですが、公開中のビルド済みバージョン(0.10)ではRDBTableに対応していません。
しかし、bitbucketの最新ソースはRDBTableに対応完了しています。
当然ステーブルリリースではないので、いざとなったらソース読む覚悟が必要です。
こんな感じのソースを書いたところ、
@Test
public void rdbtable() throws IOException {
Object value;
// create the object
RDB db = new RDB();
// connect to the server
db.open(new InetSocketAddress(InetAddress.getByAddress(new byte[] { (byte) 127, (byte) 0, (byte) 0, (byte) 1 }), 1978));
final RDBTable tTable = new RDBTable(db);
final Map tCols = new HashMap();
tCols.put("url1", "http://all.url1/");
tCols.put("title1", "たいとる all.title1");
tCols.put("url2", "http://all.url2/");
tCols.put("title2", "たいとる⑪ all.title2");
tCols.put("url3", "http://all.url3/");
tCols.put("title3", "recommend all.title3");
tTable.put("all", tCols);
// retrieve records
value = tTable.get("all");
if (value != null) {
System.out.println(value);
} else {
System.err.println("get error");
}
db.close();
}
以下の結果になりました。
{url3=http://all.url3/, url1=http://all.url1/, title1=たいとる all.title1, url2=http://all.url2/, title3=recommend all.title3, title2=たいとる⑪ all.title2}
日本語もバッチリですね。内部的にはUTF-8で処理するようです。
また、以下のコードでRubyバインディングから読み出してみたところ、
require 'tokyotyrant'
include TokyoTyrant
rdb = RDBTBL::new
rdb.open("localhost", 1978)
p rdb.get("all")
rdb.close
ちゃんと読めました。
{"url1"=>"http://all.url1/", "url2"=>"http://all.url2/", "title1"=>"\343\201\237\343\201\204\343\201\250\343\202\213 all.title1", "title2"=>"\343\201\237\343\201\204\343\201\250\343\202\213\342\221\252 all.title2", "url3"=>"http://all.url3/", "title3"=>"recommend all.title3"}
あと、TokyoTyrantを起動するときにRDBTableモードで起動する必要があるので注意。
具体的には拡張子を”.tct”にしないといけない。
$ ttserver "datafile.tct#bnum=1000000" &
おしまい。
0件のコメント