Skip to content

Instantly share code, notes, and snippets.

@nobusue
Created October 11, 2010 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobusue/619988 to your computer and use it in GitHub Desktop.
Save nobusue/619988 to your computer and use it in GitHub Desktop.
// g100pon #51 KVSの操作(保存、取得)
// Apache Cassandraを利用
// Cassandraのセットアップはこちらを参照⇒http://gihyo.jp/dev/serial/01/cassandra/0002
// セットアップ後にJARをクラスパス(~/.groovy/libなど)に追加してください。
// サンプルコードとしてはgihyo.jpの大谷さんの記事ほぼそんままです。
import org.apache.cassandra.thrift.*
import org.apache.thrift.*
import org.apache.thrift.protocol.*
import org.apache.thrift.transport.*
final String KEYSPACE = "Keyspace1"
final String COLUMN_FAMILY = "Standard1"
final String key = "sample"
def transport = new TSocket("localhost", 9160)
def protocol = new TBinaryProtocol(transport)
transport.open()
Cassandra.Client client = new Cassandra.Client(protocol)
def columnPath = new ColumnPath(COLUMN_FAMILY)
columnPath.setColumn('hoge'.getBytes())
// 1件カラムをインサート
client.insert(KEYSPACE, key, columnPath, 'sample_value'.getBytes(),
System.currentTimeMillis(), ConsistencyLevel.ONE)
transport.flush()
println("1件インサート完了.")
ColumnPath path = new ColumnPath(COLUMN_FAMILY)
path.setColumn('hoge'.getBytes()) //検索するカラム名を指定
def csc = client.get(KEYSPACE, key, path, ConsistencyLevel.ONE)
def c = csc.getColumn()
def name = new String(c.getName())
def value = new String(c.getValue())
println("${name} -> ${value} (${c.getTimestamp()})")
transport.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment