Search in sources :

Example 21 with Bin

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

the class ScanResume method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params, String setName, String binName, int size) throws Exception {
    console.info("Write " + size + " records.");
    for (int i = 1; i <= size; i++) {
        Key key = new Key(params.namespace, setName, i);
        Bin bin = new Bin(binName, i);
        client.put(params.writePolicy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 22 with Bin

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

the class Serialize method testList.

/**
 * Write list using standard java serializer.
 */
public void testList(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "seriallistkey");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Initialize list");
    ArrayList<String> list = new ArrayList<String>();
    list.add("string1");
    list.add("string2");
    list.add("string3");
    Bin bin = new Bin(params.getBinName("serialbin"), (Object) list);
    console.info("Write list using serializer.");
    client.put(params.writePolicy, key, bin);
    console.info("Read list using serializer.");
    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));
    }
    List<?> received;
    try {
        received = (List<?>) record.getValue(bin.name);
    } catch (Exception e) {
        throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
    }
    if (received.size() != 3) {
        throw new Exception(String.format("Array length mismatch: Expected=%d Received=%d", 3, received.size()));
    }
    for (int i = 0; i < received.size(); i++) {
        String expected = "string" + (i + 1);
        if (!received.get(i).equals(expected)) {
            Object obj = received.get(i);
            throw new Exception(String.format("Mismatch: index=%d expected=%s received=%s", i, expected, obj));
        }
    }
    console.info("Read list successful.");
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 23 with Bin

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

the class Serialize method testArray.

/**
 * Write array of integers using standard java serializer..
 */
public void testArray(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "serialarraykey");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Initialize array");
    int[] array = new int[10000];
    for (int i = 0; i < 10000; i++) {
        array[i] = i * i;
    }
    Bin bin = new Bin(params.getBinName("serialbin"), array);
    // Do a test that pushes this complex object through the serializer
    console.info("Write array using serializer.");
    client.put(params.writePolicy, key, bin);
    console.info("Read array using serializer.");
    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));
    }
    int[] received;
    try {
        received = (int[]) record.getValue(bin.name);
    } catch (Exception e) {
        throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
    }
    if (received.length != 10000) {
        throw new Exception(String.format("Array length mismatch: Expected=%d Received=%d", 10000, received.length));
    }
    for (int i = 0; i < 10000; i++) {
        if (received[i] != i * i) {
            throw new Exception(String.format("Mismatch: index=%d expected=%d received=%d", i, i * i, received[i]));
        }
    }
    console.info("Read array successful.");
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 24 with Bin

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

the class Serialize method testComplex.

/**
 * Write complex object using standard java serializer.
 */
public void testComplex(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "serialcomplexkey");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Initialize complex object");
    ArrayList<Object> inner = new ArrayList<Object>();
    inner.add("string2");
    inner.add(8);
    HashMap<Object, Object> innerMap = new HashMap<Object, Object>();
    innerMap.put("a", 1);
    innerMap.put(2, "b");
    innerMap.put("list", inner);
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("string1");
    list.add(4);
    list.add(inner);
    list.add(innerMap);
    Bin bin = new Bin(params.getBinName("complexbin"), new Value.BlobValue(list));
    console.info("Write complex object using serializer.");
    client.put(params.writePolicy, key, bin);
    console.info("Read complex object using serializer.");
    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;
    try {
        received = (List<?>) record.getValue(bin.name);
    } catch (Exception e) {
        throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
    }
    if (received != null && received.equals(list)) {
        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");
        console.error("Expected " + list);
        console.error("Received " + received);
    }
    console.info("Read complex object successful.");
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 25 with Bin

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

the class StoreKey method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, int size) throws Exception {
    console.info("Write " + size + " records with store user key option.");
    WritePolicy policy = new WritePolicy();
    policy.sendKey = true;
    for (int i = 1; i <= size; i++) {
        Key key = new Key(params.namespace, params.set, keyPrefix + i);
        Bin bin = new Bin(binName, i);
        client.put(policy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Aggregations

Bin (com.aerospike.client.Bin)151 Key (com.aerospike.client.Key)129 Record (com.aerospike.client.Record)70 Test (org.junit.Test)54 AerospikeException (com.aerospike.client.AerospikeException)34 ArrayList (java.util.ArrayList)31 WritePolicy (com.aerospike.client.policy.WritePolicy)27 HashMap (java.util.HashMap)22 Value (com.aerospike.client.Value)20 List (java.util.List)15 BeforeClass (org.junit.BeforeClass)14 Map (java.util.Map)13 Policy (com.aerospike.client.policy.Policy)12 IndexTask (com.aerospike.client.task.IndexTask)11 WriteListener (com.aerospike.client.listener.WriteListener)5 RegisterTask (com.aerospike.client.task.RegisterTask)5 AbstractMap (java.util.AbstractMap)4 TreeMap (java.util.TreeMap)4 CTX (com.aerospike.client.cdt.CTX)3 BitPolicy (com.aerospike.client.operation.BitPolicy)3