WordCount Client
Appending lines into the Topic
The client API gives us access to a Grainite client which can be used to perform various actions like appending to a topic and writing data to a grain:
Implement the WordCount client
As with the handler classes from the previous step, GX generated a Client
class that will allow us to work with the Grainite client. In the following steps, we will complete Client.java
to utilize the application logic we defined in the previous step through the Grainite client.
Step 0: Initiliazing Client.java
For this application, you will need to replace the contents of Client.java
with skeleton code that has been provided for you. Copy the contents of the template file, in samples/wordcount/templates/Client.java
, and paste them into your Client.java
file, located in my-wordcount/src/main/java/org/sample/wordcount
.
Step 1: Loading input files
Now, you will complete the load
method in Client.java
to parse lines from an input file and append each as an Event in line_topic
.
For this application, you are using the function append()
to push events into the topic synchronously, but the same can be done asynchronously using appendAsync()
.
Step 2: Getting word count
Here, you will the getWordCount
method in Client.java
. To get the word count for a specific word, you will first need to get a reference to the grain that contains it. Then, you can use mapGet()
to access the map containing the word you want and get the value stored for that word.
Step 3: Getting doc stats
Getting the document stats involves similar steps to those of getting a word count. The only difference is that now we are fetching data from the grain's value, and not from its map. You can use getValue()
to access the grain's value.
Step 4: Get all word counts in alphabetical order
To get the words in sorted order, you will first have to fetch the grains in sorted order. You will do this by querying from the table using all the keys (which we previously defined as the first letter of the corresponding word). If the grain exists, you will use mapQuery
to scan for all the words in the grain.
If the grain exists, you will use mapGetRange
to scan for all the words in the grain.
Last updated