Client Application

Client applications may directly interact with Topics and Tables, and Grains within those Tables by invoking the Grainite client API.

Clients may append events to Topics, read previously created Topics, as well as, query or update data stored within Grains, and even directly invoke Action Handlers on any Grains using the Client API.

Head over to GrainiteClient API to get a more detailed view of the client API.

pageGrainiteClient API

Client Code Examples

Accessing and writing to a Grain

Client.java
// Connect to Grainite server on given host and ports - obtain these from 
// your Grainite administrator.
Grainite client = GrainiteClient.getClient(serverIP, dataPort);

// Get a handle to the Customer Table in Grainite.
Table table = client.getTable(Constants.APP_NAME, 
                              Constants.CUSTOMER_TABLE_NAME);

// Get a handle to a Grain for customer id 123.
Grain grain = table.getGrain(Key.of("123"));

// Set the value as the name for the customer.
grain.setValue(Value.of("John Smith"));

Appending to a Topic

Client.java
// Get a Grainite Client.
Grainite client = GrainiteClient.getClient(...);

// Get a handle to the Food Topic.
Topic topic = client.getTopic(Constants.APP_NAME, Constants.FOOD_TOPIC_NAME);

// Append entries to the topic against Key "fruits".
String[] entriesToAppend = new String[] 
    { "Apples", "Bananas", "Oranges", "Pineapples", "Pears" };

for (String fruitName: entriesToAppend) {
  topic.appendAsync(new Event(Key.of("fruits"), Value.of(fruitName)));
}

// Flush any pending updates to the topic after sending a batch of appends asynchronously.
topic.flush();

Invoking a Grain Action from a Client application

Client.java
// Get a Grainite Client.
Grainite client = GrainiteClient.getClient(...);

// Get a handle to the Customer Table in Grainite.
Table table = client.getTable(Constants.APP_NAME, 
                              Constants.CUSTOMER_TABLE_NAME);

// Get a handle to the Grain.
Grain grain = client.getGrain(Key.of("123"));

// Invoke the "handleClientInvoke" action handler on this grain.
ResultOrStatus<Value> value = grain.invoke("handleClientInvoke", 
                                           Value.of("input"));

// Do something with the return value.
if (!value.isError()) {
  ...
}

Last updated