use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class QueryExp method runQuery1.
private void runQuery1(AerospikeClient client, Parameters params, String binName) throws Exception {
int begin = 10;
int end = 40;
console.info("Query Predicate: (bin2 > 126 && bin2 <= 140) || (bin2 = 360)");
Statement stmt = new Statement();
stmt.setNamespace(params.namespace);
stmt.setSetName(params.set);
// Filter applied on query itself. Filter can only reference an indexed bin.
stmt.setFilter(Filter.range(binName, begin, end));
// Predicates are applied on query results on server side.
// Predicates can reference any bin.
QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
policy.filterExp = Exp.build(Exp.or(Exp.and(Exp.gt(Exp.intBin("bin2"), Exp.val(126)), Exp.le(Exp.intBin("bin2"), Exp.val(140))), Exp.eq(Exp.intBin("bin2"), Exp.val(360))));
RecordSet rs = client.query(policy, stmt);
try {
while (rs.next()) {
Record record = rs.getRecord();
console.info("Record: " + record.toString());
}
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class QueryAverage method runQuery.
private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
console.info("Query for: ns=%s set=%s index=%s bin=%s", params.namespace, params.set, indexName, binName);
Statement stmt = new Statement();
stmt.setNamespace(params.namespace);
stmt.setSetName(params.set);
stmt.setFilter(Filter.range(binName, 0, 1000));
stmt.setAggregateFunction("average_example", "average");
ResultSet rs = client.queryAggregate(null, stmt);
try {
if (rs.next()) {
Object obj = rs.getObject();
if (obj instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) obj;
long sum = (Long) map.get("sum");
long count = (Long) map.get("count");
double avg = (double) sum / count;
console.info("Sum=" + sum + " Count=" + count + " Average=" + avg);
double expected = 5.5;
if (avg != expected) {
console.error("Data mismatch: Expected %s. Received %s.", expected, avg);
}
} else {
console.error("Unexpected object returned: " + obj);
}
} else {
console.error("Query failed. No records returned.");
}
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class QueryFilter method runQuery.
@SuppressWarnings("unchecked")
private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
String nameFilter = "Bill";
String passFilter = "hknfpkj";
console.info("Query for: ns=%s set=%s index=%s name=%s pass=%s", params.namespace, params.set, indexName, nameFilter, passFilter);
Statement stmt = new Statement();
stmt.setNamespace(params.namespace);
stmt.setSetName(params.set);
stmt.setFilter(Filter.equal(binName, nameFilter));
stmt.setAggregateFunction("filter_example", "profile_filter", Value.get(passFilter));
// passFilter will be applied in filter_example.lua.
ResultSet rs = client.queryAggregate(null, stmt);
try {
int count = 0;
while (rs.next()) {
Map<String, Object> map = (Map<String, Object>) rs.getObject();
validate(map, "name", nameFilter);
validate(map, "password", passFilter);
count++;
}
if (count == 0) {
console.error("Query failed. No records returned.");
}
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class QueryRegionFilter method runQuery.
private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName1, String binName2) throws Exception {
StringBuilder rgnsb = new StringBuilder();
rgnsb.append("{ ");
rgnsb.append(" \"type\": \"Polygon\", ");
rgnsb.append(" \"coordinates\": [ ");
rgnsb.append(" [[-122.500000, 37.000000],[-121.000000, 37.000000], ");
rgnsb.append(" [-121.000000, 38.080000],[-122.500000, 38.080000], ");
rgnsb.append(" [-122.500000, 37.000000]] ");
rgnsb.append(" ] ");
rgnsb.append(" } ");
console.info("Query for: ns=%s set=%s index=%s bin1=%s bin2=%s within %s", params.namespace, params.set, indexName, binName1, binName2, rgnsb);
String amenStr = "school";
Statement stmt = new Statement();
stmt.setNamespace(params.namespace);
stmt.setSetName(params.set);
stmt.setFilter(Filter.geoWithinRegion(binName1, rgnsb.toString()));
stmt.setAggregateFunction("geo_filter_example", "match_amenity", Value.get(amenStr));
ResultSet rs = client.queryAggregate(null, stmt);
try {
int count = 0;
while (rs.next()) {
Object result = rs.getObject();
console.info("Record found: value2=%s", result);
count++;
}
if (count != 2) {
console.error("wrong number of schools found. %d != 2", count);
}
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project apex-malhar by apache.
the class AerospikeTestUtils method cleanTable.
// removes all records from set SET_NAME in namespace NAMESPACE
static void cleanTable() {
AerospikeClient client = null;
try {
client = new AerospikeClient(NODE, PORT);
Statement stmnt = new Statement();
stmnt.setNamespace(NAMESPACE);
stmnt.setSetName(SET_NAME);
RecordSet rs = client.query(null, stmnt);
while (rs.next()) {
client.delete(null, rs.getKey());
}
} catch (AerospikeException e) {
LOG.error("cleanTable failed: {}", e);
throw e;
} finally {
if (null != client) {
client.close();
}
}
}
Aggregations