Monday 23 November 2015

Elasticsearch: Java: Get API

By using ‘prepareGet’ method, you can get a document by specifying _index, _type, _id.

GetResponse response = client.prepareGet("organization", "employee", "1").execute().actionGet();

Following sep-by-step procedure explain complete working application.


Step 1: Define a model class Employee.
package com.self_learn.model;

import java.util.ArrayList;
import java.util.List;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@EqualsAndHashCode()
@ToString
public class Employee {
 @Getter @Setter private String age;
 @Getter @Setter private String firstName;
 @Getter @Setter private String lastName;
 @Getter @Setter private List<String> hobbies = new ArrayList<>();
}


Step 2: Define TransportClientUtil class to get a Client instance.

package com.self_learn.util;

import static com.self_learn.util.IPUtil.isValidHosts;
import static com.self_learn.util.IPUtil.isValidPorts;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import com.google.common.base.Preconditions;

public class TransportClientUtil {
 private static Map<Map<String, Integer>, Client> localMap = new HashMap<>();

 /**
  * Take machine name and port addresses as map and return transport client.
  * Key is host name, value is port number
  * 
  * @throws UnknownHostException
  */
 public static Client getTransportClient(String clusterName,
   Map<String, Integer> map) throws UnknownHostException {

  Preconditions.checkNotNull(clusterName,
    "clusterName shouldn't be empty");
  Preconditions.checkNotNull(map, "Map shouldn't be empty");

  if (localMap.containsKey(map))
   return localMap.get(map);

  Preconditions.checkState(isValidHostPorts(map),
    "Map contains invalid host (or) port");

  Settings settings = ImmutableSettings.settingsBuilder()
    .put("cluster.name", clusterName)
    .put("client.transport.sniff", true).build();

  TransportClient client = new TransportClient(settings);

  InetSocketTransportAddress addresses[] = getInetSocketTransportAddresses(map);
  client.addTransportAddresses(addresses);
  localMap.put(map, client);
  return client;
 }

 /**
  * @param map
  * @return true, if all the entries in map are valid host, ports. Else
  *         false.
  */
 private static boolean isValidHostPorts(Map<String, Integer> map) {
  Set<String> hostNames = map.keySet();
  Set<Integer> ports = new HashSet<>(map.values());

  if (!isValidHosts(hostNames.toArray(new String[hostNames.size()])))
   return false;

  if (!isValidPorts(ports.toArray(new Integer[ports.size()])))
   return false;

  return true;
 }

 private static InetSocketTransportAddress[] getInetSocketTransportAddresses(
   Map<String, Integer> map) throws UnknownHostException {
  InetSocketTransportAddress addresses[] = new InetSocketTransportAddress[map
    .size()];
  int count = 0;

  Set<String> keys = map.keySet();
  for (String key : keys) {
   InetAddress addr = InetAddress.getByName(key);
   InetSocketTransportAddress address = new InetSocketTransportAddress(
     addr, map.get(key));
   addresses[count] = address;
  }

  return addresses;
 }

 /**
  * Get transport client for localhost.
  * 
  * @param clusterName
  * @param port
  * @return
  * @throws UnknownHostException
  */
 public static Client getLocalTransportClient(String clusterName, int port)
   throws UnknownHostException {
  Settings settings = ImmutableSettings.settingsBuilder()
    .put("cluster.name", clusterName)
    .put("client.transport.sniff", true).build();

  TransportClient client = new TransportClient(settings);
  InetAddress addr = InetAddress.getByName("127.0.0.1");
  InetSocketTransportAddress address = new InetSocketTransportAddress(
    addr, port);

  client.addTransportAddress(address);
  return client;
 }

}


IPUtil class is used to validate hostnames, ports.

package com.self_learn.util;

import org.apache.commons.validator.routines.InetAddressValidator;

import com.google.common.base.Preconditions;

/**
 * Validate IPaddresses, ports
 * 
 * @author harikrishna_gurram
 */
public class IPUtil {
 private static InetAddressValidator inetAddressValidator = InetAddressValidator
   .getInstance();

 /**
  * @param ipAddress
  * @return true if ip address is valid, else false
  */
 public static boolean isValidIPAddress(String ipAddress) {
  Preconditions.checkNotNull(ipAddress, "IP address should not be null");
  return inetAddressValidator.isValid(ipAddress);
 }

 /**
  * @param port
  *            : Port number
  * @return true if port number is valid, else false
  */
 public static boolean isValidPort(int port) {
  if (port > 0 && port < 65536)
   return true;
  return false;
 }

 /**
  * @param hostNames
  * @return true if all the elements of array represents valid hosnames, else
  *         false.
  */
 public static boolean isValidHosts(String[] hostNames) {
  Preconditions.checkNotNull(hostNames, "Host names shouldn't be empty");
  for (String hostName : hostNames) {
   if (!isValidIPAddress(hostName)) {
    return false;
   }
  }
  return true;
 }

 /**
  * 
  * @param ports
  * @return true if all the elements of array represents valid ports, else
  *         false.
  */
 public static boolean isValidPorts(Integer[] ports) {
  Preconditions.checkNotNull(ports, "ports shouldn't be empty");
  for (int port : ports) {
   if (!isValidPort(port)) {
    return false;
   }
  }
  return true;
 }

}


Step 3: Define JSONUtil class to convert object to json.

package com.self_learn.util;

import org.elasticsearch.common.base.Preconditions;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Convert object to json string.
 * 
 * @author harikrishna_gurram
 */
public class JSONUtil {
 private static Gson gson = new Gson();
 private static Gson prettyGson = new GsonBuilder().setPrettyPrinting()
   .create();

 /**
  * @param obj
  * @return json string of this object.
  */
 public static String getJson(Object obj) {
  Preconditions.checkNotNull(obj, "obj shouldn't be null");
  return gson.toJson(obj);
 }

 /**
  * @param obj
  * @return json string of this object (Pretty json).
  */
 public static String getPrettyJson(Object obj) {
  Preconditions.checkNotNull(obj, "obj shouldn't be null");
  return prettyGson.toJson(obj);
 }
}


Step 4: IndexUtil class provide methods to store data into Elasticsearch.

package com.self_learn.util;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;

import com.google.common.base.Preconditions;

/**
 * Provides utility methods to store data into Elasticsearch.
 * 
 * @author harikrishna_gurram
 */
public class IndexUtil {

 /**
  * Index given document.
  * 
  * @param client
  *            : Client used to index data
  * @param _index
  *            : Document is stored in this index
  * @param _type
  *            : Document stored in this type
  * @param _id
  *            : Specifies _id of the document
  * @param document
  *            : Represents body of the document
  * @return {@link IndexResponse}
  */
 public static IndexResponse indexData(Client client, String _index,
   String _type, String _id, String document) {
  Preconditions.checkNotNull(client, "client should not be null");
  Preconditions.checkNotNull(_index, "_index should not be null");
  Preconditions.checkNotNull(_type, "_type should not be null");
  Preconditions.checkNotNull(_id, "_id should not be null");
  Preconditions.checkNotNull(document, "data should not be null");

  IndexResponse response = client.prepareIndex(_index, _type, _id)
    .setSource(document).execute().actionGet();
  return response;
 }

 /**
  * Index given object.
  * 
  * @param client
  *            : Client used to index data
  * @param _index
  *            : Document is stored in this index
  * @param _type
  *            : Document stored in this type
  * @param _id
  *            : Specifies _id of the document
  * @param obj
  *            : Object to index
  * @return {@link IndexResponse}
  */
 public static IndexResponse indexData(Client client, String _index,
   String _type, String _id, Object obj) {
  Preconditions.checkNotNull(obj, "data should not be null");
  return indexData(client, _index, _type, _id, JSONUtil.getJson(obj));
 }

}


Step 5: SearchUtil class provides various utility methods to query Elasticsearch.

package com.self_learn.util;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;

import com.google.common.base.Preconditions;

/**
 * Provide various utility methods to query Elasticsearch.
 * 
 * @author harikrishna_gurram
 */
public class SearchUtil {

 /**
  * Returns the document by id (Takes _index, _type, _id as input).
  * 
  * @param client
  * @param _index
  * @param _type
  * @param _id
  * @return the document by id
  */
 public static GetResponse getDocumentById(Client client, String _index,
   String _type, String _id) {
  Preconditions.checkNotNull(client, "client should not be null");
  Preconditions.checkNotNull(_index, "_index should not be null");
  Preconditions.checkNotNull(_type, "_type should not be null");
  Preconditions.checkNotNull(_id, "_id should not be null");

  GetResponse response = client.prepareGet(_index, _type, _id).execute()
    .actionGet();
  return response;
 }

}


Step 6: ResponseUtil class provide methods to return respone in string format.

package com.self_learn.util;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;

import com.google.common.base.Preconditions;

/**
 * Utility class to return respone in string format.
 * 
 * @author harikrishna_gurram
 */
public class ResponseUtil {

 /**
  * @param response
  * @return string representation of {@link IndexResponse}
  */
 public static String getResponseInfo(IndexResponse response) {
  Preconditions.checkNotNull(response, "response shouldn't be null");
  String _index = response.getIndex();
  String _type = response.getType();
  String _id = response.getId();
  long _version = response.getVersion();
  boolean created = response.isCreated();

  StringBuilder builder = new StringBuilder();
  return builder.append("_index: ").append(_index).append("\n")
    .append("_type: ").append(_type).append("\n").append("_id: ")
    .append(_id).append("\n").append("_version: ").append(_version)
    .append("\n").append("created: ").append(created).toString();
 }

 /**
  * @param response
  * @return string representation of {@link GetResponse}
  */
 public static String getResponseInfo(GetResponse response) {
  Preconditions.checkNotNull(response, "response should not be null");

  String _index = response.getIndex();
  String _type = response.getType();
  String _id = response.getId();
  long _version = response.getVersion();
  String source = response.getSourceAsString();

  StringBuilder builder = new StringBuilder();
  return builder.append("_index: ").append(_index).append("\n")
    .append("_type: ").append(_type).append("\n").append("_id: ")
    .append(_id).append("\n").append("_version: ").append(_version)
    .append("\n").append("_source: ").append(source).toString();
 }
}


Step 7: Main class demonstrates complete application.

package com.self_learn.test;

import java.net.UnknownHostException;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;

import com.self_learn.model.Employee;
import com.self_learn.util.IndexUtil;
import com.self_learn.util.ResponseUtil;
import com.self_learn.util.SearchUtil;
import com.self_learn.util.TransportClientUtil;

public class Main {
 private static String clusterName = "my_cluster_1";
 private static String _index = "organization";
 private static String _type = "employee";

 public static void main(String args[]) throws UnknownHostException {
  /* Get client instance for cluster */
  Client client = TransportClientUtil.getLocalTransportClient(
    clusterName, 9300);

  /* Prepare model object */
  Employee emp = new Employee();
  emp.setAge("27");
  emp.setFirstName("PTR");
  emp.setLastName("Nayan");
  emp.getHobbies().add("Tattoos");
  emp.getHobbies().add("People Watching");
  emp.getHobbies().add("Dagger Collecting");
  emp.getHobbies().add("Confusing People");

  /* Write object into Elasticsearch */
  IndexUtil.indexData(client, _index, _type, "1", emp);

  /* Query for the object with id 1 */
  GetResponse response = SearchUtil.getDocumentById(client, _index,
    _type, "1");

  /* Print Response */
  System.out.println(ResponseUtil.getResponseInfo(response));

  client.close();
 }
}


When you ran above application, you will get following kind of output.

Sep 09, 2015 2:47:54 PM org.elasticsearch.plugins.PluginsService <init>
INFO: [Styx and Stone] loaded [], sites []
_index: organization
_type: employee
_id: 1
_version: 8
_source: {"age":"27","firstName":"PTR","lastName":"Nayan","hobbies":["Tattoos","People Watching","Dagger Collecting","Confusing People"]}






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment