ElasticSearch系列十二:掌握ES使用Java API
阿新 • • 發佈:2019-02-01
一、Java連線ElasticSearch6.x版本(可整合到spring中)
<dependencies> <!-- ES --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.2.4</version> </dependency> <!-- es升級需要依賴的 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.2.4</version> </dependency> <!--ES用到的日誌--> <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> <!--Json--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency> </dependencies>
package com.neusoft; import java.io.Serializable; /** * @author XiaoLuo * @date 2018/6/15 15:04 * @Description: ES基本資訊實體類 */ public class EsBeanParent implements Serializable { String indexName; String indexType; int shards; int replicas; String refreshInterval; String indexStoreType; String alias; boolean autoCreateIndex; String primaryFiled; public EsBeanParent() { } public String getIndexName() { return this.indexName; } public void setIndexName(String indexName) { this.indexName = indexName; } public String getIndexType() { return this.indexType; } public void setIndexType(String indexType) { this.indexType = indexType; } public int getShards() { return this.shards; } public void setShards(int shards) { this.shards = shards; } public int getReplicas() { return this.replicas; } public void setReplicas(int replicas) { this.replicas = replicas; } public String getRefreshInterval() { return this.refreshInterval; } public void setRefreshInterval(String refreshInterval) { this.refreshInterval = refreshInterval; } public String getIndexStoreType() { return this.indexStoreType; } public void setIndexStoreType(String indexStoreType) { this.indexStoreType = indexStoreType; } public String getAlias() { return this.alias; } public void setAlias(String alias) { this.alias = alias; } public boolean isAutoCreateIndex() { return this.autoCreateIndex; } public void setAutoCreateIndex(boolean autoCreateIndex) { this.autoCreateIndex = autoCreateIndex; } public String getPrimaryFiled() { return this.primaryFiled; } public void setPrimaryFiled(String primaryFiled) { this.primaryFiled = primaryFiled; } }
package com.neusoft; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpHost; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author XiaoLuo * @date 2018/6/15 15:04 * @Description: 建立連線封裝類 */ public class EsClientFactory { private static final String SCHEMA = "http"; private static final int CONNECT_TIME_OUT = 1000; private static final int SOCKET_TIME_OUT = 30000; private static final int CONNECTION_REQUEST_TIME_OUT = 500; private static final int MAX_CONNECT_NUM = 100; private static final int MAX_CONNECT_PER_ROUTE = 100; private static HttpHost HTTP_HOST=null; private static boolean uniqueConnectTimeConfig = false; private static boolean uniqueConnectNumConfig = true; private static RestClientBuilder builder; private static RestHighLevelClient restHighLevelClient; public static void init(){ if(HTTP_HOST==null){ HTTP_HOST=new HttpHost("127.0.0.1",9200,SCHEMA); } builder = RestClient.builder(HTTP_HOST); if(uniqueConnectTimeConfig){ setConnectTimeOutConfig(); } if(uniqueConnectNumConfig){ setMutiConnectConfig(); } restHighLevelClient = new RestHighLevelClient(builder); } /** * 主要關於非同步httpclient的連線延時配置 */ public static void setConnectTimeOutConfig(){ builder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectTimeout(CONNECT_TIME_OUT); requestConfigBuilder.setSocketTimeout(SOCKET_TIME_OUT); requestConfigBuilder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIME_OUT); return requestConfigBuilder; }); } /** * 主要關於非同步httpclient的連線數配置 */ public static void setMutiConnectConfig(){ builder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setMaxConnTotal(MAX_CONNECT_NUM); httpClientBuilder.setMaxConnPerRoute(MAX_CONNECT_PER_ROUTE); return httpClientBuilder; }); } public static RestHighLevelClient getHighLevelClient(){ if(restHighLevelClient==null){ init(); } return restHighLevelClient; } public static void close() { if (restHighLevelClient != null) { try { restHighLevelClient.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 單個插入 * @return */ public static Object postRequest(Map entity, EsBeanParent esBeanParent){ try { // 構建請求 IndexRequest request = new IndexRequest(esBeanParent.getIndexName(), esBeanParent.getIndexType(),esBeanParent.getPrimaryFiled()); // 將儲存資料以JSON格式關聯到請求 String jsonString = JSONObject.toJSONString(entity); request.source(jsonString,XContentType.JSON); // Java客戶端發起儲存資料請求 IndexResponse response = getHighLevelClient().index(request); return "ok"; }catch (Exception e){ e.printStackTrace(); } return null; } /** * 獲取單個 * @param id * @param esBeanParent * @return */ public static String getSourceRequest(String id, EsBeanParent esBeanParent){ try { GetRequest getRequest = new GetRequest(esBeanParent.getIndexName(), esBeanParent.getIndexType(), id); GetResponse getResponse = getHighLevelClient().get(getRequest); return getResponse.getSourceAsString(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 批量插入 * @param list * @param esBeanParent * @return */ public static Object bulkRequest(List<Map> list, EsBeanParent esBeanParent){ BulkRequest bulkRequest = new BulkRequest(); for (Map map : list) { IndexRequest indexRequest = new IndexRequest(esBeanParent.getIndexName(), esBeanParent.getIndexType(),esBeanParent.getPrimaryFiled()); indexRequest.source(map); bulkRequest.add(indexRequest); } try { BulkResponse bulkResponse = getHighLevelClient().bulk(bulkRequest); return "ok"; } catch (IOException e) { e.printStackTrace(); } return null; } /** * 單個修改 * @return */ public static Object updateRequest(Map entity, EsBeanParent esBeanParent){ Object id = entity.get(esBeanParent.getPrimaryFiled()); try { RestHighLevelClient client = getHighLevelClient(); UpdateRequest updateRequest = new UpdateRequest(esBeanParent.getIndexName(), esBeanParent.getIndexType(), id.toString()); updateRequest.doc(entity); UpdateResponse updateResponse = client.update(updateRequest); return "ok"; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 測試方法 * @param args */ public static void main(String[] args) { EsBeanParent esBeanParent = new EsBeanParent(); esBeanParent.setIndexName("company"); esBeanParent.setIndexType("employee"); esBeanParent.setPrimaryFiled("1"); Map map = new HashMap(); map.put("id","1"); map.put("name","xiaoluo"); postRequest(map, esBeanParent); String sourceRequest = getSourceRequest("1", esBeanParent); System.out.println(sourceRequest); } }