阅读cassandra表与astyanax与复合键

时间:2014-04-04 19:24:38

标签: cassandra astyanax

美好的一天

我在我的cassandra 2.0上创建了这个表,我试图用astyanax读取和写入它但不幸的是我对cassandra和java来说真的很新

create table requestformatlist (
    usedbyid text,          // rowkey
    rfid text , //  rfid 
    keystoadd Set<text>,
    keystoremove Set<text>,
primary key (usedbyid, rfid) );

表格内容

gid  | rfid | keystoadd                        | keystoremove
------+------+----------------------------------+----------------------------
1111 | 1111 |             {'AddMe1', 'AddMe2'} | {'RemoveMe1', 'RemoveMe2'}
0003 | 0003 |     {'address', 'name', 'state'} |         {'z1', 'z2', 'z3'}

我已经读过这篇文章,但不幸的是我很困惑,如果我们想要添加一行,为什么我们需要两次写入

http://brianoneill.blogspot.com/2012/09/composite-keys-connecting-dots-between.html http://brianoneill.blogspot.com/2012/10/cql-astyanax-and-compoundcomposite-keys.html

我有astyanaxdao,我可以连接它但不能读写它

我有以下代码

public void testRead() throws Exception {
 RequestFormatDAO dao = new RequestFormatDAO("localhost:9160", "l2");
 log(dao.read("0003"));
}


public ColumnList<RequestFormatListEntry> read(String rowKey) throws ConnectionException {
    OperationResult<ColumnList<RequestFormatListEntry>> result = this.getKeyspace().prepareQuery(COLUMN_FAMILY).getKey(rowKey)
            .execute();
    ColumnList<RequestFormatListEntry> requestFormat = result.getResult();
    LOG.debug("Read list [" + rowKey + "]");
    return requestFormat;
}


public class RequestFormatListEntry {
  @Component(ordinal = 0)
  public String rfid;
  @Component(ordinal = 1)
  public String field1;

  public RequestFormatListEntry(){}
}

我在读取的结果:

Read list [0003]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[null]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[keystoadd]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[keystoadd]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[keystoadd]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[keystoremove]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[keystoremove]
listEntry.rfid=>[0003]
listEntry.keystoremove=>[keystoremove]

1 个答案:

答案 0 :(得分:0)

您的问题是您正在尝试使用Thrift(“低级”API)访问您的CQL(“高级”API)表。

如果你正在使用Thrift,你的闪亮CQL表(你甚至使用CQL3集合)看起来像下面的示例。

[default@test] list requestformatlist;
Using default limit of 100
Using default cell limit of 100
-------------------
RowKey: 1111
=> (name=1111:, value=, timestamp=1396662079753000)
=> (name=1111:keystoadd:4164646d6531, value=, timestamp=1396662079753000)
=> (name=1111:keystoadd:4164646d6532, value=, timestamp=1396662079753000)
=> (name=1111:keystoremove:52656d6f766531, value=, timestamp=1396662079753000)
=> (name=1111:keystoremove:52656d6f766532, value=, timestamp=1396662079753000)
-------------------
RowKey: 0003
=> (name=0003:, value=, timestamp=1396662495503000)
=> (name=0003:keystoadd:61646472657373, value=, timestamp=1396662495503000)
=> (name=0003:keystoadd:6e616d65, value=, timestamp=1396662495503000)
=> (name=0003:keystoadd:7374617465, value=, timestamp=1396662495503000)
=> (name=0003:keystoremove:7a31, value=, timestamp=1396662495503000)
=> (name=0003:keystoremove:7a32, value=, timestamp=1396662495503000)
=> (name=0003:keystoremove:7a33, value=, timestamp=1396662495503000)

2 Rows Returned.
Elapsed time: 26 msec(s).
[default@test]

这解释了你从DAO获得的结果。

我不是Astyanax专家,但你可能应该看看https://github.com/Netflix/astyanax/wiki/Cql-and-cql3并且不要搞砸CQL和Thrift。

进一步阅读的几个链接: http://www.datastax.com/dev/blog/thrift-to-cql3 http://thelastpickle.com/blog/2013/01/11/primary-keys-in-cql.html