Search in sources :

Example 1 with ResultSet

use of com.aerospike.client.query.ResultSet in project aerospike-client-java by aerospike.

the class QuerySum method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    int begin = 4;
    int end = 7;
    console.info("Query for: ns=%s set=%s index=%s bin=%s >= %s <= %s", params.namespace, params.set, indexName, binName, begin, end);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.range(binName, begin, end));
    stmt.setAggregateFunction("sum_example", "sum_single_bin", Value.get(binName));
    // Alternately load aggregate function from resource
    // stmt.setAggregateFunction(QuerySum.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;
            if (object instanceof Long) {
                sum = (Long) rs.getObject();
            } else {
                console.error("Return value not a long: " + object);
                continue;
            }
            if (expected == (int) sum) {
                console.info("Sum matched: value=%d", expected);
            } else {
                console.error("Sum mismatch: Expected %d. Received %d.", expected, (int) sum);
            }
            count++;
        }
        if (count == 0) {
            console.error("Query failed. No records returned.");
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) ResultSet(com.aerospike.client.query.ResultSet)

Example 2 with ResultSet

use of com.aerospike.client.query.ResultSet 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 3 with ResultSet

use of com.aerospike.client.query.ResultSet 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 4 with ResultSet

use of com.aerospike.client.query.ResultSet 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 5 with ResultSet

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

Aggregations

ResultSet (com.aerospike.client.query.ResultSet)7 Statement (com.aerospike.client.query.Statement)7 Map (java.util.Map)4 Test (org.junit.Test)3