当前位置: 首页 > 知识库问答 >
问题:

使用hector在Cassandra中插入数据

东郭赞
2023-03-14

我正在尝试使用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文件夹中找不到任何插入的数据。数据插入似乎不能正常工作。代码有什么问题。下面显示了我用来创建'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个答案

仲鸿风
2023-03-14

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

get data where birth_year = 1990;

或者使用cqlsh并执行命令

select * from data where birth_year = '1990';

要查看您的数据是否已插入到列系列中...

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

 类似资料:
  • 问题内容: 这是我面临的场景的一个示例。说我有这个专栏家庭: 这是一些使用Hector的示例Java代码,用于说明如何将一些数据插入此列系列: 这有效,如果我转到cassandra-cli并列出列表,则会得到以下信息: 我现在的问题是:如何在赫克托尔中查询该数据?基本上,我需要以几种方式查询它: 给我整行,其中行键=“ rowkey1” 给我列数据,其中列名的第一部分=一些整数值 给我所有列名称的

  • 我正在尝试使用Pig将HDFS中的文件中的数据复制到Cassandra中的表中。但在将数据存储在Cassandra中时,作业失败,出现空指针异常。有人能帮我吗? 用户表结构: 创建表用户(user\u id text主键、age int、第一个文本、最后一个文本) 我的猪脚本 > A=加载“/用户/hduser/用户。txt“使用PigStorage(',')作为(id:chararray,age

  • 我是卡桑德拉的新人。我必须使用c#在卡桑德拉中一次性插入50000行。我正在使用卡桑德拉c#驱动程序。我正在使用以下代码在卡桑德拉中插入数据。请帮帮我 我收到错误:批处理中的语句无效:只允许UPDATE、INSERT和DELETE语句。

  • 命令用于将数据插入到表的列中。 语法: 示例: 在之前的文章中,我们创建一个名为“”的表,其中包含列(, , ),需要在表中插入一些数据。 我们来看看向“”表中插入数据的代码 - 在执行上面语句插入数据后,可以使用SELECT命令验证是否成功插入了数据。 执行结果如下所示 - 如下图所示 -

  • 假设我连接到一个集群 以后有没有办法检查我是否还在连接上?通过查看他们的Javadocs似乎没有明显的方法来实现这一点。

  • 我们正在尝试使用DataStax驱动程序将CSV文件中的数据插入Cassandra。有哪些方法可以做到这一点? 我们目前使用运行cqlsh从CSV文件加载。