Search This Blog

Hazelcast with python

Assumption: You are aware of, how to start hazelcast if not check here.

In today’s era python is must know language. It’s being used widely to implement regular BAU logic to special machine learning. In the parallel world hazelcast is providing extremely useful in-memory data grid which solve the problem of having restricted size of data in memory to process, hence increasing the overall performance of systems.

Let’s see how we can connect to hazelcast from our python code.

The available hazelcast client is compatible with python 2.7 version.

Let’s do this in steps

Prerequisite : hazelcast instance must be up and running and you should have the credentials and ip information 

Step1

Install python version 2.7 from here

Step 2

Install hazelcast python client. Execute below command
pip install hazelcast-python-client

Sometimes because of some restrictions we can’t install it directly. Use proxy in that case as
pip install --proxy http://<user>:<pwd>@<proxy>:<port> hazelcast-python-client

Step 3

  • Create a new python module(intelliJ) or project (eclipse)
  • Create a new python file as hazelClient.py
  • Add code as below (git)

config = hazelcast.ClientConfig() 


# use the credentials which has been used to start the hazelcast instance (pre-requisite) 
config.group_config.name = "user"
config.group_config.password = "pwd"


# localhost is the hostname or IP address, e.g. 'localhost:5701' or 143.91.84.22:5701 
config.network_config.addresses.append('localhost')

# basic logging setup to see client logs

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


# creating hazelcast client instance
client = hazelcast.HazelcastClient(config) 


my_map = client.get_map("test_map")
my_map.put("1", "data") 


client.shutdown()



No comments:

Post a Comment