import static me.prettyprint.cassandra.utils.StringUtils.bytes;
import static me.prettyprint.cassandra.utils.StringUtils.string;
import org.apache.cassandra.service.Column;
import org.apache.cassandra.service.ColumnPath;
public class ExampleClient {
public static void main(String[] args) throws IllegalStateException,
PoolExhaustedException, Exception {
CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
CassandraClient client = pool.borrowClient("localhost", 9160);
// A load balanced version would look like this:
// CassandraClient client = pool.borrowClient(new String[] {"cas1:9160",
// "cas2:9160", "cas3:9160"});
try {
Keyspace keyspace = client.getKeyspace("Keyspace1");
ColumnPath columnPath = new ColumnPath("Standard1", null,
bytes("网址"));
// insert
keyspace.insert("逖靖寒的世界", columnPath,
bytes("http://gpcuster.cnblogs.com"));
// read
Column col = keyspace.getColumn("逖靖寒的世界", columnPath);
System.out
.println("Read from cassandra: " + string(col.getValue()));
} finally {
// return client to pool. do it in a finally block to make sure it's
// executed
pool.releaseClient(client);
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23937368/viewspace-1050522/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23937368/viewspace-1050522/
本文提供了一个使用Cassandra客户端池进行数据插入与读取的Java示例代码。通过该示例,读者可以了解到如何连接Cassandra数据库、指定键空间、定义列路径以及执行基本的数据操作。
2260

被折叠的 条评论
为什么被折叠?



