The Client
class provides methods which can be used to perform operations on an Aerospike
database cluster. In order to get an instance of the Client class, you need to initialize it:
client = Aerospike::Client.new("127.0.0.1", 3000)
To customize a Client behaviour:
client = Aerospike::Client.new("127.0.0.1", 3000, :connection_queue_size => 64, :timeout => 0.005)
Notice: Examples in the section are only intended to illuminate simple use cases without too much distraction. Always follow good coding practices in production.
With a new client, you can use any of the methods specified below:
Using the provided key, adds values to the mentioned bins.
Bin value types should by of type integer
for the command to have any effect.
Parameters:
key
– A Key object, used to locate the record in the cluster.bins
– A hash used for specifying the fields and their corresponding values.options
– A hash representing Write Policy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
bins = {
"e" => 2,
"pi" => 3,
}
client.add(nil, key, bins)
Using the provided key, appends provided values to the mentioned bins.
Bin value types should by of type String
for the command to have any effect.
Parameters:
key
– A Key object, used to locate the record in the cluster.bins
– A hash used for specifying the fields and their corresponding values.options
– A hash representing Write Policy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
bins = {
"story" => ", and lived happily ever after...",
}
client.append(key, bins)
Closes the client connection to the cluster.
Example:
client.close
Removes a record with the specified key from the database cluster.
Parameters:
key
– A Key object used for locating the record to be removed.options
– A hash representing Write Policy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
returned values:
existed
– Boolean value that indicates if the Key existed.
Example:
key = Key.new("test", "demo", 123)
if client.delete(key, :ttl => 0.005)
# do something
end
Using the key provided, checks for the existence of a record in the database cluster .
Parameters:
key
– A Key object, used to locate the record in the cluster.options
– A hash representing Policy Attributes to use for this operation. If not provided,@default_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
if client.exists(key)
# do something
end
Using the keys provided, checks for the existence of records in the database cluster in one request.
Parameters:
keys
– A Key array, used to locate the records in the cluster.options
– A hash representing Policy Attributes to use for this operation. If not provided,@default_policy
will be used.
Example:
key1 = Key.new("test", "demo", 123)
key2 = Key.new("test", "demo", 42)
existance_array = client.batch_exists([key1, key2])
# do something
end
Using the key provided, reads a record from the database cluster .
Parameters:
key
– A Key object, used to locate the record in the cluster.bin_names
– (optional) Bins to retrieve. Will retrieve all bins if not provided.options
– A hash representing Policy Attributes to use for this operation. If not provided,@default_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
rec = client.get(key) # reads all the bins
Using the key provided, reads record metadata ONLY from the database cluster. Record metadata includes record generation and Expiration (TTL from the moment of retrieval, in seconds)
record.bins
will always be empty in resulting record
.
Parameters:
key
– A Key object, used to locate the record in the cluster.options
– A hash representing Policy Attributes to use for this operation. If not provided,@default_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
rec = client.get_header(key) # No bins will be retrieved
Using the keys provided, reads all relevant records from the database cluster in a single request.
Parameters:
keys
– A Key array, used to locate the record in the cluster.bin_names
– (optional) Bins to retrieve. Will retrieve all bins if not provided.options
– A hash representing Policy Attributes to use for this operation. If not provided,@default_policy
will be used.
Example:
key1 = Key.new("test", "demo", 123)
key2 = Key.new("test", "demo", 42)
recs = client.batch_get([key1, key2]) # reads all the bins
Using the keys provided, reads all relevant record metadata from the database cluster in a single request.
record.bins
will always be empty in resulting record
.
Parameters:
keys
– A Key array, used to locate the record in the cluster.options
– A hash representing Policy Attributes to use for this operation. If not provided,@default_policy
will be used.
Example:
key1 = Key.new("test", "demo", 123)
key2 = Key.new("test", "demo", 42)
recs = client.batch_get_header([key1, key2]) # reads all the bins
Checks if the client is connected to the cluster.
Using the provided key, prepends provided values to the mentioned bins.
Bin value types should by of string
for the command to have any effect.
Parameters:
key
– A Key object, used to locate the record in the cluster.bins
– A hash used for specifying the fields and value.options
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
bins = {
"story" => "Long ago, in a galaxy far far away, ",
}
client.prepend(key, bins)
Writes a record to the database cluster. If the record exists, it modifies the record with bins provided.
To remove a bin, set its value to nil
.
Parameters:
key
– A Key object, used to locate the record in the cluster.bins
– A hash used for specifying the fields to store.options
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
bins = {
"a" => "Lack of skill dictates economy of style.",
"b" => 123,
"c" => []int{1, 2, 3},
"d" => map[string]interface{}{"a" => 42, "b" => "An elephant is mouse with an operating system."},
}
client.put(key, bins, :ttl => 0.05) # ttl is set to 50ms
Create record if it does not already exist. If the record exists, the record's time to expiration will be reset to the policy's expiration.
Parameters:
key
– A Key object, used to locate the record in the cluster.options
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
key = Key.new("test", "demo", 123)
client.touch(key, :ttl => 15) # 15 seconds
Creates a secondary index. create_index
will return an IndexTask
object which can be used to determine if the operation is completed asynchronously.
Parameters:
namespace
– Namespaceset_name
– Name of the Setindex_name
– Name of indexbin_name
– Bin name to create the index onindex_type
–:string
or:numeric
options
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
idx_task = client.create_index(nil, "test", "demo", "indexName", "binName", :numeric)
idx_task.wait_till_completed
Drops an index.
Parameters:
namespace
– Namespaceset_name
– Name of the Set.index_name
– Name of indexoptions
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
client.drop_index("test", "demo", "indexName")
Registers the given UDF on the server.
Parameters:
udf_body
– UDF source codeserver_path
– Path on which the UDF should be put on the server-sidelanguage
– Only 'LUA' is currently supportedoptions
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
udf_body = "function testFunc1(rec)
local ret = map() -- Initialize the return value (a map)
local x = rec['bin1'] -- Get the value from record bin named "bin1"
rec['bin2'] = (x / 2) -- Set the value in record bin named "bin2"
aerospike:update(rec) -- Update the main record
ret['status'] = 'OK' -- Populate the return status
return ret -- Return the Return value and/or status
end"
reg_task = client.register_udf(udf_body, "udf1.lua", LUA)
# wait until UDF is created
reg_task.wait_till_completed
Read the UDF source code from a file and registers it on the server.
Parameters:
clientPath
– full file path for UDF source codeserver_path
– Path on which the UDF should be put on the server-sidelanguage
– Only 'LUA' is currently supportedoptions
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
regTask = client.register_udf_from_file(nil, "~/path/udf.lua", "udf1.lua", LUA)
# wait until UDF is created
reg_task.wait_till_completed
Executes a UDF on a record with the given key, and returns the results.
Parameters:
key
– A Key object, used to locate the record in the cluster.package_name
– server path to the UDFfunction_name
– UDF nameargs
– (optional) UDF argumentsoptions
– A hash representing WritePolicy Attributes to use for this operation. If not provided,@default_write_policy
will be used.
Example:
Considering the UDF registered in register_udf
example above:
res = client.execute_udf(key, "udf1", "testFunc1")
# res will be: {"status" => "OK"}