Adding Key and TTL when creating a record using Record UDF

Hi,

I’m new to aerospike UDF need some help on basic UDF function, when creating a new record using record UDF is it possible to give a TTL value for it? also how to add the key value when creating a new record?

This is the sample UDF that I’m using to create record:

function create_record(rec, bin1, bin2, bin3)
  if( not aerospike:exists( rec ) ) then
    aerospike:create(rec);
  end

  rec['bin1'] = bin1;
  rec["bin2"] = bin2;
  rec["bin3"]   = bin3;

  return aerospike:update(rec);
end

All the bin values are getting updated properly, but the “key” isn’t getting updated. Is there a way to update the key when creating or updating the record using UDF, also is it possible to give a TTL value when performing a create/update operation.

Thanks, Roy Justin

Update: I was able to find a way to update the TTL by the following method:

record.set_ttl(rec, ttl);

I need to know how to update the key of a record when creating/ updating using UDF.

You can create a new record with the desired key/data and remove the old one.

@ronny1204

I didn’t really understand your answer, when creating a new record how can we set the key? can you give me a code sample? I have a create statement in my sample code, but I’m not sure how to assign the key for it

aerospike:create(rec);

Justin,

The record is identified by key. When UDF execution is performed with a key. The record that you get access to using rec is the record for that key. You cannot change the key for a record. You can add/update bin values / TTL etc.

HTH

– R

1 Like

@raj,

When I tried to call the above mention UDF (create_record) through AQL command ‘key’ column is not getting updated, as in when I check the set data ‘key’ column is empty. But still I was able to query the record with the primary key using below command:

select * from namespace.setname where PK = 'key'

Also inside the UDF when I tried to retrieve the ‘key’ value it returns nil. But when I call the same UDF function using the Java Client API ‘key’ column is getting updated properly also inside UDF I can retrieve the value of the key, is there any reason for that or am I doing anything wrong?

Justin,

I was assuming by key you mean primary key.

Are you trying

rec['key'] = "key value"

inside UDF and not able to get it ??

– R