Blogs
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
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 org.elasticsearch.search.SearchHit;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.PropsUtil;
public class ESLogManager {
public static List listOfIPAddresses = new ArrayList();
public static String clusterName = "";
public static int nodePort = 0;
public static String indexName = "";
public static String indexMapping = "";
static{
String ipAddresses = GetterUtil.getString(PropsUtil.get("elasticsearch.serverIP"));
StringTokenizer tokens = new StringTokenizer(ipAddresses, ",");
while(tokens.hasMoreTokens()){
listOfIPAddresses.add(tokens.nextElement());
}
clusterName = GetterUtil.getString(PropsUtil.get("elasticsearch.cluster.name"));
String sPortNumber = GetterUtil.getString(PropsUtil.get("elasticsearch.portNumber"));
if(null!=sPortNumber || sPortNumber.length()>0){
nodePort = Integer.parseInt(sPortNumber);
}
indexName = GetterUtil.getString(PropsUtil.get("audit_index_name"));
indexMapping = GetterUtil.getString(PropsUtil.get("audit_index_mapping"));
}
@SuppressWarnings("resource")
public void insertRecordsToIndex(String json) {
TransportClient transportClient = null;
try {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build();
transportClient = new TransportClient(settings);
for(int index = 0;index<listOfIPAddresses.size();index++){
transportClient = transportClient.addTransportAddress(
new InetSocketTransportAddress((String)listOfIPAddresses.get(index),nodePort));
}
IndexRequestBuilder response = transportClient.prepareIndex(indexName,indexMapping);
response.setSource(json).execute().actionGet();
} catch (Exception e) {
e.printStackTrace();
}finally{
transportClient.close();
}
}
@SuppressWarnings("resource")
public List<Map<String,Object>> getRecordsFromIndex(){
List<Map<String,Object>> listOfRecords = new LinkedList<Map<String,Object>>();
TransportClient transportClient = null;
try {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name",clusterName).build();
transportClient = new TransportClient(settings);
for(int index = 0;index<listOfIPAddresses.size();index++){
transportClient = transportClient.addTransportAddress(
new InetSocketTransportAddress((String)listOfIPAddresses.get(index),nodePort));
}
SearchResponse response = transportClient.prepareSearch(indexName)
.setTypes(indexMapping).setSearchType(SearchType.QUERY_AND_FETCH)
.setFrom(0).setSize(Integer.MAX_VALUE).setExplain(true).execute().actionGet();
SearchHit[] results = response.getHits().getHits();
for (SearchHit hit : results) {
Map<String, Object> result = hit.getSource();
listOfRecords.add(result);
}
}catch(Exception e){
e.printStackTrace();
}finally{
transportClient.close();
}
return listOfRecords;
}
}