Search in sources :

Example 76 with Key

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

the class QueryRegion method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName) throws Exception {
    StringBuilder rgnsb = new StringBuilder();
    rgnsb.append("{ ");
    rgnsb.append("    \"type\": \"Polygon\", ");
    rgnsb.append("    \"coordinates\": [ ");
    rgnsb.append("        [[-122.500000, 37.000000],[-121.000000, 37.000000], ");
    rgnsb.append("         [-121.000000, 38.080000],[-122.500000, 38.080000], ");
    rgnsb.append("         [-122.500000, 37.000000]] ");
    rgnsb.append("    ] ");
    rgnsb.append(" } ");
    console.info("QueryRegion for: ns=%s set=%s index=%s bin=%s within %s", params.namespace, params.set, indexName, binName, rgnsb);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.geoWithinRegion(binName, rgnsb.toString()));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            String result = record.getGeoJSON(binName);
            console.info("Record found: ns=%s set=%s bin=%s digest=%s value=%s", key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), result);
            count++;
        }
        if (count != 6) {
            console.error("Query count mismatch. Expected 6. 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 77 with Key

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

the class QueryRegionFilter 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 = 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 bin1 = Bin.asGeoJSON(binName1, ptsb.toString());
        Bin bin2;
        if (i % 7 == 0) {
            bin2 = new Bin(binName2, "hospital");
        } else if (i % 2 == 0) {
            bin2 = new Bin(binName2, "school");
        } else {
            bin2 = new Bin(binName2, "store");
        }
        client.put(params.writePolicy, key, bin1, bin2);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 78 with Key

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

the class QueryString method runQuery.

private void runQuery(AerospikeClient client, Parameters params, String indexName, String binName, String valuePrefix) throws Exception {
    String filter = valuePrefix + 3;
    console.info("Query for: ns=%s set=%s index=%s bin=%s filter=%s", params.namespace, params.set, indexName, binName, filter);
    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setBinNames(binName);
    stmt.setFilter(Filter.equal(binName, filter));
    RecordSet rs = client.query(null, stmt);
    try {
        int count = 0;
        while (rs.next()) {
            Key key = rs.getKey();
            Record record = rs.getRecord();
            String result = record.getString(binName);
            if (result.equals(filter)) {
                console.info("Record found: ns=%s set=%s bin=%s key=%s value=%s", key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), result);
            } else {
                console.error("Query mismatch: Expected %s. Received %s.", filter, result);
            }
            count++;
        }
        if (count == 0) {
            console.error("Query failed. No records returned.");
        }
    } 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 79 with Key

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

the class QuerySum method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, 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, 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 80 with Key

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

the class Serialize method testArray.

/**
 * Write array of integers using standard java serializer..
 */
public void testArray(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "serialarraykey");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Initialize array");
    int[] array = new int[10000];
    for (int i = 0; i < 10000; i++) {
        array[i] = i * i;
    }
    Bin bin = new Bin(params.getBinName("serialbin"), array);
    // Do a test that pushes this complex object through the serializer
    console.info("Write array using serializer.");
    client.put(params.writePolicy, key, bin);
    console.info("Read array using serializer.");
    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));
    }
    int[] received;
    try {
        received = (int[]) record.getValue(bin.name);
    } catch (Exception e) {
        throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
    }
    if (received.length != 10000) {
        throw new Exception(String.format("Array length mismatch: Expected=%d Received=%d", 10000, received.length));
    }
    for (int i = 0; i < 10000; i++) {
        if (received[i] != i * i) {
            throw new Exception(String.format("Mismatch: index=%d expected=%d received=%d", i, i * i, received[i]));
        }
    }
    console.info("Read array successful.");
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) 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