1

我正在尝试使用 hector api 将数据插入到 cassandra 数据库中。我使用的代码如下所示。

<i>
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

import java.util.UUID;

public class HectorAPI {
    private static final String USER="data";
    public static void main(String[] args) throws Exception{
        StringSerializer ss = StringSerializer.get();
        Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "127.0.0.1:9160");
        Keyspace keyspace = HFactory.createKeyspace("myKeySpace", cluster);
        String id = UUID.randomUUID().toString();

        Mutator<String> mutator = HFactory.createMutator(keyspace, ss);
        mutator.addInsertion(id, USER, HFactory.createStringColumn("full_name", "John"))
                .addInsertion(id, USER, HFactory.createStringColumn("email", "test"))
                .addInsertion(id, USER, HFactory.createStringColumn("state","yfcy"))
                .addInsertion(id, USER, HFactory.createStringColumn("gender", "male"))
                .addInsertion(id, USER, HFactory.createStringColumn("birth_year", "1990"));
        mutator.execute();

    }

}

但是在给定 KeySpace 下的 /var/lib/cassandra/data 文件夹中找不到任何插入的数据。数据插入似乎无法正常工作。代码可能有什么问题。我用来创建“数据”列族的命令如下所示。

CREATE COLUMN FAMILY data
WITH comparator = UTF8Type
AND key_validation_class=UTF8Type
AND column_metadata = [
{column_name: full_name, validation_class: UTF8Type}
{column_name: email, validation_class: UTF8Type}
{column_name: state, validation_class:  UTF8Type, index_type: KEYS}
{column_name: gender, validation_class: UTF8Type}
{column_name: birth_year, validation_class: UTF8Type, index_type: KEYS}
];

4

1 回答 1

0

我在我的机器上执行了你的代码,它运行良好;使用 cassandra-cli 工具并执行命令

get data where birth_year = 1990;

或使用 cqlsh 并执行命令

select * from data where birth_year = '1990';

查看您的数据已插入列族...

你可以在 cassandra 的 commitlog 中看到你的数据,在 " var\lib\cassandra\commitlog" 目录中......

搜索您的数据(例如关键字“ full_name”),您会找到它。

于 2013-08-27T06:37:18.250 回答