pycassでcassandraに大量データ

やりたいこと:
単純なKey-Valueで、Valueはカウンター
pycassを使ってやる。


普通にinsertしたら、まず、integer型がダメだみたいなエラーがでる。
ググったら、どうやら、カラムのデータ型にカウンター専用っぽい「CounterColumnType」というのがあった。
http://mbothonline.wordpress.com/2012/04/22/counters-in-nosql-cassandra/
を参考に、カウンター用カラムファミリーを作る。

create keyspace SPEED;
use SPEED;
create column family counters with default_validation_class=CounterColumnType and replicate_on_write=true and key_validation_class=UTF8Type and comparator=UTF8Type;

ランダムな文字列をキーにして、1億回カウントアップ

# -*- coding: utf-8 -*-

import pycassa
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
from pycassa.types import *
import random
import string
from datetime import datetime

alphabets = string.digits + string.letters
def randstr(n):
return ''.join(random.choice(alphabets) for i in xrange(n))

pool = pycassa.ConnectionPool('SPEED')
cf = pycassa.ColumnFamily(pool, 'counters')
#cf.column_validators['val'] = CounterColumnType()
#cf.column_validators['val'] = LongType()

t0 = datetime.now()

for var in range(0, 100000000):
#key = 'key' + str(random.randint(0,1000000))
#key = 'key4あ'
key = randstr(4)
cf.add(key,'val')
if var % 10000 == 0:
print datetime.now() - t0
print var
print key
print cf.get(key)

print "********end**********"

keyが存在しているかどうかのチェックは必要無し。
cf.add(key,'val')っていうのが、もしなければ1、あれば+1っていうのを勝手にやってくれる。

速度はというと、2千万件までやったが、ずっと安定してた。
だいたい1万件につき2,3秒くらい。