Search in sources :

Example 71 with Statement

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

the class TestAsyncQuery method runQuery.

private void runQuery() {
    int begin = 26;
    int end = 34;
    Statement stmt = new Statement();
    stmt.setNamespace(args.namespace);
    stmt.setSetName(args.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.range(binName, begin, end));
    final AtomicInteger count = new AtomicInteger();
    client.query(eventLoop, new RecordSequenceListener() {

        public void onRecord(Key key, Record record) throws AerospikeException {
            int result = record.getInt(binName);
            assertBetween(26, 34, result);
            count.incrementAndGet();
        }

        public void onSuccess() {
            int size = count.get();
            assertEquals(9, size);
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, stmt);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordSequenceListener(com.aerospike.client.listener.RecordSequenceListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 72 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 73 with Statement

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

the class AsyncQuery method runQuery.

private void runQuery(AerospikeClient client, EventLoop eventLoop, final String binName) {
    int begin = 26;
    int end = 34;
    console.info("Query for: ns=%s set=%s bin=%s >= %s <= %s", params.namespace, params.set, 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));
    final AtomicInteger count = new AtomicInteger();
    client.query(eventLoop, new RecordSequenceListener() {

        public void onRecord(Key key, Record record) throws AerospikeException {
            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.incrementAndGet();
        }

        public void onSuccess() {
            int size = count.get();
            if (size != 9) {
                console.error("Query count mismatch. Expected 9. Received " + size);
            }
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            console.error("Query failed: " + Util.getErrorMessage(e));
            notifyComplete();
        }
    }, null, stmt);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordSequenceListener(com.aerospike.client.listener.RecordSequenceListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 74 with Statement

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

the class QueryExp 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));
    QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
    policy.filterExp = Exp.build(Exp.regexCompare("prefix.*suffix", RegexFlag.ICASE | RegexFlag.NEWLINE, Exp.stringBin("bin3")));
    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 75 with Statement

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

the class QueryExp method runQuery2.

private void runQuery2(AerospikeClient client, Parameters params, String binName) throws Exception {
    int begin = 10;
    int end = 40;
    console.info("Query Predicate: Record updated in 2020");
    Calendar beginTime = new GregorianCalendar(2020, 0, 1);
    Calendar endTime = new GregorianCalendar(2021, 0, 1);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.range(binName, begin, end));
    QueryPolicy policy = new QueryPolicy(client.queryPolicyDefault);
    policy.filterExp = Exp.build(Exp.and(Exp.ge(Exp.lastUpdate(), Exp.val(beginTime)), Exp.lt(Exp.lastUpdate(), Exp.val(endTime))));
    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) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) QueryPolicy(com.aerospike.client.policy.QueryPolicy)

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