I. AeroSpike configuration
1. Configuration file location: / etc/aerospike/aerospike.conf
2. Notes:
<1> The bin key name limits the number of bytes to no more than 14 bytes
<2> Each node in the cluster has a configuration file aerospike.conf, and the namespace configuration parameters in each node configuration file must be consistent
< 3 > replication factor is a configuration parameter that cannot exceed the number of cluster nodes. The more copies, the higher the reliability, but the higher the write request that must go through all the data copies. In practice, most deployments use a data factor of 2 (one master data and one copy)
Configuration of aerospike.conf:
# Aerospike database configuration file for use with systemd.
service {
paxos-single-replica-limit 1 # Number of nodes where the replica count is automatically reduced to 1.
service-threads 4
transaction-queues 4
transaction-threads-per-queue 4
proto-fd-max 15000
}
logging {
file /var/log/aerospike/aerospike.log {
context any info
}
}
network {
service {
address any
port 3000
}
heartbeat {
mode multicast
multicast-group 239.1.99.222
port 9918
# To use unicast-mesh heartbeats, remove the 3 lines above, and see
# aerospike_mesh.conf for alternative.
interval 150
timeout 10
}
fabric {
port 3001
}
info {
port 3003
}
}
namespace test {
replication-factor 2
memory-size 4G
default-ttl 30d # 30 days, use 0 to never expire/evict.
storage-engine memory
}
namespace default {
replication-factor 2
memory-size 4G
default-ttl 30d # 30 days, use 0 to never expire/evict.
storage-engine memory
# To use file storage backing, comment out the line above and use the
# following lines instead.
# storage-engine device {
# file /opt/aerospike/data/bar.dat
# filesize 16G
# data-in-memory true # Store data in memory in addition to file.
# }
}
Java Client Operation
1. Connect to AeroSpike Cluster
(1) Cluster discovery by connecting a node
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
(2) Cluster discovery by configuring multiple nodes
Host[] hosts = new Host[] {
new Host("a.host", 3000),
new Host("another.host", 3000),
new Host("and.another.host", 3000)
};
AerospikeClient client = new AerospikeClient(new ClientPolicy(), hosts);
- The advantage of the second connection over the first one is that the host configuration is read sequentially by the second connection until it is connected to the cluster and stopped reading. In the first connection, if the host is unresponsive, it will not be able to connect to the cluster.
(3) Connections with privileged authentication
import com.aerospike.client.AerospikeClient
import com.aerospike.client.Host
import com.aerospike.client.policy.ClientPolicy
Host[] hosts = new Host[] {
new Host("a.host", 3000),
new Host("another.host", 3000),
new Host("and.another.host", 3000)
}
ClientPolicy policy = new ClientPolicy()
policy.user = "myuser"
policy.password = "mypass"
AerospikeClient client = new AerospikeClient(policy, hosts)
2. Writing Records
WritePolicy policy = new WritePolicy();
policy.timeout = 50;
Single Value Writing
Key key = new Key("test", "myset", "mykey");
Bin bin = new Bin("mybin", "myvalue");
client.put(policy, key, bin);
Multivalue Writing
Key key = new Key("test", "myset", "mykey");
Bin bin1 = new Bin("name", "John");
Bin bin2 = new Bin("age", 25);
client.put(policy, key, bin1, bin2);
Delete values (using asNull)
Key key = new Key("test", "myset", "mykey");
Bin bin1 = Bin.asNull(binName1); // Set bin value to null to drop bin.
client.put(policy, key, bin1);
3. Reading Records
Read all field information for the specified Key
Record record = client.get(policy, key);
Read specific field information for the specified Key
Record record = client.get(policy, key, "name", "age");
4. Delete records
Key key = new Key("test", "myset", "mykey");
client.delete(policy, key);
5. Batch reading of multiple Key records
Key[] keys = new Key[size];
for (int i = 0; i < size; i++) {
keys[i] = new Key("test", "myset", (i + 1));
}
Record[] records = client.get(policy, keys);
6. Scanning records, i.e. acquiring all records
public final class ScanParallelTest implements ScanCallback {
public static void main(String[] args) {
try {
new ScanParallelTest().runTest();
}
catch (Exception e) {
e.printStackTrace();
}
}
private int recordCount;
public void runTest() throws AerospikeException {
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
try {
ScanPolicy policy = new ScanPolicy();
policy.concurrentNodes = true;
policy.priority = Priority.LOW;
policy.includeBinData = false;
client.scanAll(policy, "test", "demoset", this);
System.out.println("Records " + recordCount);
}
finally {
client.close();
}
}
public void scanCallback(Key key, Record record) {
recordCount++;
if ((recordCount % 10000) == 0) {
System.out.println("Records " + recordCount);
}
}
}