Search in sources :

Example 66 with Key

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

the class OperateMap method runScoreExample.

/**
 * Map score example.
 */
public void runScoreExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "mapkey");
    String binName = params.getBinName("mapbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("Charlie"), Value.get(55));
    inputMap.put(Value.get("Jim"), Value.get(98));
    inputMap.put(Value.get("John"), Value.get(76));
    inputMap.put(Value.get("Harry"), Value.get(82));
    // Write values to empty map.
    Record record = client.operate(params.writePolicy, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    console.info("Record: " + record);
    // Increment some user scores.
    record = client.operate(params.writePolicy, key, MapOperation.increment(MapPolicy.Default, binName, Value.get("John"), Value.get(5)), MapOperation.decrement(MapPolicy.Default, binName, Value.get("Jim"), Value.get(4)));
    console.info("Record: " + record);
    // Get top two scores.
    record = client.operate(params.writePolicy, key, MapOperation.getByRankRange(binName, -2, 2, MapReturnType.KEY_VALUE));
    console.info("Record: " + record);
    // Print results.
    List<?> results = record.getList(binName);
    for (Object result : results) {
        console.info("Received: " + result);
    }
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 67 with Key

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

Example 68 with Key

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

the class PutGet method runSingleBinTest.

/**
 * Execute put and get on a server configured as single-bin.
 */
private void runSingleBinTest(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "putgetkey");
    Bin bin = new Bin("", "value");
    console.info("Single Bin Put: namespace=%s set=%s key=%s value=%s", key.namespace, key.setName, key.userKey, bin.value);
    client.put(params.writePolicy, key, bin);
    console.info("Single Bin 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));
    }
    validateBin(key, bin, record);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 69 with Key

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

the class QueryExecute method validateRecords.

private void validateRecords(AerospikeClient client, Parameters params, String indexName, String binName1, String binName2, int size) throws Exception {
    int begin = 1;
    int end = size + 100;
    console.info("Validate records");
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setFilter(Filter.range(binName1, begin, end));
    RecordSet rs = client.query(null, stmt);
    try {
        int[] expectedList = new int[] { 1, 2, 3, 104, 5, 106, 7, 108, -1, 10 };
        int expectedSize = size - 1;
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            int value1 = record.getInt(binName1);
            int value2 = record.getInt(binName2);
            console.info("Record found: ns=%s set=%s bin1=%s value1=%s bin2=%s value2=%s", key.namespace, key.setName, binName1, value1, binName2, value2);
            int val1 = value1;
            if (val1 == 9) {
                console.error("Data mismatch. value1 " + val1 + " should not exist");
                break;
            }
            if (val1 == 5) {
                if (value2 != 0) {
                    console.error("Data mismatch. value2 " + value2 + " should be null");
                    break;
                }
            } else if (value1 != expectedList[value2 - 1]) {
                console.error("Data mismatch. Expected " + expectedList[value2 - 1] + ". Received " + value1);
                break;
            }
            count++;
        }
        if (count != expectedSize) {
            console.error("Query count mismatch. Expected " + expectedSize + ". Received " + count);
        }
    } finally {
        rs.close();
    }
}
Also used : Statement(com.aerospike.client.query.Statement) Record(com.aerospike.client.Record) RecordSet(com.aerospike.client.query.RecordSet) Key(com.aerospike.client.Key)

Example 70 with Key

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

the class QueryExecute method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName1, String binName2, int size) throws Exception {
    console.info("Write " + size + " records.");
    for (int i = 1; i <= size; i++) {
        Key key = new Key(params.namespace, params.set, keyPrefix + i);
        client.put(params.writePolicy, key, new Bin(binName1, i), new Bin(binName2, i));
    }
}
Also used : Bin(com.aerospike.client.Bin) 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