一
引入所需的Jar包,这里使用的是5.1.2的版本
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.1.2</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.1.2</version> </dependency>
二
package com.hoss.jobs.elasticSearch; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.hoss.biz.service.elasticsearch.ElasticsearchData; import com.hoss.jpa.dto.elasticSearchData.ElasticSearchDto; 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.index.query.RangeQueryBuilder; import org.elasticsearch.search.SearchHits; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.*; @Component public class ElasticSearchComponent { private static String host; //服务器地址 private static Integer port; //端口 private static String custerName;//es集群名称 private Map<String, TransportClient> clientMap = new HashMap<>(); public void init() { String[] split = host.split(","); Map<String, Integer> ips = new HashMap<>(); ips.put(s, port); Settings settings = Settings.builder().put("client.transport.sniff", false).put("cluster.name", custerName).build(); addClient(settings, getAllAddress(ips)); } public void addClient(Settings settings, List<InetSocketTransportAddress> inetSocketTransportAddresses) { TransportClient client = new PreBuiltTransportClient(settings). addTransportAddresses(inetSocketTransportAddresses.toArray(new InetSocketTransportAddress[inetSocketTransportAddresses.size()])); clientMap.put(settings.get("cluster.name"), client); } private List<InetSocketTransportAddress> getAllAddress(Map<String, Integer> ips) { ArrayList<InetSocketTransportAddress> inetSocketList = new ArrayList<>(); for (String ip : ips.keySet()) { try { inetSocketList.add(new InetSocketTransportAddress(InetAddress.getByName(ip), ips.get(ip))); } catch (UnknownHostException e) { e.printStackTrace(); } } return inetSocketList; } private TransportClient getClient(String custername) { return clientMap.get(custername); } public TransportClient getClient() { return getClient(custerName); } public void getElasticSearchCount() { init(); SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String format = df.format(new Date()); format = format.substring(0, 8) + "000000"; String nowTime = df.format(new Date()); TransportClient client = getClient(); RangeQueryBuilder format1 = QueryBuilders.rangeQuery("SS_END_TIME").from(format).to(nowTime);//创建查询条件 SearchHits hits = client.prepareSearch("xxxxxxxxx") .setTypes("leixing").setQuery(format1) .setFrom(0).setSize(2000).get().getHits();//获取查询结果集 for (int j = 0; j < hits.getHits().length; j++) { String jsonstr = hits.getHits()[j] .getSourceAsString(); JSONObject json_1 = JSON.parseObject(jsonstr); Integer ziduan_1 = (Integer) json_1.get("ziduan_1"); Integer ziduan_2 = (Integer) json_1.get("ziduan_2"); String ziduan_3 = (String) json_1.get("ziduan_3"); } } }