Search in sources :

Example 56 with Key

use of com.aerospike.client.Key in project aerospike-client-java by aerospike.

the class Batch method writeRecords.

/**
 * Write records individually.
 */
private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, String valuePrefix, 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, valuePrefix + 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);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 57 with Key

use of com.aerospike.client.Key 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.");
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 58 with Key

use of com.aerospike.client.Key in project aerospike-client-java by aerospike.

the class Generation method runExample.

/**
 * Exercise record generation functionality.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "genkey");
    String binName = params.getBinName("genbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    // Set some values for the same record.
    Bin bin = new Bin(binName, "genvalue1");
    console.info("Put: namespace=%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);
    bin = new Bin(binName, "genvalue2");
    console.info("Put: namespace=%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);
    // Retrieve record and its generation count.
    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));
    }
    Object received = record.getValue(bin.name);
    String expected = bin.value.toString();
    if (received.equals(expected)) {
        console.info("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d", key.namespace, key.setName, key.userKey, bin.name, received, record.generation);
    } else {
        throw new Exception(String.format("Get mismatch: Expected %s. Received %s.", expected, received));
    }
    // Set record and fail if it's not the expected generation.
    bin = new Bin(binName, "genvalue3");
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d", key.namespace, key.setName, key.userKey, bin.name, bin.value, record.generation);
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL;
    writePolicy.generation = record.generation;
    client.put(writePolicy, key, bin);
    // Set record with invalid generation and check results .
    bin = new Bin(binName, "genvalue4");
    writePolicy.generation = 9999;
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expected generation=%d", key.namespace, key.setName, key.userKey, bin.name, bin.value, writePolicy.generation);
    try {
        client.put(writePolicy, key, bin);
        throw new Exception("Should have received generation error instead of success.");
    } catch (AerospikeException ae) {
        if (ae.getResultCode() == ResultCode.GENERATION_ERROR) {
            console.info("Success: Generation error returned as expected.");
        } else {
            throw new Exception(String.format("Unexpected set return code: namespace=%s set=%s key=%s bin=%s value=%s code=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value, ae.getResultCode()));
        }
    }
    // Verify results.
    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));
    }
    received = record.getValue(bin.name);
    expected = "genvalue3";
    if (received.equals(expected)) {
        console.info("Get successful: namespace=%s set=%s key=%s bin=%s value=%s generation=%d", key.namespace, key.setName, key.userKey, bin.name, received, record.generation);
    } else {
        throw new Exception(String.format("Get mismatch: Expected %s. Received %s.", expected, received));
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 59 with Key

use of com.aerospike.client.Key in project aerospike-client-java by aerospike.

the class ListMap method testListComplex.

/**
 * Write/Read ArrayList<Object> directly instead of relying on java serializer.
 */
private void testListComplex(AerospikeClient client, Parameters params) throws Exception {
    console.info("Read/Write ArrayList<Object>");
    Key key = new Key(params.namespace, params.set, "listkey2");
    client.delete(params.writePolicy, key);
    byte[] blob = new byte[] { 3, 52, 125 };
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("string1");
    list.add(2);
    list.add(blob);
    Bin bin = new Bin(params.getBinName("listbin2"), list);
    client.put(params.writePolicy, key, bin);
    Record record = client.get(params.policy, key, bin.name);
    List<?> receivedList = (List<?>) record.getValue(bin.name);
    validateSize(3, receivedList.size());
    validate("string1", receivedList.get(0));
    // Server convert numbers to long, so must expect long.
    validate(2L, receivedList.get(1));
    validate(blob, (byte[]) receivedList.get(2));
    console.info("Read/Write ArrayList<Object> successful.");
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) List(java.util.List) ArrayList(java.util.ArrayList) Key(com.aerospike.client.Key)

Example 60 with Key

use of com.aerospike.client.Key in project aerospike-client-java by aerospike.

the class ListMap method testMapComplex.

/**
 * Write/Read HashMap<Object,Object> directly instead of relying on java serializer.
 */
private void testMapComplex(AerospikeClient client, Parameters params) throws Exception {
    console.info("Read/Write HashMap<Object,Object>");
    Key key = new Key(params.namespace, params.set, "mapkey2");
    client.delete(params.writePolicy, key);
    byte[] blob = new byte[] { 3, 52, 125 };
    List<Integer> list = new ArrayList<Integer>();
    list.add(100034);
    list.add(12384955);
    list.add(3);
    list.add(512);
    HashMap<Object, Object> map = new HashMap<Object, Object>();
    map.put("key1", "string1");
    map.put("key2", 2);
    map.put("key3", blob);
    // map.put("key4", Value.getAsList(list)) works too
    map.put("key4", list);
    map.put("key5", true);
    map.put("key6", false);
    Bin bin = new Bin(params.getBinName("mapbin2"), map);
    client.put(params.writePolicy, key, bin);
    Record record = client.get(params.policy, key, bin.name);
    Map<?, ?> receivedMap = (Map<?, ?>) record.getValue(bin.name);
    validateSize(6, receivedMap.size());
    validate("string1", receivedMap.get("key1"));
    // Server convert numbers to long, so must expect long.
    validate(2L, receivedMap.get("key2"));
    validate(blob, (byte[]) receivedMap.get("key3"));
    List<?> receivedInner = (List<?>) receivedMap.get("key4");
    validateSize(4, receivedInner.size());
    validate(100034L, receivedInner.get(0));
    validate(12384955L, receivedInner.get(1));
    validate(3L, receivedInner.get(2));
    validate(512L, receivedInner.get(3));
    validate(true, receivedMap.get("key5"));
    validate(false, receivedMap.get("key6"));
    console.info("Read/Write HashMap<Object,Object> successful");
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.aerospike.client.Key)

Aggregations

Key (com.aerospike.client.Key)204 Record (com.aerospike.client.Record)106 Bin (com.aerospike.client.Bin)104 Test (org.junit.Test)79 AerospikeException (com.aerospike.client.AerospikeException)50 ArrayList (java.util.ArrayList)50 Value (com.aerospike.client.Value)37 HashMap (java.util.HashMap)32 List (java.util.List)31 WritePolicy (com.aerospike.client.policy.WritePolicy)21 Map (java.util.Map)15 Statement (com.aerospike.client.query.Statement)10 BeforeClass (org.junit.BeforeClass)10 Policy (com.aerospike.client.policy.Policy)9 RecordSet (com.aerospike.client.query.RecordSet)9 IndexTask (com.aerospike.client.task.IndexTask)8 IOException (java.io.IOException)6 Collections.singletonList (java.util.Collections.singletonList)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 BatchRead (com.aerospike.client.BatchRead)5