Now that you have the Coherence data grid up and
running, the next step is to put some data into the grid and check if we
can retrieve it. After all, a data grid without any data is not a very
useful thing.
You will shortly learn how to
use the Coherence APIs to store and retrieve your application data from
the grid, but for now let's keep things simple and use the Coherence
command-line utility to perform basic grid operations.
Coherence console
Coherence ships with a
command-line utility that allows you to manipulate caches within a data
grid. Keep your grid nodes running and launch Coherence console by
executing the coherence.sh/cmd script within the bin directory.
For the most part the
resulting screen looks exactly the same as the screens for the
previously started Coherence nodes. As a matter of fact, the Coherence
console is a Coherence node, and you can see that it has joined the cluster as the third member.
The only significant difference is the prompt at the bottom of the screen:
This is where you can
enter various commands to create caches, put values into them and get
values from them, and perform many other operations. I will show you how
to perform some of those tasks in this section. If you would like to learn more, feel free to
consult the Coherence manuals for more details.
Warning: Use at your own risk
The Coherence console is a
development tool and should not be used in production. Many things can
go wrong, and you can easily loose data not only in the Coherence cache,
but in the underlying data store as well if you are not careful.
Now that you can't say you haven't been warned, let's see what we can do with it ☺.
Creating caches
The first thing you need to do in order to store some data in the grid is to create a named cache. This can be easily accomplished from the console prompt, using the cache command:
This will create a cache with the name countries. You can later use that name to obtain a reference to the cache programmatically.
You will notice that the prompt changed to:
This tells us that the currently
active cache is the one we just created. What this means is that all
the commands we execute next will be executed against that cache.
Working with the cache
Now that we have an active cache, let's put some data into it. This is also very simple-all we need to do is to execute the put command as many times as we want:
Map (countries): put USA "United States"
Map (countries): put GBR "United Kingdom"
Map (countries): put RUS Russia
Map (countries): put CHN China
Map (countries): put JPN Japan
Map (countries): put DEU Germany
Map (countries): put FRA France
Map (countries): put ITA Italy
Map (countries): put SRB Serbia
As you can see, the put
command takes two arguments-key and value. If either of the two
arguments contains a space, it has to be enclosed in quotes in order to
be parsed properly.
You will also notice that each put command returns null (which was intentionally omitted earlier to reduce clutter). Actually, each cache put returns the old cache value for the specified key. In this case, we started with an empty cache, which is why null was returned for each command. If you modify an existing value, you will see that the original value is returned:
Map (countries): put USA "United States of America"
United States
We can now use the list command to see a list of all cache entries:
Map (countries): list
JPN = Japan
SRB = Serbia
CHN = China
GBR = United Kingdom
ITA = Italy
FRA = France
USA = United States of America
DEU = Germany
RUS = Russia
Notice that the Coherence cache behaves exactly like the standard HashMap and provides no order guarantees for cache items. This is typically not an issue, but it is good to know.
If you only want to find out how many items are in the cache, you can use the size command:
Now, that we have some data in the cache, we can retrieve a single item from it, using the get command:
Map (countries): get SRB
Serbia
Or we can delete an item using the remove command:
Map (countries): remove SRB
Serbia
Similar to a put command, remove returns the value that was just deleted from the cache.
You can verify that the item was indeed removed by issuing a list or size command again:
Map (countries): list
JPN = Japan
CHN = China
GBR = United Kingdom
ITA = Italy
FRA = France
USA = United States of America
DEU = Germany
RUS = Russia
Map (countries): size
8
Now, as much fun as all of this
is, it won't take you long to realize that the Coherence console has
some limitations. For one, the fact that you can only use primitive
types, such as numbers and strings, for cache keys and values, will put
fairly severe constraints on what you can accomplish.
In the next section, we will
see how you can work with Coherence caches programmatically, and how you
can store instances of your custom classes within the data grid.
However, before we do that, let me show you why Coherence can have such a
positive impact on your application's availability and reliability.
Now go back to the console window and issue a list
command. You will notice that all the data is still there. To prove
that I'm not playing any Jedi mind tricks on you, start a brand new
Coherence node and after it comes up and joins the cluster, kill the
other cache server node that you started earlier. Once again, list all
the items in the cache from the console and you will see that no data
was lost even though both of the original storage nodes have
disappeared. Basically, as long as there are enough storage nodes in the
grid to hold all the data, the data will be safe.