-
Notifications
You must be signed in to change notification settings - Fork 157
RiakClient & Cluster Node Builders (v2.0)
Note: This document is for the 2.x Java Client series.
Client with single Node
Client with multiple Nodes
Client with multiple Nodes and custom settings
Building a RiakClient object with a single node.
import java.net.UnknownHostException;
public class RiakClusters {
public static void main(String [] args) throws UnknownHostException {
// Riak Client with supplied IP and Port
RiakClient client = RiakClient.newClient(8087, "172.16.1.34");
client.shutdown();
}
}
By specifying multiple nodes you can get fail-over on operation failures (retry failed operation on different node), and round-robin balancing of operations across your nodes.
When performing an operation, a RiakCluster object internal to the client will retry an operation on a different node if it encounters a recoverable error. The object will try an operation 3 times by default before reporting a failure.
import java.net.UnknownHostException;
public class RiakClusters {
public static void main(String [] args) throws UnknownHostException {
// Riak Client with multiple node connections
LinkedList<String> ipAddresses = new LinkedList<String>();
ipAddresses.add("172.16.1.34");
ipAddresses.add("172.16.1.35");
ipAddresses.add("172.16.1.36");
RiakClient myNodeClient = RiakClient.newClient(8087, ipAddresses);
myNodeClient.shutdown();
}
}
By building a List of RiakNodes, and then building a RiakCluster object we can specify custom min and max connection #'s, and retry # settings.
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakNode;
import java.net.UnknownHostException;
public class RiakClusters {
public static void main(String [] args) throws UnknownHostException {
// Riak Client with multiple nodes and custom settings
// Keep 10-50 connections to each node.
RiakNode.Builder nodeTemplate = new RiakNode.Builder()
.withMinConnections(10)
.withMaxConnections(50);
// Create a list of 3 nodes to connect to.
LinkedList<RiakNode> nodes = new LinkedList<RiakNode>();
nodes.add(nodeTemplate.withRemoteAddress("172.16.1.34").withRemotePort(8087).build());
nodes.add(nodeTemplate.withRemoteAddress("172.16.1.35").withRemotePort(8087).build());
nodes.add(nodeTemplate.withRemoteAddress("172.16.1.36").withRemotePort(8087).build());
// Increase the # of execution attempts (retries) to 5, from the default of 3
RiakCluster myCluster = RiakCluster.builder(nodes)
.withExecutionAttempts(5)
.build();
// RiakCluster must be started before given to RiakClient ctor.
myCluster.start();
RiakClient myCustomClient = new RiakClient(myCluster);
myCustomClient.shutdown();
}
}
You can visualize the RiakNode, RiakCluster, and RiakClient objects working together with this graph: