Pelops Java Client插入到Cassandra数据库中

时间:2013-04-07 00:18:20

标签: java cassandra pelops

我最近开始使用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的工作原理。

提前致谢。

1 个答案:

答案 0 :(得分: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()。

问候,卡罗