Thursday, 27 July 2017

Elasticsearch Java Client

Elasticsearch clients
All Elasticsearch operations are executed using a Client object and asynchronous in nature. There are two categories of clients.
a.   Node client
b.   Transport client

Node client
public static Client getNodeClient() {
 Node node = nodeBuilder().node();
 Client client = node.client();
 return client;
}

Above snippet is used to create client node.

When you start a node, it joins Elasticsearch cluster. You can define cluster.name in the /src/main/resources/elasticsearch.yml file in your project. As long as elasticsearch.yml is present in the classpath, it will be used when you start your node.

You can also create a node for a given cluster using following snippet.

public static Client getNodeClient(String clusterName) {
 Node node = nodeBuilder().clusterName(clusterName).node();
 Client client = node.client();
 return client;
}


A node can hold data (or) can just used for processing requests. Following snippet is used to create a client, which is used to processing requests (Do not store any data).

public static Client getPassiveNode() {
 Node node = nodeBuilder().settings(Settings.settingsBuilder().put("http.enabled", false)).client(true).node();
 Client client = node.client();
 return client;
}

You can create passive node to a given cluster using following snippet.  

/* Used to return passive node (Don't store any data in it) */
public static Client getPassiveNode(String clusterName) {
 Node node = nodeBuilder().clusterName(clusterName).settings(Settings.settingsBuilder().put("http.enabled", false)).client(true).node();
 Client client = node.client();
 return client;
}


You can close client by calling node.close method.

Note
1.   Frequently starting and stopping one or more node clients creates unnecessary noise across the cluster.
2.   Embedded node client will respond to outside requests, just like any other client. So it is better to disable HTTP for an embedded node client.

Simple ClientUtil class given below.

ClientUtil.java

package com.self_learn.util;

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.node.Node;

import com.google.common.base.Preconditions;

public class ClientUtil {
 private static Node localNode = null;
 private static Node localPassiveNode = null;
 private static Map<String, Node> activeNodes = new HashMap<>();
 private static Map<String, Node> passiveNodes = new HashMap<>();

 public static void closeLocalNode() {
  Preconditions.checkNotNull(localNode, "localNode is not initialized");
  localNode.close();
 }

 public static void closeLocalPassiveNode() {
  Preconditions.checkNotNull(localPassiveNode,
    "localPassiveNode is not initialized");
  localPassiveNode.close();
 }

 public static void closeActiveNode(String clusterName) {
  Preconditions
    .checkNotNull(clusterName, "clusterName shouldn't be null");
  Preconditions.checkNotNull(activeNodes.get(clusterName),
    "There is no node for cluster " + clusterName);
  activeNodes.get(clusterName).close();
 }

 public static void closePassiveNode(String clusterName) {
  Preconditions
    .checkNotNull(clusterName, "clusterName shouldn't be null");
  Preconditions.checkNotNull(passiveNodes.get(clusterName),
    "There is no node for cluster " + clusterName);
  passiveNodes.get(clusterName).close();
 }

 public static Client getClient() {
  if (localNode == null) {
   localNode = nodeBuilder()
     .settings(
       ImmutableSettings.settingsBuilder().put(
         "http.enabled", false)).client(true).node();

   Client client = localNode.client();
   return client;
  }
  return null;
 }

 /* Used to return passive node (Don't store any data in it) */
 public static Client getPassiveClient() {

  if (localPassiveNode == null) {
   localPassiveNode = nodeBuilder()
     .settings(
       ImmutableSettings.settingsBuilder().put(
         "http.enabled", false)).client(true).node();
   Client client = localPassiveNode.client();
   return client;
  }

  return null;
 }

 public static Client getClientForCluster(String clusterName) {
  Preconditions
    .checkNotNull(clusterName, "clusterName shouldn't be null");

  if (activeNodes.get(clusterName) == null) {
   Node node = nodeBuilder()
     .settings(
       ImmutableSettings.settingsBuilder().put(
         "http.enabled", false))
     .clusterName(clusterName).node();
   Client client = node.client();
   activeNodes.put(clusterName, node);
   return client;
  }

  return activeNodes.get(clusterName).client();
 }

 /* Used to return passive node (Don't store any data in it) */
 public static Client getPassiveClientForCluster(String clusterName) {
  Preconditions
    .checkNotNull(clusterName, "clusterName shouldn't be null");

  if (passiveNodes.get(clusterName) == null) {
   Node node = nodeBuilder()
     .settings(
       ImmutableSettings.settingsBuilder().put(
         "http.enabled", false)).client(true)
     .clusterName(clusterName).node();
   Client client = node.client();
   passiveNodes.put(clusterName, node);
   return client;
  }
  return passiveNodes.get(clusterName).client();
 }
}

No comments:

Post a Comment