插入和从组合键Cassandra获取:InvalidRequestException(为什么:没有足够的字节读取组件0的值) [英] inserting and getting from Cassandra with Composite Key: InvalidRequestException(why:Not enough bytes to read value of component 0)

查看:332
本文介绍了插入和从组合键Cassandra获取:InvalidRequestException(为什么:没有足够的字节读取组件0的值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中使用thrift尝试插入到具有复合键的列系列中。我得到以下异常:

I'm trying to insert into a Column Family with a composite Key using thrift in Java. I get the following exception:

 InvalidRequestException(why:Not enough bytes to read value of component 0)

这里是我如何使用CQLSH创建CF。我想在test中插入(1,2,aaa)。

here is how I created CF using CQLSH. I want to insert ("1","2","aaa") into "test".

CREATE COLUMNFAMILY test(id1 varchar,id2 varchar,value varchar, PRIMARY KEY(id1,id2));

CREATE COLUMNFAMILY test (id1 varchar, id2 varchar, value varchar,PRIMARY KEY (id1,id2));

这是我的源代码,任何人都知道这里有什么问题以及如何使它工作?

Here is my source code, anybody has any idea what's wrong here and how to make it work?

public static void main(String[] args) throws Exception {       

    TSocket socket = new TSocket("10.10.8.200", 9160);
    TFramedTransport transport = new TFramedTransport(socket);

    Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));     
    transport.open();
    client.set_cql_version("3.0.0");
    client.set_keyspace("bigdata");

    ColumnParent parent = new ColumnParent("test");

    List<AbstractType<?>> keyTypes = new ArrayList<AbstractType<?>>(); 
    keyTypes.add(UTF8Type.instance);
    keyTypes.add(UTF8Type.instance);
    CompositeType compositeKey = CompositeType.getInstance(keyTypes);

    Builder builder = new Builder(compositeKey);
    builder.add(ByteBuffer.wrap("1".getBytes()));
    builder.add(ByteBuffer.wrap("2".getBytes()));
    ByteBuffer rowid = builder.build();

    Column column = new Column();
    column.setName("value".getBytes());
    column.setValue("aaa".getBytes());
    column.setTimestamp(System.currentTimeMillis());

    client.insert(rowid, parent, column, ConsistencyLevel.ONE);

}


推荐答案

最后,我想出了如何使用thrift API将一个复合键插入到ColumnFamily中。我不得不说,这没有任何意义。但它的工作原理:

OK. Finally, I figured out how to insert into a ColumnFamily with a composite Key using thrift API. I have to say, this doesn't make any sense. But it works:

    ColumnParent parent = new ColumnParent("test");

    List<AbstractType<?>> keyTypes = new ArrayList<AbstractType<?>>(); 
    keyTypes.add(UTF8Type.instance);
    keyTypes.add(UTF8Type.instance);
    CompositeType compositeKey = CompositeType.getInstance(keyTypes);

    Builder builder = new Builder(compositeKey);
    builder.add(ByteBuffer.wrap("2".getBytes()));
    builder.add(ByteBuffer.wrap("value".getBytes()));
    ByteBuffer columnName = builder.build();


    Column column = new Column();
    column.setName(columnName);
    column.setValue("3".getBytes());
    column.setTimestamp(System.currentTimeMillis());

    client.insert(ByteBuffer.wrap("1".getBytes()), parent, column, ConsistencyLevel.ONE);

这篇关于插入和从组合键Cassandra获取:InvalidRequestException(为什么:没有足够的字节读取组件0的值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆