use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class AsyncBatch method batchReadComplex.
/**
* Read records with varying namespaces, bin names and read types in one batch.
* This requires Aerospike Server version >= 3.6.0
*/
private void batchReadComplex() throws Exception {
// Batch gets into one call.
// Batch allows multiple namespaces in one call, but example test environment may only have one namespace.
String[] bins = new String[] { binName };
List<BatchRead> records = new ArrayList<BatchRead>();
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 1), bins));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 2), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 3), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 4), false));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 5), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 6), true));
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 7), bins));
// This record should be found, but the requested bin will not be found.
records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 8), new String[] { "binnotfound" }));
// This record should not be found.
records.add(new BatchRead(new Key(params.namespace, params.set, "keynotfound"), bins));
// Execute batch.
client.get(eventLoop, new BatchListListener() {
public void onSuccess(List<BatchRead> records) {
// Show results.
int found = 0;
for (BatchRead record : records) {
Key key = record.key;
Record rec = record.record;
if (rec != null) {
found++;
console.info("Record: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, rec.getValue(binName));
} else {
console.info("Record not found: ns=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, binName);
}
}
if (found != 8) {
console.error("Records found mismatch. Expected %d. Received %d.", 8, found);
}
taskComplete();
}
public void onFailure(AerospikeException e) {
console.error("Batch read complex failed: " + Util.getErrorMessage(e));
taskComplete();
}
}, null, records);
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class UserDefinedFunction method writeUsingUdf.
private void writeUsingUdf(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "udfkey1");
Bin bin = new Bin(params.getBinName("udfbin1"), "string value");
client.execute(params.writePolicy, key, "record_example", "writeBin", Value.get(bin.name), bin.value);
Record record = client.get(params.policy, key, bin.name);
String expected = bin.value.toString();
String received = record.getString(bin.name);
if (received != null && received.equals(expected)) {
console.info("Data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
} else {
console.error("Data mismatch: Expected %s. Received %s.", expected, received);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class UserDefinedFunction method writeIfNotExists.
private void writeIfNotExists(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "udfkey3");
String binName = "udfbin3";
// Delete record if it already exists.
client.delete(params.writePolicy, key);
// Write record only if not already exists. This should succeed.
client.execute(params.writePolicy, key, "record_example", "writeUnique", Value.get(binName), Value.get("first"));
// Verify record written.
Record record = client.get(params.policy, key, binName);
String expected = "first";
String received = record.getString(binName);
if (received != null && received.equals(expected)) {
console.info("Record written: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
} else {
console.error("Data mismatch: Expected %s. Received %s.", expected, received);
}
// Write record second time. This should fail.
console.info("Attempt second write.");
client.execute(params.writePolicy, key, "record_example", "writeUnique", Value.get(binName), Value.get("second"));
// Verify record not written.
record = client.get(params.policy, key, binName);
received = record.getString(binName);
if (received != null && received.equals(expected)) {
console.info("Success. Record remained unchanged: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
} else {
console.error("Data mismatch: Expected %s. Received %s.", expected, received);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class UserDefinedFunction method appendListUsingUdf.
private void appendListUsingUdf(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "udfkey5");
String binName = params.getBinName("udfbin5");
String value = "appended value";
client.execute(params.writePolicy, key, "record_example", "appendListBin", Value.get(binName), Value.get(value));
Record record = client.get(params.policy, key, binName);
if (record != null) {
Object received = record.getValue(binName);
if (received != null && received instanceof List<?>) {
List<?> list = (List<?>) received;
if (list.size() == 5) {
Object obj = list.get(4);
if (obj.equals(value)) {
console.info("UDF data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
return;
}
}
}
console.error("UDF data mismatch");
console.error("Expected: " + value);
console.error("Received: " + received);
} else {
console.error("Failed to find record: " + key.userKey);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class QuerySum method writeRecords.
private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, int size) throws Exception {
for (int i = 1; i <= size; i++) {
Key key = new Key(params.namespace, params.set, keyPrefix + i);
Bin bin = new Bin(binName, i);
console.info("Put: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
client.put(params.writePolicy, key, bin);
}
}
Aggregations