0% found this document useful (0 votes)
54 views

Hazelcast IMDG Java Client: Before Getting Started

The document provides a reference card on using the Hazelcast Java client to interact with distributed data structures like maps, queues, sets, and more. It includes code examples and instructions for configuring the client and accessing Hazelcast features.

Uploaded by

nnpena83
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Hazelcast IMDG Java Client: Before Getting Started

The document provides a reference card on using the Hazelcast Java client to interact with distributed data structures like maps, queues, sets, and more. It includes code examples and instructions for configuring the client and accessing Hazelcast features.

Uploaded by

nnpena83
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Hazelcast IMDG Java Client Code Reference Card

Reference Card

Hazelcast IMDG Java Client

Contents

Before Getting Started...................................................................................................................... 1

Configure Hazelcast Java Client....................................................................................................... 2


Creating a Hazelcast Cluster Member....................................................................................................................... 2
Distributed Map........................................................................................................................................................... 2
Distributed MultiMap.................................................................................................................................................. 2
jCache............................................................................................................................................................................ 3
Distributed List............................................................................................................................................................. 3
Distributed Set.............................................................................................................................................................. 3
Distributed Queue....................................................................................................................................................... 4
Distributed Lock........................................................................................................................................................... 4
Distributed Topic.......................................................................................................................................................... 4

About Hazelcast IMDG...................................................................................................................... 5


Resources...................................................................................................................................................................... 5

Before Getting Started


TT Download latest hazelcast distribution from https://hazelcast.org/download/
TT Unzip to any folder
TT Start hazelcast member using startup script <hazelast_folder>/bin/start.sh

© 2017 Hazelcast Inc. “Hazelcast” is a trademark of Hazelcast Inc. www.hazelcast.com Reference Card 1
Hazelcast IMDG Java Client Code Reference Card

Configure Hazelcast Java Client


TT Install Hazelcast Artifact in Maven, Gradle, Ivy, ...

<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
</dependency>

Creating a Hazelcast Cluster Member

Config config = new Config();


HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);

Distributed Map

ClientConfig config = new ClientConfig();



HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);



ConcurrentMap<String, String> map = hazelcast.getMap("my-distributed-map");



map.put("key", "value");

map.get("key");

map.putIfAbsent("somekey", "somevalue");

map.replace("key", "oldvalue", "newvalue");

System.out.println(map);

Distributed MultiMap

ClientConfig config = new ClientConfig();


HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);

MultiMap<String, String> multimap = hazelcast.getMultiMap("restaurants");

multimap.put("New York", "Red Lobster");


multimap.put("New York", "Eataly");
System.out.println((multimap.get("New York"));

multimap.put("Las Vegas", "Alibi");


multimap.put("Las Vegas", "Pub & Grill");
System.out.println(multimap.get("Las Vegas"));

© 2017 Hazelcast Inc. “Hazelcast” is a trademark of Hazelcast Inc. www.hazelcast.com Reference Card 2
Hazelcast IMDG Java Client Code Reference Card

jCache

public static void main(String[] args) throws Exception {


Config config = new Config();
CacheSimpleConfig cacheConfig = config.getCacheConfig("languages");
cacheConfig.setKeyType(String.class.getName()).setValueType(String.class.getName());

HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

ICacheManager cacheManager = hz.getCacheManager();



Cache<String, String> cache = cacheManager.getCache("languages");

cache.put("language", "Java");
System.out.println(cache.get("language"));
}

Distributed List

ClientConfig config = new ClientConfig();


HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);

List<String> list = hazelcast.getList("my-distributed-list");

String element = "element1";


list.add(element);
System.out.println(String.format("contains element1: %s", list.
contains(element)));

List<String> elements = Arrays.asList("element2", "element3");


list.addAll(elements);
System.out.println(String.format("contains all elements: %s", list.
containsAll(elements)));

Distributed Set
A set is a data structure that doesn’t allow duplicates.

ClientConfig config = new ClientConfig();


HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);

Set<String> set = hazelcast.getSet("my-distributed-set");

List<String> elements = Arrays.asList("duplicate", "duplicate", "non-duplicate");


set.addAll(elements);
System.out.println(String.format("look, ma, no duplicates: %s", set));

© 2017 Hazelcast Inc. “Hazelcast” is a trademark of Hazelcast Inc. www.hazelcast.com Reference Card 3
Hazelcast IMDG Java Client Code Reference Card

Distributed Queue

ClientConfig config = new ClientConfig();


HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);

IQueue<String> queue = hazelcast.getQueue("my-distributed-queue");

queue.offer("item");
System.out.println(queue.poll());

queue.offer("anotheritem", 500, TimeUnit.MILLISECONDS);


System.out.println(queue.poll(5000, TimeUnit.MILLISECONDS));

queue.offer("yetanotheritem");
System.out.println(queue.take());

Distributed Lock

ClientConfig config = new ClientConfig();


HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);

Lock hzLock = hazelcast.getLock("my-distributed-lock");

hzLock.lock();
try{
  // do something}
finally{
  hzLock.unlock();
}

Distributed Topic

ClientConfig config = new ClientConfig();


HazelcastInstance hazelcast = HazelcastClient.newHazelcastClient(config);

ITopic<String> topic = hazelcast.getTopic("my-distributed-topic");

topic.addMessageListener(msg -> System.out.println(msg.getMessageObject()));

topic.publish("hello from the distributed world");

© 2017 Hazelcast Inc. “Hazelcast” is a trademark of Hazelcast Inc. www.hazelcast.com Reference Card 4
Hazelcast IMDG Java Client Code Reference Card

About Hazelcast IMDG


Hazelcast IMDG is an open-source in-memory data grid providing Java developers with an easy-to-use and
powerful solution for creating highly available and scalable applications. Hazelcast IMDG can be used in the
areas like clustering, in-memory NoSQL, application scaling, database caching and as an Oracle Coherence
replacement and Software AG Terracotta alternative.

Resources
Getting Started with Hazelcast IMDG:

Getting Started: https://hazelcast.org/clients/java/


Docs: http://docs.hazelcast.org/docs/latest/manual/html-single/index.html,
http://docs.hazelcast.org/docs/3.8/javadoc/
Download Hazelcast: https://hazelcast.org/download/

Join the discussion:

Google Group: https://groups.google.com/forum/#!forum/hazelcast


Stack Overflow: http://stackoverflow.com/questions/tagged/hazelcast

Blog And Use Cases:

Blog: https://blog.hazelcast.com
Use Cases: https://hazelcast.org/use-cases/data-grid

Professional Support:

https://hazelcast.com/services/support

Follow us online:

Twitter @Hazelcast https://twitter.com/hazelcast


Facebook https://www.facebook.com/hazelcast/
LinkedIn https://www.linkedin.com/company/hazelcast

350 Cambridge Ave, Suite 100, Palo Alto, CA 94306 USA


Email: [email protected] Phone: +1 (650) 521-5453
Visit us at www.hazelcast.com Reference Card 5

You might also like