The Hazelcast Cloud Developer Hub

Welcome to the Hazelcast Cloud developer hub. You'll find comprehensive guides and documentation to help you start working with Hazelcast Cloud as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Python Client

Quick Setup

Follow the below instructions to connect a Python client to your cluster.

1. Create a cluster as explained in the Create a Cluster section. After you create the cluster, the cluster-specific page will load and the cluster's state will be "Pending" for some seconds, as shown below.

2. When the cluster's state becomes "Running", the previously inactive button Configure Client on this page becomes active. Click on it and the following dialog box appears:

3. Click on the Python tab (the Java tab appears by default) and download the ZIP file as instructed in Step 1 of the dialog box.

4. Extract the ZIP file and run the command, in the extracted folder, shown in Step 2 of the above dialog box.

You should see the log output similar to the following:

Connection Successful! in your logs means that your client application has successfully connected to your cluster in Hazelcast Cloud. The sample code inserts random entries to your cluster as you can see in the logs. In your Hazelcast Cloud console (your cluster's page where you can reach from the "Clusters" top menu), you should see the charts are being populated with metrics of your map, as shown below:

Client Code

Now, as we have successfully connected and put data into the cluster, let's review and explain the client code you downloaded.

Go to the directory (extracted from the ZIP file you downloaded in the Step 3 above) and locate the client sample code. If you had enabled encryption in your cluster, open the client_with_ssl.py file. Otherwise, open the client.py file. We will explain both files line by line in the following sections.

client.py

This is the downloaded sample client code when your cluster does not have an encryption.

import hazelcast
import logging
import random
import time

if __name__ == "__main__":
    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)

    config = hazelcast.ClientConfig()

    # Set up group name and password for authentication
    config.group_config.name = "YOUR_CLUSTER_NAME"
    config.group_config.password = "YOUR_CLUSTER_PASSWORD"

    # Enable Hazelcast.Cloud configuration and set the token of your cluster.
    config.network_config.cloud_config.enabled = True
    config.network_config.cloud_config.discovery_token = "YOUR_CLUSTER_DISCOVERY_TOKEN"

    # Start a new Hazelcast client with this configuration.
    client = hazelcast.HazelcastClient(config)

    my_map = client.get_map("map").blocking()
    my_map.put("key", "value")

    if my_map.get("key") == "value":
        print("Connection Successful!")
        print("Now, `map` will be filled with random entries.");
        while True:
            random_key = random.randint(1, 100000)
            my_map.put("key" + str(random_key), "value" + str(random_key))
            my_map.get("key" + str(random.randint(1,100000)))
            if random_key % 10 == 0:
                print("Map size:" + str(my_map.size()))
            time.sleep(0.1)
    else:
        raise Exception("Connection failed, check your configuration.")


    client.shutdown()

First, we create a client configuration using the following line:

config = hazelcast.ClientConfig()

Then, we set the cluster name and password via group configuration using the following line:

config.group_config.name = "YOUR_CLUSTER_NAME"
config.group_config.password = "YOUR_CLUSTER_PASSWORD"

The cluster name and password are unique to your cluster; the client is authenticated using this password.

Then, we set the discovery token using the following line:

config.network_config.cloud_config.discovery_token = "YOUR_CLUSTER_DISCOVERY_TOKEN"

This token is also unique to your cluster and used to discover Hazelcast cluster members.

Now, we create the client with config using the following line:

client = hazelcast.HazelcastClient(config)

This step creates the connection between your application and the cluster.

We get/create a map and put a simple entry ("key", "value") using the following lines, respectively:

my_map = client.get_map("map").blocking()
my_map.put("key", "value")

For simplicity, we created a blocking map.

Then, we check if the entry has been put successfully and let the code throw an exception if the value is not correct using the following lines. Finally, we put random entries with 100-millisecond intervals:

if my_map.get("key") == "value":
        print("Connection Successful!")
        print("Now, `map` will be filled with random entries.");
        while True:
            random_key = random.randint(1, 100000)
            my_map.put("key" + str(random_key), "value" + str(random_key))
            my_map.get("key" + str(random.randint(1,100000)))
            if random_key % 10 == 0:
                print("Map size:" + str(my_map.size()))
            time.sleep(0.1)
    else:
        raise Exception("Connection failed, check your configuration.")

client_with_ssl.py

This is the downloaded sample client code when your cluster has encryption enabled.

import hazelcast
import logging
import os
import random
import time

if __name__ == "__main__":

    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)

    config = hazelcast.ClientConfig()
    config.network_config.ssl_config.enabled = True
    config.network_config.ssl_config.cafile = os.path.abspath("ca.pem")
    config.network_config.ssl_config.certfile = os.path.abspath("cert.pem")
    config.network_config.ssl_config.keyfile = os.path.abspath("key.pem")
    config.network_config.ssl_config.password = "YOUR_TLS_PASSWORD"

    # Set up group name and password for authentication
    config.group_config.name = "YOUR_CLUSTER_NAME"
    config.group_config.password = "YOUR_CLUSTER_PASSWORD"

    # Enable Hazelcast.Cloud configuration and set the token of your cluster.
    config.network_config.cloud_config.enabled = True
    config.network_config.cloud_config.discovery_token = "YOUR_CLUSTER_DISCOVERY_TOKEN"

    # Start a new Hazelcast client with this configuration.
    client = hazelcast.HazelcastClient(config)

    my_map = client.get_map("map").blocking()
    my_map.put("key", "value")

    if my_map.get("key") == "value":
        print("Connection Successful!")
        print("Now, `map` will be filled with random entries.");
        while True:
            random_key = random.randint(1, 100000)
            my_map.put("key" + str(random_key), "value" + str(random_key))
            my_map.get("key" + str(random.randint(1,100000)))
            if random_key % 10 == 0:
                print("Map size:" + str(my_map.size()))
            time.sleep(0.1)
    else:
        raise Exception("Connection failed, check your configuration.")

    client.shutdown()

The only difference between this one and the Client.java is the lines that enable and configure TLS encryption on the client side:

config.network_config.ssl_config.enabled = True
config.network_config.ssl_config.cafile = os.path.abspath("ca.pem")
config.network_config.ssl_config.certfile = os.path.abspath("cert.pem")
config.network_config.ssl_config.keyfile = os.path.abspath("key.pem")
config.network_config.ssl_config.password = "YOUR_TLS_PASSWORD"

You may want to move the 'pem' files to another directory. Then, you need to set 'ca', 'cert' and 'key' file directories accordingly.

More Configuration Options

Please refer to the Hazelcast Python Client Documentation for more information on the configuration options of the client.

Updated a day ago

Python Client


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.