image

  • フォト Amazonギフト券
    ※この時計の時刻は、閲覧しているパソコンのものであり、必ずしも正確な時間とは限りません

検索

最近のトラックバック

無料ブログはココログ

« ピックアップ:FreeMind0.9.0RC1が出てた, ITエンジニアにコンピュータ・サイエンスは必須, etc... | トップページ | 郵便番号を tokyo-tyrant にいれてみた »

2008-12-30

memcachd を試す

郵便番号を memcached にいれてみた。

郵便番号データをそのまま memcached にいれ、ruby と java で 取り出してみた。
次の順でコードを示そう。
1. 郵便番号の格納 (ruby で)
2. ruby, jruby で 値をとりだす。
3. java で値をとりだす。

1. 郵便番号の格納 (ruby で)
=====================
  - http://www.tuyudaku.net/sqlite/import.html
  > SQLite - CSVファイルのインポート
を参考にして utf8 にした csv ファイルを取り出す。
そのあと、 ruby で行単位に memcached に登録する。

$ cat mem-zip.rb
See http://firewing.wordpress.com/2007/04/20/%E3%83%A1%E3%83%A2-memcached-ruby/

$KCODE = 'utf8'

require 'rubygems'
require 'fastercsv'
require 'memcache'

cache = MemCache::new 'localhost:11211'

CSV_FILE = 'zipcode.csv'
FasterCSV.foreach(CSV_FILE) do |row|
  cache[row[2]] = row.to_s
end

別端末で、$ memcached として、サーバーを走らせる。
そのあと、ruby mem-zip.rb とする。

2. ruby, jruby で 値をとりだす。
======================
$ cat query-zip.rb

$KCODE = 'utf8'

require 'rubygems'
require 'memcache'
require 'pp'

cache = MemCache::new 'localhost:11211'

pp cache.stats()

p cache[ARGV[0]]

ruby と jruby のどちらでも動作する。

$ time ruby query-zip.rb  1000000
{"localhost:11211"=>
  {"get_hits"=>14,
   "bytes"=>21574359,
   "rusage_system"=>2.791334,
   "pid"=>36202,
   "connection_structures"=>7,
   "threads"=>4,
   "limit_maxbytes"=>67108864,
   "evictions"=>0,
   "pointer_size"=>32,
   "time"=>1230610177,
   "version"=>"1.2.6",
   "bytes_written"=>989977,
   "total_items"=>122581,
   "cmd_get"=>15,
   "total_connections"=>24,
   "curr_connections"=>6,
   "uptime"=>4865,
   "cmd_set"=>122581,
   "rusage_user"=>1.36321,
   "bytes_read"=>18076476,
   "get_misses"=>1,
   "curr_items"=>118618}}
"13101100  1000000トウキョウトチヨダクイカニケイサイガナイバアイ東京都千代田区以下に掲載がない場合000000"

real    0m0.752s
user    0m0.176s
sys    0m0.052s

$ time jruby query-zip.rb  1000000
{"localhost:11211"=>
  {"pid"=>36202,
   "uptime"=>4900,
   "time"=>1230610212,
   "version"=>"1.2.6",
   "pointer_size"=>32,
   "rusage_user"=>1.363866,
   "rusage_system"=>2.792644,
   "curr_items"=>118618,
   "total_items"=>122581,
   "bytes"=>21574359,
   "curr_connections"=>6,
   "total_connections"=>25,
   "connection_structures"=>7,
   "cmd_get"=>16,
   "cmd_set"=>122581,
   "get_hits"=>15,
   "get_misses"=>1,
   "evictions"=>0,
   "bytes_read"=>18076496,
   "bytes_written"=>990665,
   "limit_maxbytes"=>67108864,
   "threads"=>4}}
"13101100  1000000トウキョウトチヨダクイカニケイサイガナイバアイ東京都千代田区以下に掲載がない場合000000"

real    0m4.293s
user    0m2.031s
sys    0m0.231s

3. java で値をとりだす。
=================

$ cat JQuery.java

// See http://www.nilab.info/zurazure2/000455.html
// See http://d.hatena.ne.jp/bubbles/20081029/1225291896

import java.util.*;
import com.danga.MemCached.*;

public class JQuery {

    public static void main(String[] args){

    // SockIOPool を初期化
    String[] serverlist = { "localhost:11211" };
    SockIOPool pool = SockIOPool.getInstance();
    pool.setServers(serverlist);
    pool.initialize();

    // memcached からオブジェクトを取り出す
    MemCachedClient mc = new MemCachedClient();
    mc.setPrimitiveAsString( true );
    mc.setSanitizeKeys( false );

    System.out.println(mc);
    String value  = (String)mc.get(key);
    System.out.println(key);
    System.out.println(value);
    }

}

$ cat run.sh
#! /bin/sh

# javac -cp java_memcached-release_2.0.1.jar JQuery.java
java -cp .:java_memcached-release_2.0.1.jar JQuery $*

$ ruby mem-zip.rb
ntsitm384184:memcached youichikato$ time ./run.sh 1000000
com.danga.MemCached.MemCachedClient Tue Dec 30 13:28:03 JST 2008 - ++++ retrieving object and stuffing into a string.
1000000
"�13101100  1000000トウキョウトチヨダクイカニケイサイガナイバアイ東京都千代田区以下に掲載がない場合000000

real    0m0.207s
user    0m0.142s
sys    0m0.054s

value 表示で先頭がすこし文字化けしている。
これは、ruby でmemcashed にいれるときに
   # cache[row[2]] = row.to_s
  cache.set(row[2], row.to_s, 0, true)
として、マーシャリングの制御をしてやると解消する。

以上の実験では、memcached にいれるデータ粒度、構造などはあまり適切でないが、
12 万件というデータ容量の処理は問題なくこなせていることがわかった。

« ピックアップ:FreeMind0.9.0RC1が出てた, ITエンジニアにコンピュータ・サイエンスは必須, etc... | トップページ | 郵便番号を tokyo-tyrant にいれてみた »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: memcachd を試す:

« ピックアップ:FreeMind0.9.0RC1が出てた, ITエンジニアにコンピュータ・サイエンスは必須, etc... | トップページ | 郵便番号を tokyo-tyrant にいれてみた »

mokuji

2013年12月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

google

  • twitter
  • __
  • _
    Googleボットチェッカー

合わせて読む

  • 合わせて読む
    フィードメーター - katoy: cocolog あわせて読みたい

リンク