1

我有一些关于 Hector 将双精度/浮点数据插入 Cassandra 的问题

new Double("13.45")------->13.468259733915328
new Float("64.13") ------->119.87449

当我通过 hector 将数据插入 Cassandra 时

TestDouble ch = new TestDouble("talend_bj",
        "localhost:9160");
String family = "talend_1";
ch.ensureColumnFamily(family);
List values = new ArrayList();
values.add(HFactory.createColumn("id", 2, StringSerializer.get(),
        IntegerSerializer.get()));
values.add(HFactory.createColumn("name", "zhang",
        StringSerializer.get(), StringSerializer.get()));
values.add(HFactory.createColumn("salary", 13.45,
        StringSerializer.get(), DoubleSerializer.get()));
ch.insertSuper("14", values, "user1", family, StringSerializer.get(),
        StringSerializer.get());
StringSerializer se = StringSerializer.get();
MultigetSuperSliceQuery<String, String, String, String> q = me.prettyprint.hector.api.factory.HFactory
        .createMultigetSuperSliceQuery(ch.getKeyspace(), se, se, se, se);
// q.setSuperColumn("user1").setColumnNames("id","name")
q.setKeys("12", "11","13", "14");
q.setColumnFamily(family);
q.setRange("z", "z", false, 100);
QueryResult<SuperRows<String, String, String, String>> r = q
        .setColumnNames("user1", "user").execute();
Iterator iter = r.get().iterator();
while (iter.hasNext()) {
    SuperRow superRow = (SuperRow) iter.next();
    SuperSlice s = superRow.getSuperSlice();
    List<HSuperColumn> superColumns = s.getSuperColumns();
    for (HSuperColumn superColumn : superColumns) {
        List<HColumn> columns = superColumn.getColumns();
        System.out.println(DoubleSerializer.get().fromBytes(((String) superColumn.getSubColumnByName("salary").getValue()).getBytes()));
    }
}

您可以看到 13.45 但我得到的列值为 13.468259733915328

4

1 回答 1

0

你应该把问题一分为二。编写后,如果您定义了部分架构或在命令行 cli 上使用 te ASSUME 关键字,请查看 cassandra 中的数据以查看是否正确。PlayOrm 有这个 EXACT 单元测试(它是在 astyanax 而不是 hector 之上的 PlayOrm 上),它工作得很好......请注意 -200.23 测试中的比较......

https://github.com/deanhiller/playorm/blob/master/input/javasrc/com/alvazan/test/TestColumnSlice.java

一旦关闭,您在 cassandra 中的数据看起来是否正确?如果是这样,那么问题在于您读取代码中的值,否则,就是写入。

于 2012-09-28T14:20:38.827 回答