Comment on page
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.
Java
Python
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"));
Client.py
# Connect to Grainite server on given host and ports - obtain these from
# your Grainite administrator.
client = grainite_client.get_client(server_ip, data_port)
# Get a handle to the Customer Table in Grainite.
table = client.get_table(Constants.APP_NAME, Constants.CUSTOMER_TABLE_NAME)
# Get a handle to a Grain for customer id 123.
grain = table.get_grain(Key("123"))
# Set the value as the name for the customer.
grain.set_value(Value("John Smith"))
Java
Python
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();
Client.py
# Get a Grainite Client.
client = grainite_client.get_client(...)
# Get a handle to the Food Topic.
topic = client.get_topic(Constants.APP_NAME, Constants.FOOD_TOPIC_NAME)
# Append entries to the topic against Key "fruits".
entries_to_append = ["Apples", "Bananas", "Oranges", "Pineapples", "Pears"]
for fruit_name in entries_to_append:
topic.append_async(Event(Key("fruits"), Value(fruit_name)))
# Flush any pending updates to the topic after sending a batch of appends asynchronously.
topic.flush()
Java
Python
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()) {
...
}
Client.py
# Get a Grainite Client.
client = grainite_client.get_client(...)
# Get a handle to the Customer Table in Grainite.
table = client.get_table(Constants.APP_NAME, Constants.CUSTOMER_TABLE_NAME)
# Get a handle to the Grain.
grain = client.get_grain(Key("123"))
# Invoke the "handle_client_invoke" action handler on this grain.
value = grain.invoke("handle_client_invoke", Value("input"))
# Do something with the return value.
if value.is_error():
...
Last modified 6mo ago