Search in sources :

Example 61 with RecordSet

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

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

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

Example 64 with RecordSet

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

use of com.aerospike.client.query.RecordSet in project gora by apache.

the class AerospikeStore method execute.

/**
 * {@inheritDoc}
 *
 * @param query the query to execute.
 * @return the query result
 */
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
    List<AerospikeResultRecord> resultRecords = new ArrayList<>();
    String namespace = aerospikeParameters.getAerospikeMapping().getNamespace();
    String set = aerospikeParameters.getAerospikeMapping().getSet();
    try {
        // Query execution without any keys
        if (query.getStartKey() == null && query.getEndKey() == null) {
            try (RecordSet recordSet = aerospikeClient.query(null, getStatement(namespace, set))) {
                while (recordSet.next()) {
                    AerospikeResultRecord aerospikeRecord = new AerospikeResultRecord(recordSet.getKey(), recordSet.getRecord());
                    resultRecords.add(aerospikeRecord);
                }
            }
        } else // Query execution for single key
        if (query.getKey() != null) {
            Key key = getAerospikeKey(query.getKey());
            Record record = aerospikeClient.get(null, key);
            if (record != null) {
                resultRecords.add(new AerospikeResultRecord(key, record));
            }
        }
        // }
        return new AerospikeQueryResult<>(this, query, resultRecords, getFieldsToQuery(null));
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) AerospikeResultRecord(org.apache.gora.aerospike.query.AerospikeResultRecord) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) AerospikeResultRecord(org.apache.gora.aerospike.query.AerospikeResultRecord) RecordSet(com.aerospike.client.query.RecordSet) AerospikeQueryResult(org.apache.gora.aerospike.query.AerospikeQueryResult) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Aggregations

RecordSet (com.aerospike.client.query.RecordSet)65 Statement (com.aerospike.client.query.Statement)63 Test (org.junit.Test)43 QueryPolicy (com.aerospike.client.policy.QueryPolicy)28 Record (com.aerospike.client.Record)22 Key (com.aerospike.client.Key)9 AerospikeException (com.aerospike.client.AerospikeException)6 AerospikeClient (com.aerospike.client.AerospikeClient)4 ArrayList (java.util.ArrayList)3 GregorianCalendar (java.util.GregorianCalendar)3 Node (com.aerospike.client.cluster.Node)2 ExecuteTask (com.aerospike.client.task.ExecuteTask)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Bin (com.aerospike.client.Bin)1 Expression (com.aerospike.client.exp.Expression)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 AerospikeQueryResult (org.apache.gora.aerospike.query.AerospikeQueryResult)1