Using Hazelcast REST API in Java applications

Hazelcast is an open-source in-memory data grid (IMDG) solution that provides distributed caching and clustering capabilities for Java applications. In addition to its powerful in-memory data storage and processing capabilities, Hazelcast also exposes a REST API that allows you to interact with and manage the Hazelcast IMDG cluster programmatically.

Setting up Hazelcast REST API

To use the Hazelcast REST API in your Java application, follow these steps:

  1. Add Hazelcast dependency: Firstly, add the Hazelcast dependency to your project’s build file, such as Maven or Gradle. For Maven, add the following dependency to your pom.xml file:

    <dependencies>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
            <version>4.2.2</version>
        </dependency>
    </dependencies>
    
  2. Start the Hazelcast cluster: Before using the REST API, you need to start a Hazelcast IMDG cluster. You can either start Hazelcast programmatically or use the provided scripts and configurations for standalone and cluster setups.

  3. Configure Hazelcast REST API: To enable the REST API, you need to configure the hazelcast.yaml file. Add the following configuration to enable the REST API with default settings:

    network:
      port:
        port-count: 100
        auto-increment: true
      reuse-address: true
       
    rest-api:
      enabled: true
    
  4. Interacting with Hazelcast REST API: Once the cluster and REST API are set up, you can start interacting with the Hazelcast REST API in your Java application.

    For example, here’s how you can retrieve a Hazelcast map using the REST API:

    import com.hazelcast.client.HazelcastClient;
    import com.hazelcast.client.config.ClientConfig;
    import com.hazelcast.client.config.ClientNetworkConfig;
    import com.hazelcast.core.HazelcastInstance;
    import com.hazelcast.core.IMap;
       
    public class HazelcastRestApiClient {
       
        public static void main(String[] args) {
            ClientNetworkConfig networkConfig = new ClientNetworkConfig();
            networkConfig.addAddress("localhost:5701");
       
            ClientConfig clientConfig = new ClientConfig();
            clientConfig.setNetworkConfig(networkConfig);
       
            HazelcastInstance hazelcastClient = HazelcastClient.newHazelcastClient(clientConfig);
       
            IMap<String, String> myMap = hazelcastClient.getMap("myMap");
            String value = myMap.get("key");
       
            System.out.println("Value: " + value);
       
            hazelcastClient.shutdown();
        }
    }
    

    In this example, we configure a Hazelcast client using the hazelcast-client library and retrieve a map named myMap by connecting to the Hazelcast cluster running on localhost:5701. We then retrieve the value associated with the key “key” and print it to the console.

Conclusion

In this blog post, we learned how to use the Hazelcast REST API in Java applications. By enabling and configuring the REST API, you can interact with the Hazelcast IMDG cluster programmatically and perform various operations such as reading and writing data to distributed maps. Hazelcast provides a seamless integration experience, empowering you to leverage the power of in-memory data grids in your applications.

#hazelcast #rest-api