Get Started with the Hazelcast CLC
In this tutorial, you’ll learn how to use Hazelcast CLC commands to authenticate with Hazelcast Cloud and create two production clusters. You’ll connect to and switch between the clusters from the command line. Finally, you’ll perform some basic operations on a cluster from both the command line and by running a script to automate the same actions.
Before You Begin
You need the following:
-
Hazelcast CLC installed on your local machine.
Step 1. Authenticating with Cloud
To allow the Hazelcast CLC to to interact with Cloud clusters, you must generate a Cloud token.
Step 2. Create Two Clusters on Cloud
In this step, you’ll create two production clusters called Test1 and Test2 from the command line, and run some commands to check their status.
-
Execute the following command to create your first production cluster.
Wait while the cluster is created and the cluster configuration is imported into the Hazelcast CLC.
-
Create your second production cluster in the same way.
-
Try running the following command to check your clusters have been created:
The details of all clusters linked to your Cloud account are returned, including the Cluster ID, Cluster Name, Cluster State, Hazelcast Version.
Step 3. Add Configuration to Connect to Cloud
To connect to each production cluster, you need to import additional cluster configuration from Cloud.
-
Run the
clc config import
to update the cluster configuration on your local machine. -
Run the following command to connect to your cluster using the Hazelcast CLC.
-
The Hazelcast CLC only connects to the cluster when necessary. Run a command that requires a connection to the cluster, such as
object list
:As this is a new cluster, no objects are returned.
-
Press kbd:[Ctrl+D] or type
\exit
to shut down the Hazelcast CLC while you complete the configuration of your second production cluster. -
Repeat steps 1 to 4 for Test2.
Step 4. Switch Between Clusters
Having separate local configurations for your two production clusters allows you to switch between them without leaving the command line. Make sure both clusters are running before you start.
Step 5. Write Data to a Map
Now that you’ve connected to both your clusters, try using the Hazelcast CLC to create a map on Test1, write some data to it, and query the data. You can use SQL to do this.
-
Start by creating a mapping to a new
currency
map.The SQL mapping statement does a number of things:
-
Adds column headings for currencies and codes
-
Creates a SQL connection to the map
-
Tells Hazelcast how to serialize and deserialize the keys and values.
-
-
Add some data to the map.
-
Try running some simple queries against the
currency
map. For example, this query returns all data in the map and orders it by the currency code.The results look like this:
-------------------------------------------------------------------------------- __key | Code | Currency -------------------------------------------------------------------------------- 1 | CAD | Canadian Dollar 4 | GBP | Pounds Sterling 2 | INR | Indian Rupee 3 | MXN | Mexican Peso 5 | TRY | Turkish Lira 6 | USD | United States Dollar --------------------------------------------------------------------------------
Step 6. Automate Actions
When you’re ready, combine the commands that you’ve learned about so far into a script and run them from the command line.
The script writes the currency data to a new map called currencydata
on a cluster and queries it.
-
Copy the following commands into a script.
myscript.sqlCREATE OR REPLACE MAPPING currencydata ( __key INT, Code VARCHAR, Currency VARCHAR ) TYPE IMap OPTIONS( 'keyFormat'='int', 'valueFormat'='json-flat' ); INSERT INTO currencydata VALUES (1, 'CAD', 'Canadian Dollar'), (2, 'INR', 'Indian Rupee'), (3, 'MXN', 'Mexican Peso'), (4, 'GBP', 'Pounds Sterling'), (5, 'TRY', 'Turkish Lira'), (6, 'USD', 'United States Dollar'); SELECT * FROM currencydata ORDER BY Code;
-
Save your script as
myscript.sql
. +. To run the script on your Test1 cluster, execute the following command. -
Then, to run the script on your Test2 cluster, execute the same command replacing the cluster name.
Summary
In this tutorial, you learned how to do the following:
-
Authenticate with Cloud.
-
Create a cluster and check its status.
-
Connect to a Cloud production cluster.
-
Switch between clusters from the command line.
-
Write data to a map and query the data using SQL.
-
Automate commands by running a sequence of actions from a shell script.