use of com.aerospike.client.Record 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.getGeoJSONString(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();
}
}
use of com.aerospike.client.Record 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();
}
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class Replace method runReplaceExample.
public void runReplaceExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "replacekey");
Bin bin1 = new Bin("bin1", "value1");
Bin bin2 = new Bin("bin2", "value2");
Bin bin3 = new Bin("bin3", "value3");
console.info("Put: namespace=%s set=%s key=%s bin1=%s value1=%s bin2=%s value2=%s", key.namespace, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value);
client.put(params.writePolicy, key, bin1, bin2);
console.info("Replace with: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin3.name, bin3.value);
WritePolicy policy = new WritePolicy();
policy.recordExistsAction = RecordExistsAction.REPLACE;
client.put(policy, key, bin3);
console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
Record record = client.get(params.policy, key);
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
if (record.getValue(bin1.name) == null) {
console.info(bin1.name + " was deleted as expected.");
} else {
console.error(bin1.name + " found when it should have been deleted.");
}
if (record.getValue(bin2.name) == null) {
console.info(bin2.name + " was deleted as expected.");
} else {
console.error(bin2.name + " found when it should have been deleted.");
}
validateBin(key, bin3, record);
}
use of com.aerospike.client.Record in project aerospike-client-java by aerospike.
the class Touch method runExample.
/**
* Demonstrate touch command.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "touchkey");
Bin bin = new Bin(params.getBinName("touchbin"), "touchvalue");
console.info("Create record with 2 second expiration.");
WritePolicy writePolicy = new WritePolicy();
writePolicy.expiration = 2;
client.put(writePolicy, key, bin);
console.info("Touch same record with 5 second expiration.");
writePolicy.expiration = 5;
Record record = client.operate(writePolicy, key, Operation.touch(), Operation.getHeader());
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, null));
}
if (record.expiration == 0) {
throw new Exception(String.format("Failed to get record expiration: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
console.info("Sleep 3 seconds.");
Thread.sleep(3000);
record = client.get(params.policy, key, bin.name);
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
console.info("Success. Record still exists.");
console.info("Sleep 4 seconds.");
Thread.sleep(4000);
record = client.get(params.policy, key, bin.name);
if (record == null) {
console.info("Success. Record expired as expected.");
} else {
console.error("Found record when it should have expired.");
}
}
use of com.aerospike.client.Record 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();
}
}
Aggregations