Search in sources :

Example 21 with Statement

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

the class StoreKey method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    int begin = 2;
    int end = 5;
    console.info("Query user key 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));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            if (key.userKey != null) {
                Object userkey = key.userKey.getObject();
                Record record = rs.getRecord();
                int result = record.getInt(binName);
                if (userkey != null) {
                    console.info("Record found with user key: ns=%s set=%s bin=%s userkey=%s value=%s", key.namespace, key.setName, binName, userkey, result);
                } else {
                    console.error("Record found with null user key: ns=%s set=%s bin=%s userkey=null value=%s", key.namespace, key.setName, binName, result);
                }
            } else {
                console.error("Record found with null user key: ns=%s set=%s bin=%s userkey=null", key.namespace, key.setName, binName);
            }
            count++;
        }
        if (count != 4) {
            console.error("Query count mismatch. Expected 4. Received " + count);
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) Key(com.aerospike.client.Key)

Example 22 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 23 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 24 with Statement

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

the class QueryPredExp method runQuery3.

private void runQuery3(AerospikeClient client, Parameters params, String binName) throws Exception {
    int begin = 20;
    int end = 30;
    console.info("Query Predicate: bin3 contains string with 'prefix' and 'suffix'");
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.range(binName, begin, end));
    stmt.setPredExp(PredExp.stringBin("bin3"), PredExp.stringValue("prefix.*suffix"), PredExp.stringRegex(RegexFlag.ICASE | RegexFlag.NEWLINE));
    RecordSet rs = client.query(null, 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)

Example 25 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)

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