Search in sources :

Example 66 with Bin

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

the class Append method runExample.

/**
 * Append string to an existing string.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "appendkey");
    String binName = params.getBinName("appendbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    Bin bin = new Bin(binName, "Hello");
    console.info("Initial append will create record.  Initial value is " + bin.value + '.');
    client.append(params.writePolicy, key, bin);
    bin = new Bin(binName, " World");
    console.info("Append \"" + bin.value + "\" to existing record.");
    client.append(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));
    }
    Object received = record.getValue(bin.name);
    String expected = "Hello World";
    if (received.equals(expected)) {
        console.info("Append successful: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        console.error("Append mismatch: Expected %s. Received %s.", expected, received);
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 67 with Bin

use of com.aerospike.client.Bin 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 68 with Bin

use of com.aerospike.client.Bin 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 69 with Bin

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

the class AsyncBatch method writeRecords.

/**
 * Write records individually.
 */
private void writeRecords() {
    WriteHandler handler = new WriteHandler(size);
    for (int i = 1; i <= size; i++) {
        Key key = sendKeys[i - 1];
        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(eventLoop, handler, params.writePolicy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 70 with Bin

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

the class QueryGeoCollection method writeMapRecords.

private void writeMapRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, String binName2, String valuePrefix, int size) throws Exception {
    for (int i = 0; i < size; i++) {
        Key key = new Key(params.namespace, params.set, keyPrefix + i);
        HashMap<String, Value> map = new HashMap<String, Value>();
        for (int jj = 0; jj < 10; ++jj) {
            double plat = 0.0 + (0.01 * i);
            double plng = 0.0 + (0.10 * jj);
            String geoString = generatePoint(plat, plng);
            map.put(valuePrefix + "pointkey_" + i + "_" + jj, Value.getAsGeoJSON(geoString));
            double rlat = 0.0 + (0.01 * i);
            double rlng = 0.0 - (0.10 * jj);
            geoString = generatePolygon(rlat, rlng);
            map.put(valuePrefix + "regionkey_" + i + "_" + jj, Value.getAsGeoJSON(geoString));
        }
        Bin bin = new Bin(binName, map);
        Bin bin2 = new Bin(binName2, "other_bin_value_" + i);
        client.put(params.writePolicy, key, bin, bin2);
    }
    console.info("Write " + size + " records.");
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) Value(com.aerospike.client.Value) Key(com.aerospike.client.Key)

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