ElasticSearchHandler.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.yihu.quota.util;
  2. import com.yihu.quota.model.rest.FieldInfo;
  3. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  4. import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
  5. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
  6. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
  7. import org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest;
  8. import org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse;
  9. import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
  10. import org.elasticsearch.client.Requests;
  11. import org.elasticsearch.client.transport.TransportClient;
  12. import org.elasticsearch.common.settings.Settings;
  13. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  14. import org.elasticsearch.common.xcontent.XContentBuilder;
  15. import org.elasticsearch.common.xcontent.XContentFactory;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.stereotype.Component;
  21. import java.io.IOException;
  22. import java.net.InetAddress;
  23. import java.net.UnknownHostException;
  24. import java.util.List;
  25. /**
  26. * Created by janseny on 2018/9/17.
  27. */
  28. @Configuration
  29. @Component
  30. public class ElasticSearchHandler {
  31. private static Logger logger = LoggerFactory.getLogger(ElasticSearchHandler.class);
  32. @Value("${elasticsearch.cluster-name}")
  33. private String clusterName;
  34. @Value("${elasticsearch.cluster-nodes}")
  35. private String ip;
  36. /**
  37. * 取得实例
  38. * @return
  39. */
  40. public synchronized TransportClient getTransportClient() {
  41. TransportClient client = null ;
  42. try {
  43. Settings settings = Settings.builder().put("cluster.name", clusterName)
  44. /* .put("client.transport.sniff", true)*/
  45. .put("client.transport.ping_timeout", "30s").build();
  46. String host = ip.split(";")[0];
  47. host = host.substring(0,host.indexOf(":"));
  48. client = TransportClient.builder().settings(settings).build()
  49. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), 9300));
  50. } catch (UnknownHostException e) {
  51. e.printStackTrace();
  52. }
  53. return client;
  54. }
  55. /**
  56. * 关闭连接
  57. * @param client es客户端
  58. */
  59. public void close(TransportClient client) {
  60. client.close();
  61. }
  62. /**
  63. * 为集群添加新的节点
  64. * @param nodeName
  65. * @param client es客户端
  66. */
  67. public synchronized void addNode(String nodeName,TransportClient client) {
  68. try {
  69. client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(nodeName), 9300));
  70. close(client);
  71. } catch (UnknownHostException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. /**
  76. * 删除集群中的某个节点
  77. * @param client es客户端
  78. * @param name
  79. */
  80. public synchronized void removeNode(String name,TransportClient client) {
  81. try {
  82. client.removeTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(name), 9300));
  83. close(client);
  84. } catch (UnknownHostException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. /**
  89. * 创建索引
  90. *
  91. * @param index 索引名称
  92. * @param client es客户端
  93. */
  94. public void createIndex(String index,TransportClient client) {
  95. CreateIndexRequest request = new CreateIndexRequest(index);
  96. client.admin().indices().create(request);
  97. close(client);
  98. }
  99. /**
  100. * 判断指定的索引名是否存在
  101. * @param indexName 索引名
  102. * @return 存在:true; 不存在:false;
  103. */
  104. public boolean isExistsIndex(String indexName,TransportClient client){
  105. IndicesExistsResponse response = client.admin().indices().exists(
  106. new IndicesExistsRequest().indices(new String[]{indexName})).actionGet();
  107. close(client);
  108. return response.isExists();
  109. }
  110. /**
  111. * 判断指定的索引的类型是否存在
  112. * @param indexName 索引名
  113. * @param indexType 索引类型
  114. * @return 存在:true; 不存在:false;
  115. */
  116. public boolean isExistsType(String indexName,String indexType,TransportClient client){
  117. TypesExistsResponse response = client.admin().indices()
  118. .typesExists(new TypesExistsRequest(new String[]{indexName}, indexType)).actionGet();
  119. boolean isEXist = response.isExists();
  120. close(client);
  121. return isEXist;
  122. }
  123. /**
  124. * 创建mapping
  125. * @param index 索引
  126. * @param type 类型
  127. * @param client es客户端
  128. * @param xMapping mapping描述
  129. */
  130. public void createBangMapping(String index, String type, XContentBuilder xMapping,TransportClient client) {
  131. PutMappingRequest mapping = Requests.putMappingRequest(index).type(type).source(xMapping);
  132. client.admin().indices().putMapping(mapping).actionGet();
  133. close(client);
  134. }
  135. /**
  136. * 根据信息自动创建索引与mapping
  137. * 构建mapping描述 有问题
  138. * @param fieldInfoList
  139. * @param client es客户端
  140. * @return
  141. */
  142. public void createIndexAndCreateMapping(String index, String type,List<FieldInfo> fieldInfoList,TransportClient client) {
  143. XContentBuilder mapping = null;
  144. try {
  145. CreateIndexRequestBuilder cib = client.admin()
  146. .indices().prepareCreate(index);
  147. mapping = XContentFactory.jsonBuilder()
  148. .startObject().startObject("properties"); //设置之定义字段
  149. for(FieldInfo info : fieldInfoList){
  150. String field = info.getField();
  151. String dataType = info.getDataType();
  152. if(dataType == null || "".equals(dataType.trim())){
  153. dataType = "String";
  154. }
  155. dataType = dataType.toLowerCase();
  156. Integer participle = info.getParticiple();
  157. if("string".equals(dataType)){
  158. mapping.startObject(field).field("type", "string").field("index", "not_analyzed").endObject();
  159. }else if("date".equals(dataType)){
  160. mapping.startObject(field).field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject();
  161. }else if("object".equals(dataType)){//对象属性
  162. mapping.startObject(field).startObject("properties");
  163. //子属性
  164. List<FieldInfo> childFieldList = info.getFieldInfos();
  165. for(FieldInfo child : childFieldList){
  166. String childField = child.getField();
  167. String childDataType = child.getDataType();
  168. int childParticiple = child.getParticiple();
  169. if("string".equals(childDataType)){
  170. mapping.startObject(childField).field("type", "string").field("index", "not_analyzed").endObject();
  171. }else if("date".equals(dataType)){
  172. mapping.startObject(childField).field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject();
  173. }else {
  174. mapping.startObject(childField) .field("type", childDataType).endObject();
  175. }
  176. }
  177. mapping.endObject().endObject();
  178. }else if("nested".equals(dataType)){//子集属性
  179. mapping.startObject(field).field("type", "nested").startObject("properties");
  180. //子属性
  181. List<FieldInfo> childFieldList = info.getFieldInfos();
  182. for(FieldInfo child : childFieldList){
  183. String childField = child.getField();
  184. String childDataType = child.getDataType();
  185. int childParticiple = child.getParticiple();
  186. if("string".equals(childDataType)){
  187. mapping.startObject(childField).field("type", "string").field("index", "not_analyzed").endObject();
  188. }else if("date".equals(dataType)){
  189. mapping.startObject(childField).field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject();
  190. }else {
  191. mapping.startObject(childField) .field("type", childDataType).endObject();
  192. }
  193. }
  194. mapping.endObject().endObject();
  195. }else {
  196. mapping.startObject(field) .field("type", dataType).endObject();
  197. }
  198. }
  199. mapping.endObject()
  200. .endObject();
  201. cib.addMapping(type, mapping);
  202. cib.execute().actionGet();
  203. close(client);
  204. } catch (IOException e) {
  205. logger.debug("创建索引发生异常");
  206. }
  207. }
  208. /**
  209. * 根据模板自动创建索引与mapping
  210. * @param index 索引字段
  211. * @param type 类型
  212. * @param client 客户端
  213. * @throws IOException
  214. */
  215. public void createMappingByTemplate(String index, String type,TransportClient client) throws IOException {
  216. CreateIndexRequestBuilder cib=client.admin()
  217. .indices().prepareCreate(index);
  218. XContentBuilder mapping = XContentFactory.jsonBuilder()
  219. .startObject()
  220. .startObject("properties") //设置之定义字段
  221. .startObject("id") .field("type", "integer").field("index", "not_analyzed").endObject()
  222. .startObject("classs").field("type", "integer").field("index", "not_analyzed").endObject()
  223. .startObject("score").field("type", "integer").field("index", "not_analyzed").endObject()
  224. //子属性
  225. .startObject("student")
  226. .startObject("properties")
  227. .startObject("name").field("type", "string").field("index", "not_analyzed").endObject()
  228. .startObject("age").field("type", "string").endObject()
  229. .endObject()
  230. .endObject()
  231. .startObject("updatetime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
  232. .endObject()
  233. .endObject();
  234. cib.addMapping(type, mapping);
  235. cib.execute().actionGet();
  236. close(client);
  237. }
  238. }