Search in sources :

Example 6 with Statement

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

the class QueryRegion method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) 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("QueryRegion for: ns=%s set=%s index=%s bin=%s within %s", params.namespace, params.set, indexName, binName, rgnsb);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.geoWithinRegion(binName, rgnsb.toString()));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            String result = record.getGeoJSON(binName);
            console.info("Record found: ns=%s set=%s bin=%s digest=%s value=%s", key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), result);
            count++;
        }
        if (count != 6) {
            console.error("Query count mismatch. Expected 6. 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 7 with Statement

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

the class QueryGeoCollection method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String binName, String binName2, IndexCollectionType indexType) throws Exception {
    console.info("Query for: ns=%s set=%s bin=%s %s within <region>", params.namespace, params.set, binName, indexType.toString());
    StringBuilder rgnsb = generateQueryRegion();
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.geoWithinRegion(binName, indexType, rgnsb.toString()));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        Set<String> uniques = new HashSet<String>();
        while (rs.next()) {
            Record record = rs.getRecord();
            uniques.add(record.getString(binName2));
            count++;
        }
        if (count != 697) {
            console.error("Query failed. %d records expected. %d returned.", 697, count);
        } else if (uniques.size() != 21) {
            console.error("Query failed. %d unique records expected. %d unique returned.", 21, uniques.size());
        } else {
            console.info("query succeeded with %d records %d unique", count, uniques.size());
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) HashSet(java.util.HashSet)

Example 8 with Statement

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

the class QueryInteger method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    int begin = 14;
    int end = 18;
    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));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            int result = record.getInt(binName);
            console.info("Record found: ns=%s set=%s bin=%s digest=%s value=%s", key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), result);
            count++;
        }
        if (count != 5) {
            console.error("Query count mismatch. Expected 5. 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 9 with Statement

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

the class QueryString method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName, String valuePrefix) throws Exception {
    String filter = valuePrefix + 3;
    console.info("Query for: ns=%s set=%s index=%s bin=%s filter=%s", params.namespace, params.set, indexName, binName, filter);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.equal(binName, filter));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            String result = record.getString(binName);
            if (result.equals(filter)) {
                console.info("Record found: ns=%s set=%s bin=%s key=%s value=%s", key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), result);
            } else {
                console.error("Query mismatch: Expected %s. Received %s.", filter, result);
            }
            count++;
        }
        if (count == 0) {
            console.error("Query failed. No records returned.");
        }
    } 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 10 with Statement

use of com.aerospike.client.query.Statement 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)

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