Hi, I am using UDFs and I have written a simple UDF to store value in a set.
For example: value passed in input is 33, and I am just storing it in a set. Following is the code.
function write_aggregate(data, objectValue)
if aerospike:exists(data) then
debug("Data for record exists")
else
debug("Data for record do not exist")
data.value = objectValue
aerospike:create(data)
end
end
When I am trying to fetch the value of this key via Java client then I get the type of the key “value” depending upon the input I have passed.
Example: If input passed in UDF function was 33 or 33.0, then the type is long
If input passed in UDF function was 33.20, then type is double
I want to have a defined type for my key. How can I achieve it?
Thanks for the help in advance
Hi. I’m not sure why you’re using a UDF to do a simple put() operation. That should be a direct key-value operation from the client, rather than the client calling a UDF. Don’t use UDFs unless you absolutely have to. It’s much better for performance and scale to use the rich operations you get with Aerospike over the native data types.
But mainly Aerospike is schema free. You should be enforcing data types in your application. Your Java client should be mapping an object’s attributes a specific way, and your Lua, if you really must use it, should use matching types.
Hi Ronen, Thank you for the reply.
I should have mentioned but I am not using UDF for a simple put( ) operation. I just said the same to explain the concern. I apply aggregation logic to my record and then store it in set.
In java client, as the value type for list of bins is different w.r.t. the input passed to UDF as I have mentioned in the example, should I handle such cases explicitly?
For example: if the value type of bin is Long, should I explicitly convert it to the data type I require?
Ah, I get it. Yes, cast values from ambiguous types to the specific one you need in your Java object.
1 Like