为什么我通过hector将double / float列插入Cassandra并获得不正确的值int database

时间:2012-09-28 09:59:48

标签: java floating-point cassandra hector

我有一些关于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

1 个答案:

答案 0 :(得分:0)

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

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

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