Search in sources :

Example 71 with Bin

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

the class QueryRegion method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, int size) throws Exception {
    console.info("Write " + size + " records.");
    for (int i = 0; i < size; i++) {
        double lng = -122 + (0.1 * i);
        double lat = 37.5 + (0.1 * i);
        StringBuilder ptsb = new StringBuilder();
        ptsb.append("{ \"type\": \"Point\", \"coordinates\": [");
        ptsb.append(String.valueOf(lng));
        ptsb.append(", ");
        ptsb.append(String.valueOf(lat));
        ptsb.append("] }");
        Key key = new Key(params.namespace, params.set, keyPrefix + i);
        Bin bin = Bin.asGeoJSON(binName, ptsb.toString());
        client.put(params.writePolicy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 72 with Bin

use of com.aerospike.client.Bin 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);
}
Also used : 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 73 with Bin

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

Example 74 with Bin

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

the class Touch method runExample.

/**
 * Demonstrate touch command.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "touchkey");
    Bin bin = new Bin(params.getBinName("touchbin"), "touchvalue");
    console.info("Create record with 2 second expiration.");
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = 2;
    client.put(writePolicy, key, bin);
    console.info("Touch same record with 5 second expiration.");
    writePolicy.expiration = 5;
    Record record = client.operate(writePolicy, key, Operation.touch(), Operation.getHeader());
    if (record == null) {
        throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, null));
    }
    if (record.expiration == 0) {
        throw new Exception(String.format("Failed to get record expiration: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    console.info("Sleep 3 seconds.");
    Thread.sleep(3000);
    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));
    }
    console.info("Success. Record still exists.");
    console.info("Sleep 4 seconds.");
    Thread.sleep(4000);
    record = client.get(params.policy, key, bin.name);
    if (record == null) {
        console.info("Success. Record expired as expected.");
    } 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 75 with Bin

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

the class TestAsyncQuery method asyncQuery.

@Test
public void asyncQuery() {
    WriteListener listener = new WriteListener() {

        private int count = 0;

        public void onSuccess(final Key key) {
            // in the same event loop thread.
            if (++count == size) {
                runQuery();
            }
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    };
    for (int i = 1; i <= size; i++) {
        final Key key = new Key(args.namespace, args.set, keyPrefix + i);
        Bin bin = new Bin(binName, i);
        client.put(eventLoop, listener, null, key, bin);
    }
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) WriteListener(com.aerospike.client.listener.WriteListener) Key(com.aerospike.client.Key) Test(org.junit.Test)

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