Search in sources :

Example 1 with BatchListListener

use of com.aerospike.client.listener.BatchListListener in project aerospike-client-java by aerospike.

the class AsyncBatch method batchReadComplex.

/**
	 * Read records with varying namespaces, bin names and read types in one batch.
	 * This requires Aerospike Server version >= 3.6.0
	 */
private void batchReadComplex() throws Exception {
    // Batch gets into one call.
    // Batch allows multiple namespaces in one call, but example test environment may only have one namespace.
    String[] bins = new String[] { binName };
    List<BatchRead> records = new ArrayList<BatchRead>();
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 1), bins));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 2), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 3), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 4), false));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 5), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 6), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 7), bins));
    // This record should be found, but the requested bin will not be found.
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 8), new String[] { "binnotfound" }));
    // This record should not be found.
    records.add(new BatchRead(new Key(params.namespace, params.set, "keynotfound"), bins));
    // Execute batch.
    client.get(eventLoop, new BatchListListener() {

        public void onSuccess(List<BatchRead> records) {
            // Show results.
            int found = 0;
            for (BatchRead record : records) {
                Key key = record.key;
                Record rec = record.record;
                if (rec != null) {
                    found++;
                    console.info("Record: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, rec.getValue(binName));
                } else {
                    console.info("Record not found: ns=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, binName);
                }
            }
            if (found != 8) {
                console.error("Records found mismatch. Expected %d. Received %d.", 8, found);
            }
            taskComplete();
        }

        public void onFailure(AerospikeException e) {
            console.error("Batch read complex failed: " + Util.getErrorMessage(e));
            taskComplete();
        }
    }, null, records);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) BatchListListener(com.aerospike.client.listener.BatchListListener) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 2 with BatchListListener

use of com.aerospike.client.listener.BatchListListener in project aerospike-client-java by aerospike.

the class TestAsyncBatch method asyncBatchReadComplex.

@Test
public void asyncBatchReadComplex() throws Exception {
    // Batch gets into one call.
    // Batch allows multiple namespaces in one call, but example test environment may only have one namespace.
    String[] bins = new String[] { binName };
    List<BatchRead> records = new ArrayList<BatchRead>();
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 1), bins));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 2), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 3), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 4), false));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 5), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 6), true));
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 7), bins));
    // This record should be found, but the requested bin will not be found.
    records.add(new BatchRead(new Key(args.namespace, args.set, keyPrefix + 8), new String[] { "binnotfound" }));
    // This record should not be found.
    records.add(new BatchRead(new Key(args.namespace, args.set, "keynotfound"), bins));
    // Execute batch.
    client.get(eventLoop, new BatchListListener() {

        public void onSuccess(List<BatchRead> records) {
            // Show results.
            int found = 0;
            int count = 0;
            for (BatchRead record : records) {
                Record rec = record.record;
                count++;
                if (rec != null) {
                    found++;
                    Object value = rec.getValue(binName);
                    if (count != 4 && count <= 7) {
                        if (!assertEquals(valuePrefix + count, value)) {
                            notifyComplete();
                            return;
                        }
                    } else {
                        if (!assertNull(value)) {
                            notifyComplete();
                            return;
                        }
                    }
                }
            }
            assertEquals(8, found);
            notifyComplete();
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, records);
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) BatchListListener(com.aerospike.client.listener.BatchListListener) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) Test(org.junit.Test)

Aggregations

AerospikeException (com.aerospike.client.AerospikeException)2 BatchRead (com.aerospike.client.BatchRead)2 Key (com.aerospike.client.Key)2 Record (com.aerospike.client.Record)2 BatchListListener (com.aerospike.client.listener.BatchListListener)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)1