A newer version of Hazelcast CLC is available.

View latest

Get Started With the Hazelcast CLC

In this tutorial, you’ll learn the basics of how to configure the Hazelcast CLC, start it, and connect it to a cluster on Hazelcast Cloud. You’ll also see how to switch between clusters, using the Hazelcast CLC. Finally, you’ll perform some basic operations on a cluster from the command line and then by running a script to automate the same actions.

Before You Begin

You need the following:

  • The Hazelcast CLC installed on your local machine

  • Two Cloud clusters. For this tutorial, you’ll use one development and one production cluster, to show how you might use the CLC for basic administration and deployment tasks.

Step 1. Add Configuration for the Development Cluster

Make sure that your development cluster is running before you start.

  1. Sign in to Cloud and select the development cluster.

  2. From the cluster dashboard, click CLI to open the Quick Connection Guide for the Hazelcast CLC client.

  3. Run the clc config import command in step 2. This command imports the cluster configuration to your local machine.

  4. Run the clc -c pr-<CLUSTER_ID> command in step 3 to connect to your cluster using the Hazelcast CLC.

  5. You can use the clc config list command to confirm that the configuration was imported. The output will look something like this:

    clc config list
    default
    dev
  6. The CLC only connects to the cluster when necessary. Run a command that requires a connection to the cluster, such as object list:

    CLC> \object list
    Connected to cluster: pr-3814

    As this is a new cluster, no objects are returned.

  7. Press Ctrl+D or type \exit to shut down the Hazelcast CLC while you complete the configuration for your production cluster.

Step 2. Add Configuration for the Production Cluster

The configuration of the production cluster is the same as the previous step, but using the production cluster configuration.

Make sure that your production cluster is running before you start.

  1. Sign in to Cloud and select the production cluster.

  2. From the cluster dashboard, click CLI to open the Quick Connection Guide for the Hazelcast CLC client.

  3. Run the clc config import command in step 2. This command imports the cluster configuration to your local machine.

  4. Run the clc -c pr-<CLUSTER_ID> command in step 3 to connect to your cluster using the Hazelcast CLC.

  5. You can use the clc config list command to confirm that the configuration was imported. The output will look something like this:

    clc config list
    default
    prod
  6. The CLC only connects to the cluster when necessary. Run a command that requires a connection to the cluster, such as object list:

    CLC> \object list
    Connected to cluster: pr-3814

    As this is a new cluster, no objects are returned.

  7. Press Ctrl+D or type \exit to shut down the Hazelcast CLC.

Step 3. Switch Between Clusters

Having separate local configurations for your development and production clusters allows you to switch between them without leaving the command line. Make sure both clusters are running before you start.

  1. Execute the following command to connect to the development cluster. Replace the placeholder $CLUSTER_NAME with the name of your cluster:

    clc -c $CLUSTER_NAME

    As before, the Hazelcast CLC connects to your development cluster.

  2. Type \exit to return to the command line.

  3. Execute the following command to use the production cluster.

    clc -c $CLUSTER_NAME

    The Hazelcast CLC shell is started, using the configuration for the production cluster. You can exit the shell by typing \exit.

Step 4. Write Data to a Map

Now that you’ve connected to both your clusters, try using the Hazelcast CLC to create a map on your development cluster, write some data to it, and query the data. You can use SQL to do this.

  1. Start by creating a mapping to a new currency map. Replace the placeholder $CLUSTER_NAME with the name of your cluster:

    clc sql -c $CLUSTER_NAME "CREATE OR REPLACE MAPPING currency (__key INT, Code VARCHAR, Currency VARCHAR) TYPE IMap OPTIONS('keyFormat'='int', 'valueFormat'='json-flat');"

    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.

  2. Add some data to the map.

    clc sql -c $CLUSTER_NAME "INSERT INTO currency 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');"
  3. 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.

    clc sql -c $CLUSTER_NAME "SELECT * FROM currency ORDER BY Code" -f table

    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 5. 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 first writes the currency data to a new map called currencydata on a cluster.

  1. Copy the following commands into a script.

    myscript.sql
    CREATE 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;
  2. Save your script as myscript.sql.

  • Linux and MacOS

  • Windows

  1. To run the script on your development cluster, replacing the placeholder $CLUSTER_NAME with the name of your development cluster:

    cat myscript.sql | clc -c $CLUSTER_NAME
  2. Then, to run the script on your production cluster, execute the following command:

    cat myscript.sql | clc -c $CLUSTER_NAME
  1. To run the script on your development cluster, replacing the placeholder $CLUSTER_NAME with the name of your development cluster:

    type myscript.sql | clc -c $CLUSTER_NAME
  2. Then, to run the script on your production cluster, execute the following command:

    type myscript.sql | clc -c $CLUSTER_NAME

Step 6. Clean Up

To delete both test clusters from your account.

  1. Sign in to Cloud and select a test cluster.

  2. Click Delete and confirm your deletion.

Summary

In this tutorial, you learned how to do the following:

  • Connect to a development cluster on Cloud.

  • Connect to a production cluster on Cloud.

  • 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.

Learn More

Use these resources to continue learning: