How to insert floating point numbers in Aerospike KV store?
Asked Answered
P

3

5

I am Using Aerospike 3.40. Bin with floating point value doesn't appear. I am using python client. Please help.

Proficient answered 19/2, 2015 at 19:42 Comment(4)
could you please explain more. its not clear.Bacteriostasis
Milad, Looks like Aerospike KV store does not support floating point numbers. Can any one explain how to achieve this?Proficient
Dear Dhanasekaran S Thanks for your explanation. If you want answers you need to add more data about your problem. ;)Bacteriostasis
I think this link will help you asking good questions. stackoverflow.com/help/how-to-askBacteriostasis
N
5

The server does not natively support floats. It supports integers, strings, bytes, lists, and maps. Different clients handle the unsupported types in different ways. The PHP client, for example, will serialize the other types such as boolean and float and store them in a bytes field, then deserialize them on reads. The Python client will be doing that starting with the next release (>= 1.0.38).

However, this approach has the limitation of making it difficult for different clients (PHP and Python, for example) to read such serialized data, as it's not serialized using a common format.

One common way to get around this with floats is to turn them into integers. For example, If you have a bin called 'currency' you can multiply the float by 100, chop off the mantissa, and store it as an integer. On the way out you simply divide by 100. A similar method is to store the significant digits in one bin and the mantissa in another, both of them integer types, and recombine them on the read. So 123.456789 gets stored as v_sig and v_mantissa.

(v_sig, v_mantissa) = str(123.456789).split('.')

on read you would combine the two

v = float(v_sig)+float("0."+str(v_mantissa))

FYI, floats are now supported natively as doubles on the aerospike server versions >= 3.6.0. Most clients, such as the Python and PHP one supports casting floats to as_double.

Nelda answered 21/2, 2015 at 20:46 Comment(10)
Ronen, Thanks for your reply. I have tried using struct.pack to convert float to byte array then insert into aerospike bin. But I am unable to retrieve back from aerospike using python client. Core Dump error thrown. Also how do I find on which column I need to deserialize to convert back to float. Can you please explain in detail? Thanks in advance.Proficient
Hey Dhanasekaran, if you run into a bug you can (and should) open a GitHub issue at the repo github.com/aerospike/aerospike-client-python/issues. A 1.0.38 release is pending soon. If the problem persists please do that.Nelda
Ronen, Thanks for your valuable reply. 1.0.38 is out and I have tested which floating point bins are not getting appeared. Can you please help me out? Update:Proficient
Update: Getting huge list of warnings as given blow while upgrading to 1.0.38 using pip. will this be a reason? Was floating point issue handled in this release? ./aerospike-client-c/include/aerospike/as_udf.h:255:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]Proficient
Hey, you can actually open a new issue on the GitHub repo, if you're running into a problem, though warnings aren't a bug per-se. No, floating point is not handled in the release, because it would first change on the server before the clients. That's not planned soon.Nelda
Ronen, Thanks for your reply. As per your Answer you mentioned in the beginning of this post, "The PHP client, for example, will serialize the other types such as boolean and float and store them in a bytes field, then deserialize them on reads. The Python client will be doing that starting with the next release (>= 1.0.38)" I expected the python client 1.0.38 may do this. May I now when this fix will be out? What will be my temporary fix till then? Also can you explain in brief for what is the difficulty in supporting Floats in Aerospike?Proficient
Rosen, any inputs. Expecting your valuable reply.Proficient
Okay, I pushed out a new Python aerospike 1.0.40 release, and unsupported types such as floats will now be handled automatically for you. See the release notes (github.com/aerospike/aerospike-client-python/releases/tag/…) and try it for yourself.Nelda
Ronen, thanks and this is a timely release from you. Thanks to you and aerospike team.Proficient
@RonenBotzer Let me know if we have similar thing for Java client as well.Styria
H
6

It is now supported in Aerospike 3.6 version

Holt answered 10/11, 2015 at 11:25 Comment(0)
N
5

The server does not natively support floats. It supports integers, strings, bytes, lists, and maps. Different clients handle the unsupported types in different ways. The PHP client, for example, will serialize the other types such as boolean and float and store them in a bytes field, then deserialize them on reads. The Python client will be doing that starting with the next release (>= 1.0.38).

However, this approach has the limitation of making it difficult for different clients (PHP and Python, for example) to read such serialized data, as it's not serialized using a common format.

One common way to get around this with floats is to turn them into integers. For example, If you have a bin called 'currency' you can multiply the float by 100, chop off the mantissa, and store it as an integer. On the way out you simply divide by 100. A similar method is to store the significant digits in one bin and the mantissa in another, both of them integer types, and recombine them on the read. So 123.456789 gets stored as v_sig and v_mantissa.

(v_sig, v_mantissa) = str(123.456789).split('.')

on read you would combine the two

v = float(v_sig)+float("0."+str(v_mantissa))

FYI, floats are now supported natively as doubles on the aerospike server versions >= 3.6.0. Most clients, such as the Python and PHP one supports casting floats to as_double.

Nelda answered 21/2, 2015 at 20:46 Comment(10)
Ronen, Thanks for your reply. I have tried using struct.pack to convert float to byte array then insert into aerospike bin. But I am unable to retrieve back from aerospike using python client. Core Dump error thrown. Also how do I find on which column I need to deserialize to convert back to float. Can you please explain in detail? Thanks in advance.Proficient
Hey Dhanasekaran, if you run into a bug you can (and should) open a GitHub issue at the repo github.com/aerospike/aerospike-client-python/issues. A 1.0.38 release is pending soon. If the problem persists please do that.Nelda
Ronen, Thanks for your valuable reply. 1.0.38 is out and I have tested which floating point bins are not getting appeared. Can you please help me out? Update:Proficient
Update: Getting huge list of warnings as given blow while upgrading to 1.0.38 using pip. will this be a reason? Was floating point issue handled in this release? ./aerospike-client-c/include/aerospike/as_udf.h:255:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]Proficient
Hey, you can actually open a new issue on the GitHub repo, if you're running into a problem, though warnings aren't a bug per-se. No, floating point is not handled in the release, because it would first change on the server before the clients. That's not planned soon.Nelda
Ronen, Thanks for your reply. As per your Answer you mentioned in the beginning of this post, "The PHP client, for example, will serialize the other types such as boolean and float and store them in a bytes field, then deserialize them on reads. The Python client will be doing that starting with the next release (>= 1.0.38)" I expected the python client 1.0.38 may do this. May I now when this fix will be out? What will be my temporary fix till then? Also can you explain in brief for what is the difficulty in supporting Floats in Aerospike?Proficient
Rosen, any inputs. Expecting your valuable reply.Proficient
Okay, I pushed out a new Python aerospike 1.0.40 release, and unsupported types such as floats will now be handled automatically for you. See the release notes (github.com/aerospike/aerospike-client-python/releases/tag/…) and try it for yourself.Nelda
Ronen, thanks and this is a timely release from you. Thanks to you and aerospike team.Proficient
@RonenBotzer Let me know if we have similar thing for Java client as well.Styria
S
1

Floating point number can be divided into two parts, before decimal point and after it and storing them in two bins and leveraging them in the application code.

However, creating more number of bins have performance overheads in Aerospike as a new malloc will be used per bin.

If switching from Python to any other language is not the use case, it is better to use a better serialization mechanism and save it in single bin. That would mean only one bin per floating number is used and also will reduce the data size in Aerospike. Lesser amount of data in Aerospike always helps in speed in terms of Network I/O which is the main aim of Caching.

Styria answered 7/5, 2015 at 7:58 Comment(4)
Ankur, thanks for ur reply. this effort is currently taken care by the recent release of Python client by Ronen botzer.Proficient
Sure. Don't think if same thing is planned for Java client as well.Styria
@ Ankur, Yes. Currently I am looking for the same feature in Java Client. Which is not supported at present. Any thoughts how to handle this?Proficient
Either store them in two bins and merge them in your application or use them as is and Aerospike will store them as Blob. My answer above should help your use case.Styria

© 2022 - 2024 — McMap. All rights reserved.