我最近开始使用Cassandra数据库。我能够在我的本地机器上设置单节点集群。
现在,我正在考虑开始使用Pelops client向Cassandra数据库写入一些示例数据。
下面是我到目前为止创建的键空间和列族-
create keyspace my_keyspace with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy' and strategy_options = {replication_factor:1};
use my_keyspace;
create column family users with column_type = 'Standard' and comparator = 'UTF8Type';下面是我到目前为止拥有的代码。我取得了一些进展,就像以前一样,我得到了一些例外,我能够修复它。现在我得到了另一个异常
public class MyPelops {
private static final Logger log = Logger.getLogger(MyPelops.class);
public static void main(String[] args) throws Exception {
// A comma separated List of Nodes
String NODES = "localhost";
// Thrift Connection Pool
String THRIFT_CONNECTION_POOL = "Test Cluster";
// Keyspace
String KEYSPACE = "my_keyspace";
// Column Family
String COLUMN_FAMILY = "users";
Cluster cluster = new Cluster(NODES, 9160);
Pelops.addPool(THRIFT_CONNECTION_POOL, cluster, KEYSPACE);
Mutator mutator = Pelops.createMutator(THRIFT_CONNECTION_POOL);
log.info("- Write Column -");
mutator.writeColumn(
COLUMN_FAMILY,
"Row1",
new Column().setName(" Name ".getBytes()).setValue(
" Test One ".getBytes()));
mutator.writeColumn(
COLUMN_FAMILY,
"Row1",
new Column().setName(" Work ".getBytes()).setValue(
" Engineer ".getBytes()));
log.info("- Execute -");
mutator.execute(ConsistencyLevel.ONE);
Selector selector = Pelops.createSelector(THRIFT_CONNECTION_POOL);
int columnCount = selector.getColumnCount(COLUMN_FAMILY, "Row1",
ConsistencyLevel.ONE);
log.info("- Column Count = " + columnCount);
List<Column> columnList = selector
.getColumnsFromRow(COLUMN_FAMILY, "Row1",
Selector.newColumnsPredicateAll(true, 10),
ConsistencyLevel.ONE);
log.info("- Size of Column List = " + columnList.size());
for (Column column : columnList) {
log.info("- Column: (" + new String(column.getName()) + ","
+ new String(column.getValue()) + ")");
}
log.info("- All Done. Exit -");
System.exit(0);
}
}每当我运行这个程序时,我都会得到这样的异常-
Exception in thread "main" org.scale7.cassandra.pelops.exceptions.InvalidRequestException: Column timestamp is required当它试图执行这行代码时,这个异常就会出现-
mutator.execute线路
如上所述,我也是Cassandra数据库和Pelops客户端的新手。这是我第一次使用它。有没有人能帮助我逐步解决这个问题?我在本地机器上运行Cassandra 1.2.3。
任何一步一步的指导,比如如何在Cassandra数据库中插入数据,都会对我理解Cassandra的工作原理有很大帮助。
提前谢谢。
发布于 2013-04-09 22:55:53
每个cassandra列都是一个键-值-时间戳三元组。您没有在您的列中设置时间戳
Column c = new Column();
c.setTimestamp(System.currentTimeMillis());您可以使用客户端的方式创建列,从而使工作更轻松
mutator.writeColumn(
COLUMN_FAMILY,
"Row1",
mutator.newColumn(" Name ", " Test One "));通过这种方式,您可以避免设置时间戳(客户端将为您做这件事)和对字符串使用getBytes()。
致敬,卡洛
https://stackoverflow.com/questions/15857465
复制相似问题