Search in sources :

Example 76 with Statement

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();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) QueryPolicy(com.aerospike.client.policy.QueryPolicy)

Example 77 with Statement

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

Example 78 with Statement

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

Example 79 with Statement

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

Example 80 with Statement

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

Aggregations

Statement (com.aerospike.client.query.Statement)82 RecordSet (com.aerospike.client.query.RecordSet)63 Test (org.junit.Test)53 QueryPolicy (com.aerospike.client.policy.QueryPolicy)28 Record (com.aerospike.client.Record)23 Key (com.aerospike.client.Key)10 ResultSet (com.aerospike.client.query.ResultSet)9 Node (com.aerospike.client.cluster.Node)8 ExecuteTask (com.aerospike.client.task.ExecuteTask)8 AerospikeException (com.aerospike.client.AerospikeException)7 Map (java.util.Map)6 AerospikeClient (com.aerospike.client.AerospikeClient)4 Ignore (org.junit.Ignore)4 Bin (com.aerospike.client.Bin)3 GregorianCalendar (java.util.GregorianCalendar)3 RecordSequenceListener (com.aerospike.client.listener.RecordSequenceListener)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2