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 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.Key in project aerospike-client-java by aerospike.
the class Replace method runReplaceOnlyExample.
public void runReplaceOnlyExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "replaceonlykey");
Bin bin = new Bin("bin", "value");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
console.info("Replace record requiring that it exists: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
try {
WritePolicy policy = new WritePolicy();
policy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
client.put(policy, key, bin);
console.error("Failure. This command should have resulted in an error.");
} catch (AerospikeException ae) {
if (ae.getResultCode() == ResultCode.KEY_NOT_FOUND_ERROR) {
console.info("Success. Key not found error returned as expected.");
} else {
throw ae;
}
}
}
Aggregations