0

我最近开始使用 Cassandra 数据库。我能够在我的本地机器上设置单节点集群。

现在我正在考虑开始使用Pelops client.

以下是我迄今为止创建的键空间和列族-

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 的工作原理。

提前致谢。

4

1 回答 1

1

每个 cassandra 列都是一个 Key-Value-Timestamp 三元组。你没有在你的列中设置时间戳

Column c = new Column();
c.setTimestamp(System.currentTimeMillis());

可以使用客户端的方式创建列,让工作更轻松

   mutator.writeColumn(
            COLUMN_FAMILY,
            "Row1",
            mutator.newColumn(" Name ", " Test One "));

通过这种方式,您可以避免设置时间戳(客户端会为您完成)和在 String 上使用 getBytes()。

问候, 卡罗

于 2013-04-09T14:55:53.430 回答