use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class TestQuerySum method querySum.
@Test
public void querySum() {
int begin = 4;
int end = 7;
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(args.set);
stmt.setBinNames(binName);
stmt.setFilter(Filter.range(binName, begin, end));
stmt.setAggregateFunction(TestQuerySum.class.getClassLoader(), "udf/sum_example.lua", "sum_example", "sum_single_bin", Value.get(binName));
ResultSet rs = client.queryAggregate(null, stmt);
try {
// 4 + 5 + 6 + 7
int expected = 22;
int count = 0;
while (rs.next()) {
Object object = rs.getObject();
long sum = 0;
if (object instanceof Long) {
sum = (Long) rs.getObject();
} else {
fail("Return value not a long: " + object);
}
assertEquals(expected, (int) sum);
count++;
}
assertNotEquals(0, count);
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class TestQueryAverage method queryAverage.
@Test
public void queryAverage() {
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(args.set);
stmt.setFilter(Filter.range(binName, 0, 1000));
stmt.setAggregateFunction(TestQueryAverage.class.getClassLoader(), "udf/average_example.lua", "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;
assertEquals(5.5, avg, 0.00000001);
} else {
fail("Unexpected object returned: " + obj);
}
} else {
fail("Query failed. No records returned.");
}
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class TestQueryCollection method queryCollection.
@Test
public void queryCollection() {
String queryMapKey = mapKeyPrefix + 2;
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(args.set);
stmt.setBinNames(binName);
stmt.setFilter(Filter.contains(binName, IndexCollectionType.MAPKEYS, queryMapKey));
RecordSet rs = client.query(null, stmt);
try {
int count = 0;
while (rs.next()) {
Record record = rs.getRecord();
Map<?, ?> result = (Map<?, ?>) record.getValue(binName);
if (!result.containsKey(queryMapKey)) {
fail("Query mismatch: Expected mapKey " + queryMapKey + " Received " + result);
}
count++;
}
assertNotEquals(0, count);
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class TestQueryFilter method queryFilter.
@Test
@SuppressWarnings("unchecked")
public void queryFilter() {
String nameFilter = "Bill";
String passFilter = "hknfpkj";
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(args.set);
stmt.setFilter(Filter.equal(binName, nameFilter));
stmt.setAggregateFunction(TestQueryFilter.class.getClassLoader(), "udf/filter_example.lua", "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();
assertEquals(nameFilter, map.get("name"));
assertEquals(passFilter, map.get("password"));
count++;
}
assertNotEquals(0, count);
} finally {
rs.close();
}
}
use of com.aerospike.client.query.Statement in project aerospike-client-java by aerospike.
the class TestQueryPredExp method queryPredicate3.
@Test
public void queryPredicate3() {
int begin = 10;
int end = 45;
Statement stmt = new Statement();
stmt.setNamespace(args.namespace);
stmt.setSetName(setName);
stmt.setFilter(Filter.range(binName, begin, end));
stmt.setPredExp(PredExp.recLastUpdate(), PredExp.integerValue(System.currentTimeMillis() * 1000000L + 100), PredExp.integerGreater());
/*
stmt.setPredicate(
Predicate.recordLastUpdate().greaterThan(Predicate.nanos(System.currentTimeMillis() + 100))
);
*/
RecordSet rs = client.query(null, stmt);
try {
while (rs.next()) {
//Record record = rs.getRecord();
//System.out.println(record.getValue(binName).toString() + ' ' + record.expiration);
//count++;
}
// Do not asset count since some tests can run after this one.
//assertEquals(0, count);
} finally {
rs.close();
}
}
Aggregations