標籤:資料 虛擬 dock min 列印 類型 plugin turn depend
Elasticsearch Java API (二): index建立刪除 cluster管理
elastic官網有權威的java api 英文的 需要耐心看 這裡整理下基本操作
建立maven工程添加依賴
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.locationtech.spatial4j</groupId> <artifactId>spatial4j</artifactId> <version>0.6</version> </dependency> <dependency> <groupId>com.vividsolutions</groupId> <artifactId>jts</artifactId> <version>1.13</version> <exclusions> <exclusion> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>delete-by-query</artifactId> <version>2.4.1</version> </dependency>
首先自訂一個client用於串連es
/** * Created by forgeeks at 2017-03-22 18:27 */public class MyClient { private Settings settings; private TransportClient client; private IndexResponse response; public IndexResponse getResponse() { return response; } public void setResponse(IndexResponse response) { this.response = response; } public TransportClient getClient() { return client; } public MyClient(String clusterName, String inetAddress, Integer port) throws UnknownHostException { settings = Settings.builder().put("cluster.name", clusterName).build(); client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(inetAddress), port)); } public void close(){ getClient().close(); }}
串連方法很簡單,照著官方的抄就可以了
接下來看看串連的cluster狀態
/** * 列印叢集健康狀態 * * @param client */ public static void printClustesInfo(MyClient client) { ClusterHealthResponse healths = client.getClient().admin().cluster().prepareHealth().get(); Map<String, ClusterIndexHealth> map = healths.getIndices(); String clusterName = healths.getClusterName(); int numberOfDataNodes = healths.getNumberOfDataNodes(); int numberOfNodes = healths.getNumberOfNodes(); System.out.println("叢集名稱: " + clusterName + " 資料節點數:" + numberOfDataNodes + " 得到節點數:" + numberOfNodes + " 索引數:" + map.size()); for (String key : map.keySet()) { System.out.print(key + " "); } }
再看下怎麼建立index,首先要明白像資料庫一樣,如果已存在還建立就會拋異常,所以先檢查
/**判斷index是否存在 * * @param client * @param index * @return */ public static boolean indexExists(MyClient client, String index) { IndicesExistsRequest request = new IndicesExistsRequest(index); IndicesExistsResponse response = client.getClient().admin().indices().exists(request).actionGet(); if (response.isExists()) return true; else return false; } /** * 建立索引和類型 * * @param client * @param index * @param type * @throws IOException */ public static void createIndex(MyClient client, String index, String type) throws IOException { if (indexExists(client, index)) { System.out.println("index 已存在"); return; } IndexResponse response = client.getClient().prepareIndex(index, type) .setSource(jsonBuilder().startObject().endObject()).get(); client.setResponse(response); System.out.print(response.toString() + "索引成功建立"); }
建立成功console會有資訊
或者敲命令列 要注意的是win和linux下curl 不一定能用 只有安裝了才能用 win下的即使能用也不要用 不是針對 建個虛擬機器 按(一)的方法再跑一台es 叢集設定下 後面再說
叢集 再用xshell連 可以直接用linux下的curl 然後會徹底解放
[[email protected] config]# curl -XGET 132.126.3.180:9200/_cat/indices?v
好了,接下來是(三)搜尋
Elasticsearch Java API (二): index建立刪除 cluster管理