使用hector将数据插入Cassandra

时间:2013-08-24 10:38:52

标签: cassandra hector

我正在尝试使用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}
];

1 个答案:

答案 0 :(得分: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”),您就会找到它。