Search in sources :

Example 31 with Record

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

the class OperateBit method runSimpleExample.

/**
 * Simple example of bit functionality.
 */
public void runSimpleExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "bitkey");
    String binName = params.getBinName("bitbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    byte[] bytes = new byte[] { 0b00000001, 0b00000010, 0b00000011, 0b00000100, 0b00000101 };
    client.put(params.writePolicy, key, new Bin(binName, bytes));
    // Set last 3 bits of bitmap to true.
    Record record = client.operate(params.writePolicy, key, BitOperation.set(BitPolicy.Default, binName, -3, 3, new byte[] { (byte) 0b11100000 }), Operation.get(binName));
    List<?> list = record.getList(binName);
    byte[] val = (byte[]) list.get(1);
    for (byte b : val) {
        console.info(Byte.toString(b));
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 32 with Record

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

the class OperateMap method runSimpleExample.

/**
 * Simple example of map operate functionality.
 */
public void runSimpleExample(AerospikeClient client, Parameters params) {
    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(1), Value.get(55));
    inputMap.put(Value.get(2), Value.get(33));
    // Write values to empty map.
    Record record = client.operate(params.writePolicy, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    console.info("Record: " + record);
    // Pop value from map and also return new size of map.
    record = client.operate(params.writePolicy, key, MapOperation.removeByKey(binName, Value.get(1), MapReturnType.VALUE), MapOperation.size(binName));
    console.info("Record: " + record);
    // There should be one result for each map operation on the same map bin.
    // In this case, there are two map operations (pop and size), so there
    // should be two results.
    List<?> list = record.getList(binName);
    for (Object value : list) {
        console.info("Received: " + value);
    }
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 33 with Record

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

the class OperateMap method runNestedMapCreateExample.

public void runNestedMapCreateExample(AerospikeClient client, Parameters params) {
    Key key = new Key(params.namespace, params.set, "mapkey2");
    String binName = params.getBinName("mapbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    Map<Value, Value> m1 = new HashMap<Value, Value>();
    m1.put(Value.get("key21"), Value.get(7));
    m1.put(Value.get("key22"), Value.get(6));
    Map<Value, Value> m2 = new HashMap<Value, Value>();
    m2.put(Value.get("a"), Value.get(3));
    m2.put(Value.get("c"), Value.get(5));
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("key1"), Value.get(m1));
    inputMap.put(Value.get("key2"), Value.get(m2));
    // Create maps.
    client.put(params.writePolicy, key, new Bin(binName, inputMap));
    // Create key ordered map at "key2" only if map does not exist.
    // Set map value to 4 for map key "key21" inside of map key "key2".
    CTX ctx = CTX.mapKey(Value.get("key2"));
    Record record = client.operate(params.writePolicy, key, MapOperation.create(binName, MapOrder.KEY_VALUE_ORDERED, ctx), MapOperation.put(MapPolicy.Default, binName, Value.get("b"), Value.get(4), ctx), Operation.get(binName));
    record = client.get(params.policy, key);
    console.info("Record: " + record);
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) CTX(com.aerospike.client.cdt.CTX) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 34 with Record

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

the class OperateMap method runListRangeExample.

/**
 * Value list range example.
 */
public void runListRangeExample(AerospikeClient client, Parameters params) {
    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);
    List<Value> l1 = new ArrayList<Value>();
    l1.add(Value.get(new GregorianCalendar(2018, 1, 1).getTime()));
    l1.add(Value.get(1));
    List<Value> l2 = new ArrayList<Value>();
    l2.add(Value.get(new GregorianCalendar(2018, 1, 2).getTime()));
    l2.add(Value.get(2));
    List<Value> l3 = new ArrayList<Value>();
    l3.add(Value.get(new GregorianCalendar(2018, 2, 1).getTime()));
    l3.add(Value.get(3));
    List<Value> l4 = new ArrayList<Value>();
    l4.add(Value.get(new GregorianCalendar(2018, 2, 2).getTime()));
    l4.add(Value.get(4));
    List<Value> l5 = new ArrayList<Value>();
    l5.add(Value.get(new GregorianCalendar(2018, 2, 5).getTime()));
    l5.add(Value.get(5));
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("Charlie"), Value.get(l1));
    inputMap.put(Value.get("Jim"), Value.get(l2));
    inputMap.put(Value.get("John"), Value.get(l3));
    inputMap.put(Value.get("Harry"), Value.get(l4));
    inputMap.put(Value.get("Bill"), Value.get(l5));
    // Write values to empty map.
    Record record = client.operate(params.writePolicy, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    console.info("Record: " + record);
    List<Value> end = new ArrayList<Value>();
    end.add(Value.get(new GregorianCalendar(2018, 2, 2).getTime()));
    end.add(Value.getAsNull());
    // Delete values < end.
    record = client.operate(params.writePolicy, key, MapOperation.removeByValueRange(binName, null, Value.get(end), MapReturnType.COUNT));
    console.info("Record: " + record);
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) GregorianCalendar(java.util.GregorianCalendar) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 35 with Record

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

the class OperateMap method runScoreExample.

/**
 * Map score example.
 */
public void runScoreExample(AerospikeClient client, Parameters params) {
    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.increment(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)

Aggregations

Record (com.aerospike.client.Record)259 Key (com.aerospike.client.Key)140 Test (org.junit.Test)139 ArrayList (java.util.ArrayList)75 Bin (com.aerospike.client.Bin)70 AerospikeException (com.aerospike.client.AerospikeException)62 Value (com.aerospike.client.Value)54 WritePolicy (com.aerospike.client.policy.WritePolicy)47 List (java.util.List)41 HashMap (java.util.HashMap)36 ThrowingRunnable (org.junit.function.ThrowingRunnable)32 BatchPolicy (com.aerospike.client.policy.BatchPolicy)29 Policy (com.aerospike.client.policy.Policy)29 Statement (com.aerospike.client.query.Statement)23 RecordSet (com.aerospike.client.query.RecordSet)22 Expression (com.aerospike.client.exp.Expression)18 Map (java.util.Map)15 HLLValue (com.aerospike.client.Value.HLLValue)14 Collections.singletonList (java.util.Collections.singletonList)9 MapPolicy (com.aerospike.client.cdt.MapPolicy)7