Skip to main content

Connecting


To connect to an Aerospike database, create an AerospikeClient object that specifies the IP address and port of one or more seed nodes in the cluster.

Single Seed Node

The client first connects to a seed node, and then discovers the rest of the cluster.

import com.aerospike.client.AerospikeClient;

AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

Multiple Seed Nodes

Multiple seed nodes can also be provided. The client iterates through the array of nodes until it successfully connects to a node. It then discovers all nodes in the cluster.

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)
};

AerospikeClient client = new AerospikeClient(new ClientPolicy(), hosts);

User Authentication

Aerospike enterprise servers may require user 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);

TLS Secured Connection

TLS connections require certificate configuration on both client and server. This knowledge base article explains how to configure the server for TLS connections. The TLS connection port is usually changed to 4333 instead of the typically unsecured port 3000.

The client TLS name ("clusterTLSName" in this example) should be equal to either the certificate common name (CN) or the certificate subject alternative name. Usually, the client TLS name is also the server's "tls-name" configuration value.

The TlsPolicy can also be configured to limit protocols/ciphers and revoke certificates. See documentation for client TLS configuration options.

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Host;
import com.aerospike.client.policy.ClientPolicy;

Host host = new Host("192.168.10.10", "clusterTLSName", 4333);

TlsPolicy tlsPolicy = new TlsPolicy();

ClientPolicy policy = new ClientPolicy();
policy.tlsPolicy = tlsPolicy;

AerospikeClient client = new AerospikeClient(policy, host);

TLS secured connection with PKI authentication

TLS connections can be extended with PKI authentication. PKI user authentication specifies that the username is located directly in the client certificate's common name (CN). PKI requires that mutual authentication (both client and server validate each other's certificates) is configured on the server. The server must examine the client's certificate to obtain the username.

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Host;
import com.aerospike.client.policy.ClientPolicy;

Host host = new Host("192.168.10.10", "clusterTLSName", 4333);

TlsPolicy tlsPolicy = new TlsPolicy();

ClientPolicy policy = new ClientPolicy();
policy.tlsPolicy = tlsPolicy;
policy.authMode = AuthMode.PKI;

AerospikeClient client = new AerospikeClient(policy, host);

Compression

You can enable data compression on both the client side and the server side. The Java client uses Zstandard (ZLib) for compression. For information about configuring compression on the server side, see Configuring Storage Compression.

note

Use of compression requires Aerospike Database Enterprise Edition.

AerospikeClient client = new AerospikeClient("localhost", 3000);

String sighting = "{\"sighting\": {\"occurred\": 20200912, \"reported\": 20200916, \"posted\": 20201105, \"report\": {\"city\": \"Kirkland\", \"duration\": \"~30 minutes\", \"shape\": [\"circle\"], \"state\": \"WA\", \"summary\": \"4 rotating orange lights in the Kingsgate area above the Safeway. Around 9pm the power went out in the Kingsgate area. Four lights were spotted rotating above the local Safeway and surrounding streets. They were rotating fast but staying relatively in the same spots. Also described as orange lights. About thirty minutes later they disappeared. The second they disappeared the power was restored. Later a station of police from Woodinville and Kirkland came to guard the street where it happened. They wouldn't let anyone go past the street, putting out search lights and flare signals so people couldn't drive past Safeway. The police also would not let people walk past to go home.\"}, \"location\": \"\\\"{\\\"type\\\":\\\"Point\\\",\\\"coordinates\\\":[-122.1966441,47.69328259]}\\\"\"}}";

Key compressExampleKey = new Key("sandbox", "ufo", Value.get(1));

// Create write policy and enable compression on the policy
WritePolicy policy = client.copyWritePolicyDefault();
policy.compress = true;

client.put(policy, compressExampleKey, new Bin("sighting", Value.get(sighting)));

// Create read policy and enable compression on the policy
Policy readPolicy = client.copyReadPolicyDefault();
readPolicy.compress = true;
client.get(readPolicy, compressExampleKey);

Maintenance Thread

The AerospikeClient constructor creates a maintenance thread that periodically pings nodes for cluster status. If a network disturbance is detected and the client can‘t reach any nodes, the seed nodes are used until client-server connection is reestablished.

The AerospikeClient instance is thread-safe and can be used concurrently. Connections are cached with connection pool(s) for each server node.

Cleanup

When all commands complete and the application is prepared for a clean shutdown, call the close() method to remove resources held by the AerospikeClient object.

client.close();
note

The AerospikeClient object cannot be used after calling close().