Skip to main content
Loading

Java Client

When creating Java client applications to work with Aerospike Cloud, use the client object com.aerospike.client.proxy.AerospikeClientProxy. If you have existing Java client applications, you can modify them to work with Aerospike Cloud with the following factory code.

Using AerospikeFactory.java

A simple implementation of the standard factory pattern is included with com.aerospike.client.proxy.AerospikeClientProxy. This factory returns a class object adhering to the interface IAerospikeClient. Depending on the boolean value isProxy, it returns either a native Client object (com.aerospike.client.AerospikeClient) for use with locally hosted Aerospike deployments, or a proxy client (com.aerospike.client.proxy.AerospikeClientProxy) for use with Cloud deployments.

/*
* Copyright 2012-2023 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.aerospike.client.proxy;
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Host;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.policy.ClientPolicy;

/*
* Factory class AerospikeClientFactory generates either a native client or
* a proxy client, based on whether isProxy is true or false. You can use
* this factory class along with your own logic to select native
* or proxy to create an application that automatically works with both the
* native and proxy-based versions of Aerospike.
*/

public class AerospikeClientFactory {

/*
* Return either a native Aerospike client or a proxy client, based on isProxy.
*
* @param clientPolicy clientPolicy to pass to Client constructor
* @param hosts Host array to pass to Client constructor
* @param isProxy If true, return AerospikeClientProxy - otherwise return AerospikeClient
* @return IAerospikeClient
*/

public static IAerospikeClient getClient(ClientPolicy clientPolicy, boolean isProxy, Host[] hosts) {
if (isProxy) {
return new AerospikeClientProxy(clientPolicy, hosts);
}
else {
return new AerospikeClient(clientPolicy, hosts);
}
}
}

Main.java

When the main program calls the factory, it returns the appropriate client object based on the value of the boolean isProxy. You can replace the simple boolean logic with more sophisticated selection logic if necessary.

Replace the placeholder values in the following code with the correct values from your Aerospike Cloud instance.

// Import the Aerospike client library and other modules
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import com.aerospike.client.proxy.AerospikeClientFactory;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.policy.Policy;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.policy.ClientPolicy;
import com.aerospike.client.policy.TlsPolicy;
import com.aerospike.client.Host;
import com.aerospike.client.Key;
import com.aerospike.client.Bin;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Record;
import com.aerospike.client.cdt.MapOrder;

public class Main {
public static void main(String[] args) {


ClientPolicy clientPolicy = new ClientPolicy();
clientPolicy.user = "<<YOUR_USERID_HERE>>";
clientPolicy.password = "<<YOUR_KEY_HERE>>";

clientPolicy.tlsPolicy = new TlsPolicy();

Policy policy = new Policy();
policy.sendKey = true;
WritePolicy writePolicy = new WritePolicy(policy);

final String NAMESPACE = "aerospike_cloud";
final String SET_NAME = "TestSet1";
Boolean isProxy = true;


Host[] hosts = new Host[1];
hosts[0] = new Host("6335ce8b-f94f-4024-af20-9c3b0a46d78f.stage.aerospike-cloud.net", 4000);

// Call factory code to create a new client. This should work for both native and proxy clients
IAerospikeClient client = AerospikeClientFactory.getClient(clientPolicy, isProxy, hosts);


// Create a key in namespace "sandbox" and set "ufodata"
Key key = new Key(NAMESPACE, SET_NAME, 5001);

// Create a list of shapes to add to the report map
ArrayList<String> shape = new ArrayList<String>();
shape.add("circle");
shape.add("flash");
shape.add("disc");

// Create the report map
Map<String, Object> reportMap = new TreeMap<String, Object>();
reportMap.put("city", "Ann Arbor");
reportMap.put("state", "Michigan");
reportMap.put("shape", shape);
reportMap.put("duration", "5 minutes");
reportMap.put("summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!");

// Create the bins as Bin("binName", value)
Bin occurred = new Bin("occurred", 20220531);
Bin reported = new Bin("reported", 20220601);
Bin posted = new Bin("posted", 20220601);
Bin report = new Bin("report", reportMap, MapOrder.KEY_ORDERED);

// Write the record to Aerospike
try {
client.put(writePolicy, key, occurred, reported, posted, report);
Record record = client.get(null, key);
System.out.format("Create succeeded\nKey: %s\n", key.userKey);
}
catch (AerospikeException ae) {
System.out.format("Create Failed\nError: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
}
}