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();
}
}
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();
}
}
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();
}
}
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();
}
}
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);
}
}
Aggregations