D:\soft\elasticsearch\elasticsearch-2.1.0\libjava
package com.dxz.es; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Map; import java.util.concurrent.TimeUnit; import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.Script; import org.elasticsearch.search.SearchHit; import com.alibaba.fastjson.JSON; import com.dxz.es.model.LogModel; import com.google.common.collect.Maps; public class ElkTest { // private static final String CLUSTER_NAME = "cluster_name"; public static final String CLUSTER_NAME = "elasticsearch"; //實例名稱 private static final String IP = "127.0.0.1"; //private static final String IP = "192.168.0.29"; private static final int PORT = 9300; //端口 //1.設置集羣名稱:默認是elasticsearch,並設置client.transport.sniff爲true,使客戶端嗅探整個集羣狀態,把集羣中的其餘機器IP加入到客戶端中 /* //對ES1.6有效 private static Settings settings = ImmutableSettings .settingsBuilder() .put("cluster.name",CLUSTER_NAME) .put("client.transport.sniff", true) .build(); */ //對ES2.0有效 private static Settings settings = Settings.settingsBuilder() .put("cluster.name",CLUSTER_NAME) .put("client.transport.sniff", true) .build(); //建立私有對象 private static TransportClient client; //反射機制建立單例的TransportClient對象 ES1.6版本 // static { // try { // Class<?> clazz = Class.forName(TransportClient.class.getName()); // Constructor<?> constructor = clazz.getDeclaredConstructor(Settings.class); // constructor.setAccessible(true); // client = (TransportClient) constructor.newInstance(settings); // client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), PORT)); // } catch (Exception e) { // e.printStackTrace(); // } // } //ES2.0版本 static { try { client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(IP), PORT)); } catch (UnknownHostException e) { e.printStackTrace(); } } //取得實例 public static synchronized TransportClient getTransportClient(){ return client; } //爲集羣添加新的節點 public static synchronized void addNode(String name){ try { client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(name),9300)); } catch (UnknownHostException e) { e.printStackTrace(); } } //刪除集羣中的某個節點 public static synchronized void removeNode(String name){ try { client.removeTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(name),9300)); } catch (UnknownHostException e) { e.printStackTrace(); } } public static void main(String args[]){ String index="logstash-2016.02.16"; String type="aaa"; //insert(); //search(); //get(); update(index, type, "1"); //delete(); client.close(); } public static void search() { String index="logstash-2016.02.16"; String type="logs"; SearchResponse response=ElkTest.getTransportClient().prepareSearch(index)//設置要查詢的索引(index) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setTypes(type)//設置type, 這個在創建索引的時候同時設置了, 或者能夠使用head工具查看 .setQuery(QueryBuilders.matchQuery("message", "Accept")) //在這裏"message"是要查詢的field,"Accept"是要查詢的內容 .setFrom(0) .setSize(10) .setExplain(true) .execute() .actionGet(); System.out.println("9666666666666"); for(SearchHit hit:response.getHits()){ System.out.println(hit.getSourceAsString()); } } public static void insert() { String json = JSON.toJSONString(new LogModel()); //在這裏建立咱們要索引的對象 IndexResponse response = ElkTest.getTransportClient().prepareIndex("logstash-2016.02.16","logs")//.prepareIndex("twitter", "tweet") //必須爲對象單獨指定ID .setId("1") .setSource(json) .execute() .actionGet(); //屢次index這個版本號會變 System.out.println("response.version():"+response.getVersion()); } public static void update(String index, String type, String id) { Map<String, Object> params = Maps.newHashMap(); params.put("ntitle", "ElasticSearch Server Book"); UpdateResponse response = client.prepareUpdate("library", "book", "2") .execute().actionGet(); } public static void get() { GetResponse response = client.prepareGet("twitter", "tweet", "1") .execute().actionGet(); System.out.println("response.getId():"+response.getId()); System.out.println("response.getSourceAsString():"+response.getSourceAsString()); } public static void delete() { //在這裏建立咱們要索引的對象 DeleteResponse response = client.prepareDelete("twitter", "tweet", "1") .execute().actionGet(); System.out.println(response.getId()); System.out.println(JSON.toJSONString(response.getHeaders())); } }
sdf json