springboot對ES的集成並不友好,springboot2.0版本才支持ES 5.6的,並且查詢工具並不友好,偶然發現ES能夠按照sql插件,因而想到如下方案node
/** * elasticsearch工具 * * @author QIUQB */ @Component public class EsClient { private static final Logger log = LoggerFactory.getLogger(EsClient.class); @Autowired private HttpClientUtil httpClientUtil; @Autowired private ElasticsearchTemplate esTemplate; @Value("${spring.elasticsearch.properties.host}") private String hostname; @Value("${spring.elasticsearch.properties.port}") private int port; @Value("${spring.elasticsearch.properties.sql-port}") private int sqlPort; /** * * @param sql * @param clazz * @return * @throws IOException * @throws URISyntaxException */ public <T> List<T> queryForList(String sql,Class<T> clazz) throws IOException, URISyntaxException{ String url = "http://"+hostname+":"+sqlPort+"/_sql"; Map<String,String> params = new HashMap<String,String>(); params.put("sql", sql); String result = httpClientUtil.doGet(url,params); log.info("es查詢結果:"+result); ElasticSearchQueryModel model = JsonUtil.json2Bean(result, new TypeReference<ElasticSearchQueryModel>() {}); List<HitsBean> hits = model.getHits().getHits(); List<T> results = new ArrayList<T>(); if(null!=hits&&hits.size()>0) { for(HitsBean hit : hits) { results.add(JsonUtil.json2Bean(hit.get_source(), clazz)); } } return results; } /** * * @param sql * @param clazz * @return * @throws IOException * @throws URISyntaxException */ public <T> Page<T> page(String sql,Class<T> clazz,Integer pageSize,Integer pageNo) throws IOException, URISyntaxException{ Page<T> page = new Page<T>(pageSize,pageNo); String url = "http://"+hostname+":"+sqlPort+"/_sql"; Map<String,String> params = new HashMap<String,String>(); params.put("sql", sql+" limit "+page.getPageSize()+" offset "+page.getFirstEntityIndex()); String result = httpClientUtil.doGet(url,params); log.info("es查詢結果:"+result); ElasticSearchQueryModel model = JsonUtil.json2Bean(result, new TypeReference<ElasticSearchQueryModel>() {}); List<HitsBean> hits = model.getHits().getHits(); List<T> results = new ArrayList<T>(); if(null!=hits&&hits.size()>0) { for(HitsBean hit : hits) { results.add(JsonUtil.json2Bean(hit.get_source(), clazz)); } } page.setData(results); page.setDataCount(model.getHits().getTotal()); return page; } /** * 集羣的基本屬性 * @return */ public ESBasicDetail getEsBasicDetail() { NodesInfoResponse nodeInfo = esTemplate.getClient().admin().cluster().prepareNodesInfo().execute().actionGet(); DiscoveryNode node2 = nodeInfo.getNodes().get(0).getNode(); ESBasicDetail es = new ESBasicDetail(); es.setName(node2.getName()); es.setHttpAddress(nodeInfo.getNodes().get(0).getHttp().getAddress().publishAddress().toString()); es.setTransportAddress(node2.getAddress().toString()); es.setVersion(node2.getVersion().toString()); es.setIp(node2.getAddress().getHost()); return es; } /** * 響應的集羣健康值 */ public ClusterHealthResponse getClusterHealthResponse() { ActionFuture<ClusterHealthResponse> health = esTemplate.getClient().admin().cluster().health(new ClusterHealthRequest()); return health.actionGet(); } /** * 集羣健康值詳細信息 */ public ESHealth getClusterHealthStatus() { ClusterHealthResponse clusterHealthResponse = getClusterHealthResponse(); ESHealth health = new ESHealth(); health.setNumberOfNodes(clusterHealthResponse.getNumberOfNodes()); health.setNumberOfDataNodes(clusterHealthResponse.getNumberOfDataNodes()); health.setActiveShards(clusterHealthResponse.getActiveShards()); health.setRelocatingShards(clusterHealthResponse.getRelocatingShards()); health.setActivePrimaryShards(clusterHealthResponse.getActivePrimaryShards()); health.setInitializingShards(clusterHealthResponse.getInitializingShards()); health.setUnassignedShards(clusterHealthResponse.getUnassignedShards()); health.setActiveShardsPercent(clusterHealthResponse.getActiveShardsPercent()); health.setStatus(clusterHealthResponse.getStatus()); health.setIndices(clusterHealthResponse.getIndices()); return health; } /** * 集羣索引信息 */ public List<ESIndexDetail> getESIndexDetails() { ClusterHealthResponse createClusterHealthResponse = getClusterHealthResponse(); Map<String, ClusterIndexHealth> indices = createClusterHealthResponse.getIndices(); List<ESIndexDetail> list = new ArrayList<>(); for (Map.Entry<String,ClusterIndexHealth> map: indices.entrySet()) { ESIndexDetail index = new ESIndexDetail(); //獲取名稱 String indexName = map.getKey(); index.setIndexName(indexName); IndicesAdminClient indicesAdminClient = esTemplate.getClient().admin().indices(); IndicesStatsResponse response1 = indicesAdminClient.prepareStats(indexName).all().get(); CommonStats primaries = response1.getPrimaries(); //獲取文本大小 long count = primaries.docs.getCount(); primaries.getTotalMemory(); index.setDocs(count); list.add(index); } return list; } }