Created
October 11, 2010 04:53
-
-
Save nobusue/619988 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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