Distribute Your Data

You’re an engineer who wants to distribute data for caching and sharing. Learn how to use Hazelcast data structures.

The quickest way to learn how to use Hazelcast is to start up a cluster and add some data to it. Check out the Getting Started Guide. Then we suggest you enroll in free training and take the Hazelcast Platform Overview course. If you’re on the move, listen to a quick 10-minute podcast from one of the community engineers describing what Hazelcast is all about.

Many caching technologies are available, but we believe Hazelcast is the best, and here are seven reasons why.

Once you are ready to look at the wider data structures, check out the extensive examples in all client languages. Links to each repository are below. For a quick start:

Here’s how you can add entries to a distributed map using a Java client

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

import java.util.Map;

public class FillMapMember {

    public static void main(String[] args) {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        Map map = hz.getMap("map");
        map.put("1", "Tokyo");
        map.put("2", "Paris");
        map.put("3", "New York");
        System.out.println("Finished loading map");
    }
}

Here’s a simple predicate query over some data in Java

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.hazelcast.query.Predicates;

import java.util.Set;

public class SqlQueryMember {

    public static void main(String[] args) {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        IMap map = hz.getMap("map");

        map.put("1", new Customer("peter", true, 36));
        map.put("2", new Customer("john", false, 40));
        map.put("3", new Customer("roger", true, 20));

        Set employees = (Set) map.values(Predicates.sql("active AND age < 30"));
        System.out.println("Employees: " + employees);

        Hazelcast.shutdownAll();
    }
}

How about using a set in .NET?

using System;
using Hazelcast.Client;
using Hazelcast.Config;

namespace Hazelcast.Examples.Collections
{
    internal class SetExample
    {
        private static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();
            config.GetNetworkConfig().AddAddress("127.0.0.1");
            var client = HazelcastClient.NewHazelcastClient(config);

            var set = client.GetSet("set-example");

            set.Add("item1");
            set.Add("item2");
            set.Add("item3");
            set.Add("item3");

            Console.WriteLine("Enumerator : " + string.Join(", ", set));

            Console.WriteLine("Contains: " + string.Join(", ", set.Contains("item2")));

            Console.WriteLine("Count: " + string.Join(", ", set.Count));

            set.Destroy();
            client.Shutdown();
        }
    }
}

Make use of the CRDT PN counter in Python

import hazelcast
import logging

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

    client = hazelcast.HazelcastClient()

    pn_counter = client.get_pn_counter("pn-counter").blocking()

    print("Counter is initialized with {}".format(pn_counter.get()))

    for i in range(10):
        print("Added {} to the counter. Current value is {}".format(i, pn_counter.add_and_get(i)))

    print("Incremented the counter after getting the current value. "
          "Previous value is {}".format(pn_counter.get_and_increment()))

    print("Final value is {}".format(pn_counter.get()))

Generate a unique cluster-wide ID using Go

package main

import (
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {

	config := hazelcast.NewConfig()
	config.NetworkConfig().AddAddress("127.0.0.1:5701")
	client, err := hazelcast.NewClientWithConfig(config)
	if err != nil {
		fmt.Println(err)
		return
	}
	flakeIDGenerator, _ := client.GetFlakeIDGenerator("generator")
	id, _ := flakeIDGenerator.NewID()
	fmt.Printf("new id : %d", id)

	flakeIDGenerator.Destroy()
	client.Shutdown()

}

There are many more examples for each language in their repository