Java连接ES数据源获取数据

引入所需的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");
        }
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值