Search Tutorials


Hazelcast Tutorial- Working with Hazelcast collections- Distributed Map,List,Set,Queues | JavaInUse



Hazelcast Tutorial- Working with Hazelcast collections- Map,List,Set,Queues

In this tutorial we look at key/value storage provided by Hazelcast maps. We create a new FirstMapExample class with a main method to spin up and manipulate a named distributed map called employees. Hazelcast refers to these named collections as a namespace and must be uniquely named across the cluster.

Lets Begin

We get the instance of a Map named as employees from Hazelcast. This map name employees is unique in our cluster MyCluster. It has Fixed number of partitions (default 271). Each key falls into a partition where partitionId = hash(keyData)%PARTITION_COUNT. Partition ownerships are reassigned upon membership change. Since this map will not contain any element, when we retrieve it for the first time, the below code will insert 5 employee details in the map.
package com.javainuse.main;

import java.util.Map;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

public class FirstMapExample {
	public static void main(String[] args) {
		HazelcastInstance hz = Hazelcast.newHazelcastInstance();
		Map<String, String> employees = hz.getMap("employees");
		 if(employees.containsKey("1"))
		    {
			    employees.put("6", "emp6");
		
		    }else
	    employees.put("1", "emp1");
	    employees.put("2", "emp2");
	    employees.put("3", "emp3");
	    employees.put("4", "emp4");
	    employees.put("5", "emp5");
	    
	    System.out.println("Total number of employees " + employees.size());
	}
}
Run the above class. A node on port 5701 gets started. This node gets is part of cluster named MyCluster. Also we get an instance of map from hazelcast. During the first run we insert 5 employee details in this map. So its size is 5.
hazel_2-1
Now run the above program again. A hazelcast node gets started on port 5702. This port gets added to the MyCluster cluster. We get the same employees map. Here we insert one more employee detail emp6 in this map. So its size will be 6.
hazel_2-2
Other than Map we can also use other Hazelcast collections like Sets, Lists and Queues.
Two of these additional types are distributed versions of collections that we are hopefully already familiar with—sets and lists.
Set<String> persons = hz.getSet("persons");
persons.addAll(employees.values());
persons.add("tom");
persons.add("john");
persons.add("jobin");

List<String> countries = hz.getList("persons");
persons.addAll(employees.keySet());
persons.add("tom");
persons.add("john");
persons.add("tom"); // duplicate entry

Hazelcast provides is a first-in first-out (FIFO) based queue. This provides us with a mechanism to offer objects onto the top of a queue before retrieving them off the bottom. Such a structure would be incredibly useful if we had a number of tasks to individually handle by a number of client workers.
BlockingQueue<String> arrivals = hz.getQueue("arrivals");

    while (true) {
      String arrival = arrivals.take();

      System.err.println(
        "New arrival from: " + arrival);
    }

Download Source Code

Download it - Hazelcast Collections- Map, Set and Queue

	

See Also

Hazelcast Hello World Example- Getting started with Hazelcast. Hazelcast Tutorial- Understanding Replicated Maps using example. Hazelcast - Main Menu