Handling Float/Double datatypes using Aerospike Java Client

时间:2015-06-26 10:26:01

标签: aerospike java-client

I came to know that Java Client 3.0.33 supports Float/Double datatype at client side.

The release notes says: Support double/float on client-side. Server will store these values as longs.

This is fine. When I insert a float value to aerospike bin, It is stored as a long integer in aerospike server. Whereas while retreiving the value back using Java Client, it is getting as Long integer as saved in the server.

I expect Java Client should have converted Long to float automatically on retrieval. This is perfectly done in python client (serialize/deserialize). I have to explicitly use getFloat() to convert back to float while using Java Client.

My Question is how do I know for which bin I need to apply getFloat() and for which bin I should not. Because I never know the datatype I am reading was inserted as float.

Thanks in Advance.

2 个答案:

答案 0 :(得分:2)

使用Aerospike Java客户端3.1.2和以下代码。 double / float存储为序列化Java类型,但只要你用Jave编写和读取就可以了。

Key key = new Key("test", "some-floats", "float-001");
Bin bin = new Bin("a-float", 3.14159f);
this.client.put(null, key, bin);
Record record = this.client.get(null, key, "a-float");
double pi = record.getFloat("a-float");
System.out.println("Float:"+pi);

答案 1 :(得分:1)

最新的Aerospike java客户端(3.1.4即将发布)中给出了一种解决方法,以强制客户端始终通过java对象序列化/反序列化。它避免了客户端转换为long。示例代码如下。

double value = 22.7;
Bin bin = new Bin("mybin", (Object) value);