2012-08-03 11 views
3

我試圖用Java中的節儉組合鍵插入列族。我收到以下異常:用複合鍵插入和從Cassandra中獲取:InvalidRequestException(爲什麼:沒有足夠的字節來讀取組件0的值)

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

這裏是我如何使用CQLSH創建CF。我想在「測試」中插入(「1」,「2」,「aaa」)。

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

這裏是我的源代碼,任何人有任何想法這裏有什麼問題,以及如何使它工作?

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); 

} 

回答

0

好的。在這裏出去。如果您希望主鍵由id1和id2按照該順序組成,那麼當您插入一行時,您將爲值添加一個列,並且行鍵和列名將從id1和id2的值派生。

行鍵將是id1的值。

值的列名將是由id1(本例中爲「1」)和id2列(本例中爲「id2」)的值組成的複合值。

至少這就是如果您不直接使用CQL,您必須在Hector中執行的操作。

+0

感謝您的回答。我試過了,我快到了。除了不能賦值給列「值」。我發佈了我的新代碼。謝謝你的幫助。 – Ashkan 2012-08-07 13:55:02

+0

你得到一個錯誤(運行時或編譯器),如果是,哪一個? – 2012-08-07 14:02:11

0

好的。最後,我想出瞭如何使用thrift API將組合鍵插入到ColumnFamily中。我不得不說,這沒有任何意義。但它的工作原理:

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); 
相關問題