Search in sources :

Example 11 with Statement

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();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) ResultSet(com.aerospike.client.query.ResultSet) Test(org.junit.Test)

Example 12 with Statement

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();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) ResultSet(com.aerospike.client.query.ResultSet) Map(java.util.Map) Test(org.junit.Test)

Example 13 with Statement

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();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with Statement

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();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) ResultSet(com.aerospike.client.query.ResultSet) Map(java.util.Map) Test(org.junit.Test)

Example 15 with Statement

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();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) RecordSet(com.aerospike.client.query.RecordSet) Test(org.junit.Test)

Aggregations

Statement (com.aerospike.client.query.Statement)37 RecordSet (com.aerospike.client.query.RecordSet)26 Test (org.junit.Test)18 Record (com.aerospike.client.Record)16 Key (com.aerospike.client.Key)9 ResultSet (com.aerospike.client.query.ResultSet)7 Map (java.util.Map)6 AerospikeException (com.aerospike.client.AerospikeException)2 RecordSequenceListener (com.aerospike.client.listener.RecordSequenceListener)2 ExecuteTask (com.aerospike.client.task.ExecuteTask)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 HashSet (java.util.HashSet)1