Expressions
Jump to the Code block for a combined complete example.
Aerospike Expressions is a strongly typed, functional, domain-specific language designed for manipulating and comparing bins and record metadata. This page explores expression usage within single record commands.
For expression usage within multi-record requests see the Batch Operations and Queries pages.
Setup
The following examples will use the setup and record structure below to illustrate the use of expressions in single record commands.
using Aerospike.Client;
// Define host configurationHost config = new Host("127.0.0.1", 3000);// Establishes a connection to the serverAerospikeClient client = new AerospikeClient(null, config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001Key key = new Key("sandbox", "ufodata", 5001);
The record structure:
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+| PK | occurred | reported | posted | report | location |+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+| 5001 | 20220531 | 20220601 | 20220601 | MAP('{"shape":["circle", "flash", "disc"], "summary":"Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!", "city":"Ann Arbor", "state":"Michigan", "duration":"5 minutes"}') | GeoJSON('{"type":"Point","coordinates":[42.2808,83.743]}') |+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
Filter Expressions
Introduced in Aerospike Database 5.2, Filter Expressions are commonly used to select records that satisfy a boolean expression and work with all single record commands (reads, writes, operations, and record UDFs), batch reads, and queries. Filters are only executed when a record exists, not executing when a record is created or not found.
Read
When using a Filter Expression on a read command, the expression is defined within the read policy.
The following example creates a filter expression that checks the length of the shape
list in the report
map and only returns if there are more than two shapes.
// Create the policyPolicy policy = new Policy();// Build the expressionpolicy.filterExp = Exp.Build( Exp.GT( ListExp.Size( MapExp.GetByKey(MapReturnType.VALUE, Exp.Type.LIST, Exp.Val("shape"), Exp.MapBin("report")) ), Exp.Val(2) ));
// Read the recordRecord record = client.Get(policy, key);
// Do somethingConsole.WriteLine("Record: {0}", record.ToString().Split("bins:")[1]);
// Close the connection to the serverclient.Close();
Write
When using a Filter Expression on a write command, the expression is defined within the write policy.
The following example creates a filter expression that checks if occurred
is later than 20211231
and posted
exists. If the filter returns true, a recent
key with the value true
will be added to the report
map.
// Create the policyWritePolicy writePolicy = new WritePolicy();// Build the expressionwritePolicy.filterExp = Exp.Build( Exp.And( Exp.GT(Exp.IntBin("occurred"), Exp.Val(20211231)), Exp.BinExists("posted") ));
// Update the recordclient.Operate(writePolicy, key, MapOperation.Put(MapPolicy.Default, "report", Value.Get("recent"), Value.Get(true)));
// Close the connection to the serverclient.Close();
Operation Expressions
Introduced in Aerospike Database 5.6, Operation Expressions are bin operations that atomically compute a value from information within the record or provided by the expression. The result is either returned to the client, in a read expression, or written to a specified bin, in a write expression.
A Filter Expression can be defined in the write policy of the operate
method to determine the execution of a Bin Operation using an Operation Expression.
Read
The read expression operation computes an expression, and returns that value. A defined name
in the operation can be used as the synthetic bin name when retrieving results.
The folowing example takes the Read Filter Expression example from above and changes the scenario slightly.
This command computes the length of the shape
list in the report
map and returns the value in a synthetic bin called numShapes
.
// Build the expressionExpression exp = Exp.Build( ListExp.Size( MapExp.GetByKey(MapReturnType.VALUE, Exp.Type.LIST, Exp.Val("shape"), Exp.MapBin("report")) ));
// Read the recordRecord record = client.Operate(null, key, ExpOperation.Read("numShapes", exp, ExpReadFlags.DEFAULT));
// Do somethingConsole.WriteLine("Record: {0}", record.ToString().Split("bins:")[1]);
// Close the connection to the serverclient.Close();
Write
An operation that computes an expression and writes that value to a bin.
The folowing example takes the Write Filter Expression example from above and changes the scenario slightly.
Using the same criteria to compute a recent
value, reported
bin value and posted
bin exists,
this command updates the report
map with the recent
value, true
or false
.
// Build the expressionExpression exp = Exp.Build( MapExp.Put(MapPolicy.Default, Exp.Val("recent"), Exp.And( Exp.GT(Exp.IntBin("occurred"), Exp.Val(20211231)), Exp.BinExists("posted") ), Exp.MapBin("report") ));
// Update the recordclient.Operate(null, key, ExpOperation.Write("report", exp, ExpWriteFlags.DEFAULT));
// Close the connection to the serverclient.Close();
Code block
Expand this section for a single code block to create a Operation Expression
using Aerospike.Client;
// Define host configurationHost config = new Host("127.0.0.1", 3000);// Establishes a connection to the serverAerospikeClient client = new AerospikeClient(null, config);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001Key key = new Key("sandbox", "ufodata", 5001);
// Build the expressionExpression exp = Exp.Build( MapExp.Put(MapPolicy.Default, Exp.Val("recent"), Exp.And( Exp.GT(Exp.IntBin("occurred"), Exp.Val(20211231)), Exp.BinExists("posted") ), Exp.MapBin("report") ));
// Update the recordclient.Operate(null, key, ExpOperation.Write("report", exp, ExpWriteFlags.DEFAULT));
// Close the connection to the serverclient.Close();