【问题标题】:Inserting Data into Cassandra using hector使用 hector 将数据插入 Cassandra
【发布时间】:2013-08-27 09:06:48
【问题描述】:

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

【问题讨论】:

  • 而是检查插入文件夹中的数据,检查从 cassandra cli 或使用 CQL 插入的数据,以获取您的 UUID。

标签: cassandra hector


【解决方案1】:

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

get data where birth_year = 1990;

或者使用cqlsh并执行命令

select * from data where birth_year = '1990';

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

你可以在 cassandra 的提交日志中看到你的数据,在“var\lib\cassandra\commitlog”目录中......

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

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 2012-07-05
    • 2012-08-04
    • 2012-04-25
    • 2012-03-12
    • 1970-01-01
    • 2012-04-25
    • 2013-05-27
    • 2017-05-06
    相关资源
    最近更新 更多