Pelops Java客户端插入Cassandra数据库
我最近开始使用卡桑德拉数据库。我能够在本地计算机中设置单节点群集 现在我正在考虑开始使用Pelops Java客户端插入Cassandra数据库,java,cassandra,pelops,Java,Cassandra,Pelops,我最近开始使用卡桑德拉数据库。我能够在本地计算机中设置单节点群集 现在我正在考虑开始使用Pelops客户端向Cassandra数据库写入一些示例数据 下面是我迄今为止创建的键空间和列族- create keyspace my_keyspace with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy' and strategy_options = {replication_factor:1}; use my_ke
Pelops客户端
向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);
}
}
当它试图执行这一行时,这个异常就会出现-
mutator.execute
line
正如我前面提到的,我对Cassandra数据库和Pelops客户端也是新手。这是我第一次使用它。有人能帮我解决这个问题吗?我正在本地运行Cassandra 1.2.3
任何一步一步的指导,比如如何在Cassandra数据库中插入数据,都将有助于我理解Cassandra的工作原理
提前感谢。每个cassandra列都是一个键值时间戳三元组。 您没有在列中设置时间戳
Column c = new Column();
c.setTimestamp(System.currentTimeMillis());
您可以使用客户机方式创建列,使工作更轻松
mutator.writeColumn(
COLUMN_FAMILY,
"Row1",
mutator.newColumn(" Name ", " Test One "));
通过这种方式,您可以避免设置时间戳(客户机将为您执行)和对字符串使用getBytes()
你好,卡洛
mutator.writeColumn(
COLUMN_FAMILY,
"Row1",
mutator.newColumn(" Name ", " Test One "));