use of com.aerospike.client.Bin in project aerospike-client-java by aerospike.
the class OperateMap method runNestedListCreateExample.
public void runNestedListCreateExample(AerospikeClient client, Parameters params) {
Key key = new Key(params.namespace, params.set, "mapkey3");
String binName = params.getBinName("mapbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
List<Value> l1 = new ArrayList<Value>();
l1.add(Value.get(7));
l1.add(Value.get(9));
l1.add(Value.get(5));
Map<Value, Value> inputMap = new HashMap<Value, Value>();
inputMap.put(Value.get("key1"), Value.get(l1));
// Create maps.
client.put(params.writePolicy, key, new Bin(binName, inputMap));
// Create ordered list at map's "key2" only if list does not exist.
// Append 2,1 to ordered list.
CTX ctx = CTX.mapKey(Value.get("key2"));
Record record = client.operate(params.writePolicy, key, ListOperation.create(binName, ListOrder.ORDERED, false, ctx), ListOperation.append(binName, Value.get(2), ctx), ListOperation.append(binName, Value.get(1), ctx), Operation.get(binName));
record = client.get(params.policy, key);
console.info("Record: " + record);
}
use of com.aerospike.client.Bin in project aerospike-client-java by aerospike.
the class Prepend method runExample.
/**
* Prepend string to an existing string.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "prependkey");
String binName = params.getBinName("prependbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
Bin bin = new Bin(binName, "World");
console.info("Initial prepend will create record. Initial value is " + bin.value + '.');
client.prepend(params.writePolicy, key, bin);
bin = new Bin(binName, "Hello ");
console.info("Prepend \"" + bin.value + "\" to existing record.");
client.prepend(params.writePolicy, key, bin);
Record 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));
}
// The value received from the server is an unsigned byte stream.
// Convert to an integer before comparing with expected.
Object received = record.getValue(bin.name);
String expected = "Hello World";
if (received.equals(expected)) {
console.info("Prepend successful: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
} else {
console.error("Prepend mismatch: Expected %s. Received %s.", expected, received);
}
}
use of com.aerospike.client.Bin in project aerospike-client-java by aerospike.
the class AsyncPutGet method runExample.
/**
* Asynchronously write and read a bin using alternate methods.
*/
@Override
public void runExample(AerospikeClient client, EventLoop eventLoop) {
Key key = new Key(params.namespace, params.set, "putgetkey");
Bin bin = new Bin(params.getBinName("putgetbin"), "value");
runPutGetInline(client, eventLoop, key, bin);
runPutGetWithRetry(client, eventLoop, key, bin);
}
use of com.aerospike.client.Bin in project aerospike-client-java by aerospike.
the class BatchOperate method writeRecords.
private void writeRecords(AerospikeClient client, Parameters params) {
for (int i = 1; i <= RecordCount; i++) {
Key key = new Key(params.namespace, params.set, KeyPrefix + i);
Bin bin1 = new Bin(BinName1, i);
Bin bin2 = new Bin(BinName2, i + 10);
List<Integer> list = new ArrayList<Integer>();
for (int j = 0; j < i; j++) {
list.add(j * i);
}
Bin bin3 = new Bin(BinName3, list);
console.info("Put: ns=%s set=%s key=%s val1=%s val2=%s val3=%s", key.namespace, key.setName, key.userKey, bin1.value, bin2.value, list.toString());
client.put(params.writePolicy, key, bin1, bin2, bin3);
}
}
use of com.aerospike.client.Bin in project aerospike-client-java by aerospike.
the class Expire method expireExample.
/**
* Write and twice read an expiration record.
*/
private void expireExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "expirekey ");
Bin bin = new Bin(params.getBinName("expirebin"), "expirevalue");
console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expiration=2", key.namespace, key.setName, key.userKey, bin.name, bin.value);
// Specify that record expires 2 seconds after it's written.
WritePolicy writePolicy = new WritePolicy();
writePolicy.expiration = 2;
client.put(writePolicy, key, bin);
// Read the record before it expires, showing it is there.
console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
Record record = client.get(params.policy, key, bin.name);
if (record == null) {
throw new Exception(String.format("Failed to get record: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
Object received = record.getValue(bin.name);
String expected = bin.value.toString();
if (received.equals(expected)) {
console.info("Get record successful: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
} else {
throw new Exception(String.format("Expire record mismatch: Expected %s. Received %s.", expected, received));
}
// Read the record after it expires, showing it's gone.
console.info("Sleeping for 3 seconds ...");
Thread.sleep(3 * 1000);
record = client.get(params.policy, key, bin.name);
if (record == null) {
console.info("Expiry of record successful. Record not found.");
} else {
console.error("Found record when it should have expired.");
}
}
Aggregations