Interface MapRESConverter

If you want to index one or more MapR-DB tables in ElasticSearch and your source data uses complex data types, you must write a Java class that converts your binary source data to those data types. Extend this Java interface with the class that you write.

 /* Copyright (c) 2009 & onwards. MapR Tech, Inc., All rights reserved */
package com.mapr.fs.external.es;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.IOException;
 
import org.apache.hadoop.hbase.client.Result;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.delete.DeleteRequest;
 
public interface MapRESConverter {
  public static class ConversionResult {
    private String key;
    private UpdateRequest updateRequest;
    private DeleteRequest deleteRequest;
 
    // Save a reference to HBase Result if required later
    // Currently used by default compareRows
    private Result result;
 
    public ConversionResult(String k) {
      key = k;
      updateRequest = null;
      deleteRequest = null;
    }
 
    public ConversionResult(String k, UpdateRequest u, DeleteRequest d) {
      key = k;
      updateRequest = u;
      deleteRequest = d;
    }
 
    public void setKey(String k) { key = k; }
    public void setUpdateRequest(UpdateRequest u) { updateRequest = u; }
    public void setDeleteRequest(DeleteRequest d) { deleteRequest = d; }
    public void setHBaseResult(Result res) { result = res; }
 
    public String getKey() { return key; }
    public DeleteRequest getDeleteRequest() { return deleteRequest; }
    public UpdateRequest getUpdateRequest() { return updateRequest; }
    public Result getHBaseResult() { return result; }
  }
 
  /**
   * Allow implementer to initialize the converter
   * @param mappingMetaData A mapping of specific fields to their types.
   *                        If the user has not configured any mapping in the cluster,
   *                        this parameter will be null.
   * @throws UnsupportedOperationException
   * @throws Exception
   */
  public void init(String index, String type, MappingMetaData mappingMetaData)
      throws UnsupportedOperationException, IOException;
 
  /**
   * Send a HBase result object for the implementer to convert into an
   * array of ConversionResult objects
   * @param result HBase Result object
   * @return Array of ConversionResult objects to be updated/deleted into ES cluster
   * @throws UnsupportedOperationException
   * @throws Exception
   */
  public List<ConversionResult> convertPut(Result result)
      throws UnsupportedOperationException, IOException;
 
  /**
   * Send a row key for the implementer to convert into an
   * array of ConversionResult objects. If ConversionResult.type = DELETE,
   * we would execute a delete query on the ES cluster, else it will be an update query.
   * @param key Row key to be deleted
   * @return Array of ConversionResult objects to updated/deleted into ES cluster
   * @throws UnsupportedOperationException
   * @throws Exception
   */
  public List<ConversionResult> convertDelete(String key)
      throws UnsupportedOperationException, IOException;
 
  /**
   * Send column details (key, family and column name) of a column being deleted
   * to the implementer to convert into an array of ConversionResult objects.
   * If ConversionResult.type = DELETE, we would execute a delete query on the
   * ES cluster, else it will be an update query.
   * @param key Row key to be deleted
   * @param family Family name of the column being deleted
   * @param column Column name of the column being deleted
   * @return Array of ConversionResult objects to updated/deleted into ES cluster
   * @throws UnsupportedOperationException
   * @throws Exception
   */
  public List<ConversionResult> convertColumnDelete(String key,
                                                    String family,
                                                    String column)
    throws UnsupportedOperationException, IOException;
 
  public List<ConversionResult> convertColumnFamilyDelete(String key,
                                                          String family)
    throws UnsupportedOperationException, IOException;

  /**
   * Send HBase Result object and its corresponding list of GetResponse
   * objects for the implementer to compare.
   * The GetResponse objects are based on keys returned by the getESKeys() method.
   * Example:
   *     List<ConversionResult> convResults = converter.getESKeys(result);
   *     List<GetResponse>      responses   = new ArrayList<GetResponse>();
   *     for (ConversionResult convResult : convResults) {
   *         GetResponse response = client.prepareGet(<index>,
   *                                                  <type>,
   *                                                  convResult.getKey())
   *                                      .execute()
   *                                      .actionGet();
   *         responses.add(response);
   *     }
   *     converter.compareRows(result, responses);
   * @param result HBase Result object
   * @param responses List of GetResponse objects
   * @return boolean whether the comparison was match or not.
   * @throws UnsupportedOperationException
   * @throws Exception
   */
  public boolean compareRows(Result result, List<GetResponse> responses)
      throws UnsupportedOperationException, IOException;
 
  /**
   * Get a list of ES type keys/ids for a given HBase Result object.
   * This method is used in the comparison routine(as in above example).
   * @param result HBase Result object
   * @return List of ConversionResults with key set(The JSON doc and type is ignored).
   * @throws UnsupportedOperationException
   * @throws Exception
   */
  public List<ConversionResult> getESKeys(Result result)
      throws UnsupportedOperationException, IOException;
 
  /**
   * Allow implementer to close the converter
   */
  public void close()
      throws UnsupportedOperationException, IOException;
}