import io.github.spencerpark.ijava.IJava;
import io.github.spencerpark.jupyter.kernel.magic.common.Shell;
IJava.getKernelInstance().getMagics().registerMagics(Shell.class);
%sh asd
%%loadFromPOM
com.aerospike
aerospike-client
5.1.3
import com.aerospike.client.AerospikeClient;
AerospikeClient client = new AerospikeClient("localhost", 3000);
System.out.println("Initialized the client and connected to the cluster.");
// imports
import java.util.ArrayList;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.policy.QueryPolicy;
import com.aerospike.client.exp.Exp;
import com.aerospike.client.exp.Expression;
import com.aerospike.client.exp.ListExp;
import com.aerospike.client.exp.MapExp;
import com.aerospike.client.cdt.ListReturnType;
import com.aerospike.client.cdt.MapReturnType;
import com.aerospike.client.query.RegexFlag;
import com.aerospike.client.query.Statement;
import com.aerospike.client.query.RecordSet;
import com.aerospike.client.Record;
// The examples assume the data from in Model Training notebook.
final String Namespace = "test";
final String Set = "feature-metadata";
// test function
void filterRecords(String namespace, String set, Expression expr) {
Statement stmt = new Statement();
stmt.setNamespace(namespace);
stmt.setSetName(set);
QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
policy.filterExp = expr;
RecordSet rs = client.query(policy, stmt);
while (rs.next()) {
Key key = rs.getKey();
Record record = rs.getRecord();
System.out.format("key=%s bins=%s\n", key.userKey, record.bins);
}
rs.close();
}
// 1. Write the filter expression in Java.
Expression expr = Exp.build(
Exp.gt(
ListExp.getByValueList(ListReturnType.COUNT,
Exp.val(new ArrayList(Arrays.asList("label","f_tag1"))), Exp.listBin("tags")),
Exp.val(0)));
// 2. Test the expression.
System.out.println("Results of filter expression query (with either label or f_tag1 in the tags bin):");
filterRecords(Namespace, Set, expr);
// 3. Obtain the base64 representation of the expression.
System.out.format("Base64: %s\n", expr.getBase64());;
// 4. Transfer the base64 representation for use in Spark Connector.
// You can use the base64 string in the Spark Connector's
// "option.aerospike.pushdown.expressions" option.
// 1. Write the filter expression in Java.
Expression expr = Exp.build(
Exp.eq(MapExp.getByKey(MapReturnType.VALUE,
Exp.Type.STRING, Exp.val("f_attr1"), Exp.mapBin("attrs")),
Exp.val("v1")));
// 2. Test the expression.
System.out.println("Results of filter expression query (with a f_attr1=v1 in attrs bin):");
filterRecords(Namespace, Set, expr);
// 3. Obtain the base64 representation of the expression.
System.out.format("Base64: %s\n", expr.getBase64());;
// 4. Transfer the base64 representation for use in Spark Connector.
// You can use the base64 string in the Spark Connector's
// "option.aerospike.pushdown.expressions" option.
// 1. Write the filter expression in Java.
Expression expr = Exp.build(
Exp.regexCompare("^C.*2$", RegexFlag.ICASE, Exp.stringBin("fid")));
// 2. Test the expression.
System.out.println("Results of filter expression query (with a value starting with a C and ending in a 2 in the fid bin):");
filterRecords(Namespace, Set, expr);
// 3. Obtain the base64 representation of the expression.
System.out.format("Base64: %s\n", expr.getBase64());;
// 4. Transfer the base64 representation for use in Spark Connector.
// You can use the base64 string in the Spark Connector's
// "option.aerospike.pushdown.expressions" option.
// 1. Write the filter expression in Java.
Expression expr = Exp.build(
Exp.or(
Exp.or(
Exp.gt(
ListExp.getByValueList(ListReturnType.COUNT,
Exp.val(new ArrayList(Arrays.asList("label","f_tag1"))), Exp.listBin("tags")),
Exp.val(0)),
Exp.eq(MapExp.getByKey(MapReturnType.VALUE,
Exp.Type.STRING, Exp.val("f_attr1"), Exp.mapBin("attrs")),
Exp.val("v1"))),
Exp.regexCompare("^C.*2$", RegexFlag.ICASE, Exp.stringBin("fid"))));
// 2. Test the expression.
System.out.println("Results of filter expression query (OR'ing all above expressions:");
filterRecords(Namespace, Set, expr);
// 3. Obtain the base64 representation of the expression.
System.out.format("Base64: %s\n", expr.getBase64());;
// 4. Transfer the base64 representation for use in Spark Connector.
// You can use the base64 string in the Spark Connector's
// "option.aerospike.pushdown.expressions" option.
// 1. Write the filter expression in Java.
// 2. Test the expression.
// 3. Obtain the base64 representation of the expression.
// 4. Transfer the base64 representation for use in Spark Connector.
client.close();
System.out.println("Closed server connection.");