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();
}
}
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);
}
}
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();
}
}
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);
}
}
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.");
}
Aggregations