【问题标题】:Can't read integers value in Cassandra无法在 Cassandra 中读取整数值
【发布时间】:2012-05-10 03:14:25
【问题描述】:

我有一个简单的列族“用户”

    System.out.println("  read User");
    long timestamp = System.currentTimeMillis();
    clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"), 
            new Column(ByteBuffer.wrap("email".getBytes())).setValue(ByteBuffer.wrap("mike@gmail.com".getBytes())).setTimestamp(timestamp)
            , ConsistencyLevel.ONE);

    clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"), 
            new Column(ByteBuffer.wrap("totalPosts".getBytes())).setValue(ByteBuffer.allocate(4).putInt(27).array()).setTimestamp(timestamp)
            , ConsistencyLevel.ONE);

    SlicePredicate predicate = new SlicePredicate();
    predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 10));

    ColumnParent parent = new ColumnParent("Users");

    List<ColumnOrSuperColumn> results = clientA.get_slice(ByteBuffer.wrap("mike".getBytes()), 
            parent, 
            predicate, 
            ConsistencyLevel.ONE);

    for (ColumnOrSuperColumn result : results) {
        Column column = result.column;
        System.out.println(new String(column.getName()) + " -> "+ new String(column.getValue())+", "+column.getValue().length);
    }

返回

  read User
email -> mike@gmail.com, 14
totalPosts -> 

因此,thrift 客户端无法读取 totalPosts

使用 cassandra-cli

[default@test] 获取用户['mike']['totalPosts']; =>(列=总帖子,值=0000001b,时间戳=1336493080621) 经过的时间:10 毫秒。

如何使用 Java thrift 客户端检索此整数值?

使用 cassandra 1.1

编辑: 好像是因为这个部分

for (ColumnOrSuperColumn result : results) {
        Column column = result.column;
        System.out.println(new String(column.getName()) + " -> "+Arrays.toString(column.getValue()));
    }

返回

email -> [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109]
totalPosts -> [0, 0, 0, 27]

【问题讨论】:

    标签: cassandra


    【解决方案1】:

    您的列值以字节形式返回,与插入它们的方式相同。所以'email'列的值是[109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109],解释为ascii是mike@gmail.com。以[0, 0, 0, 27](通过 putint 调用)写入 ByteBuffer 的数字 27 以同样的方式出现。

    如果您绝对必须使用原始节俭接口,您可能希望使用ByteBuffer.getInt() 检索您的totalPosts int。但如果可能的话,我建议使用一个库来解决 thrift 接口的丑陋问题,它应该可以解决像这样的值序列化问题。也许看看Hector,或者完全跳过旧界面,直接去CQLCassandra-JDBC

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-17
      • 2016-09-30
      • 2017-02-01
      • 2013-05-22
      • 1970-01-01
      • 2014-06-10
      • 2013-01-14
      • 1970-01-01
      相关资源
      最近更新 更多