public class RiakClient extends Object
The core of the Java client models a Riak cluster:
The easiest way to get started with the client API is using one of the static methods provided to instantiate and start the client:
RiakClient client =
RiakClient.newClient("192.168.1.1","192.168.1.2","192.168.1.3");
Note that the Riak Java client uses the Riak Protocol Buffers API exclusively.
For more complex configurations you will instantiate one or more RiakNode
s
and build a RiakCluster
to supply to the
RiakClient constructor.
RiakNode.Builder builder = new RiakNode.Builder();
builder.withMinConnections(10);
builder.withMaxConnections(50);
List<String> addresses = new LinkedList<String>();
addresses.add("192.168.1.1");
addresses.add("192.168.1.2");
addresses.add("192.168.1.3");
List<RiakNode> nodes = RiakNode.Builder.buildNodes(builder, addresses);
RiakCluster cluster = new RiakCluster.Builder(nodes).build();
cluster.start();
RiakClient client = new RiakClient(cluster);
Once you have a client, RiakCommands from the com.basho.riak.client.api.commands.* packages are built then executed by the client:
Namespace ns = new Namespace("default","my_bucket");
Location loc = new Location(ns, "my_key");
FetchValue fv = new FetchValue.Builder(loc).build();
FetchValue.Response response = client.execute(fv);
RiakObject obj = response.getValue(RiakObject.class);
You can also execute all RiakCommands asynchronously. A
RiakFuture
for the operation is immediately returned:
Namespace ns = new Namespace("default","my_bucket");
Location loc = new Location(ns, "my_key");
FetchValue fv = new FetchValue.Builder(loc).build();
RiakFuture<FetchValue.Response, Location> future = client.executeAsync(fv);
future.await();
if (future.isSuccess())
{
FetchValue.Response response = future.getNow();
RiakObject obj = response.getValue(RiakObject.class);
...
}
else
{
Throwable error = future.cause();
...
}
Constructor and Description |
---|
RiakClient(RiakCluster cluster)
Create a new RiakClient to perform operations on the given cluster.
|
Modifier and Type | Method and Description |
---|---|
<T,S> T |
execute(RiakCommand<T,S> command)
Execute a RiakCommand synchronously.
|
<T,S> RiakFuture<T,S> |
executeAsync(RiakCommand<T,S> command)
Execute a RiakCommand asynchronously.
|
static RiakClient |
newClient()
Static factory method to create a new client instance.
|
static RiakClient |
newClient(InetSocketAddress... addresses)
Static factory method to create a new client instance.
|
static RiakClient |
newClient(int port,
List<String> remoteAddresses)
Static factory method to create a new client instance.
|
static RiakClient |
newClient(int port,
String... remoteAddresses)
Static factory method to create a new client instance.
|
static RiakClient |
newClient(List<String> remoteAddresses)
Static factory method to create a new client instance.
|
static RiakClient |
newClient(String... remoteAddresses)
Static factory method to create a new client instance.
|
Future<Boolean> |
shutdown()
Shut down the client and the underlying RiakCluster.
|
public RiakClient(RiakCluster cluster)
The RiakClient provides a user API on top of the client core. Once instantiated, commands are submitted to it for execution on Riak.
cluster
- the started RiakCluster to use.public static RiakClient newClient() throws UnknownHostException
UnknownHostException
public static RiakClient newClient(int port, String... remoteAddresses) throws UnknownHostException
remoteAddresses
- a list of IP addresses or hostnamesport
- the (protocol buffers) port to connect to on the supplied hosts.UnknownHostException
- if a supplied hostname cannot be resolved.public static RiakClient newClient(List<String> remoteAddresses) throws UnknownHostException
remoteAddresses
- a list of IP addresses or hostnamesUnknownHostException
- if a supplied hostname cannot be resolved.public static RiakClient newClient(String... remoteAddresses) throws UnknownHostException
remoteAddresses
- a list of IP addresses or hostnamesUnknownHostException
- if a supplied hostname cannot be resolved.public static RiakClient newClient(int port, List<String> remoteAddresses) throws UnknownHostException
remoteAddresses
- a list of IP addresses or hostnamesport
- the (protocol buffers) port to connect to on the supplied hosts.UnknownHostException
- if a supplied hostname cannot be resolved.public static RiakClient newClient(InetSocketAddress... addresses) throws UnknownHostException
addresses
- one or more addresses to connect to.UnknownHostException
- if a supplied hostname cannot be resolved.public <T,S> T execute(RiakCommand<T,S> command) throws ExecutionException, InterruptedException
Calling this method causes the client to execute the provided RiakCommand synchronously. It will block until the operation completes then either return the response on success or throw an exception on failure.
T
- The RiakCommand's return type.S
- The RiakCommand's query info type.command
- The RiakCommand to execute.ExecutionException
- if the command fails for any reason.InterruptedException
public <T,S> RiakFuture<T,S> executeAsync(RiakCommand<T,S> command)
Calling this method causes the client to execute the provided RiakCommand asynchronously. It will immediately return a RiakFuture that contains the running operation.
T
- RiakCommand's return type.S
- The RiakCommand's query info type.command
- The RiakCommand to execute.RiakFuture
public Future<Boolean> shutdown()
The underlying client core (RiakCluster) uses a number of threads as does Netty. Calling this method will shut down all those threads cleanly. Failure to do so may prevent your application from exiting.
Copyright © 2014. All rights reserved.