es搭建与集成

本文档展示了如何配置和使用Elasticsearch。首先提供了Elasticsearch的访问和安装地址,接着详细说明了一个ElasticSearchFactory配置类,包括建立TCP连接、设置集群名称等。此外,还提供了一个ElasticSearchUtil工具类,用于数据的插入和批量插入,通过TransportClient进行操作。
摘要由CSDN通过智能技术生成

es访问地址:http://192.168.231.40:9200/
es安装地址:https://blog.csdn.net/zerokissingthefire/article/details/100104607
es集成地址:https://www.cnblogs.com/zhoutao825638/p/10382046.html

-----------------------配置类-----------------------------------------------------------------------------
import org.apache.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

/**

  • @Author xuconghui

  • @Date 2020/10/13 16:38

  • @Version 1.0
    **/
    public class ElasticSearchFactory {
    private static final Logger log = Logger.getLogger(ElasticSearchFactory.class);
    private static final String ES_CLUTER_NAME = “dunhuang-application”;
    private static final int ES_CONNECT_PORT = 9300;
    private static TransportClient client=null;
    private ElasticSearchFactory(){}

    private static final class ElasticSearchFactoryInner{
    private static final ElasticSearchFactory elasticSearchFactory = new ElasticSearchFactory();
    }

    public static ElasticSearchFactory get(){
    return ElasticSearchFactoryInner.elasticSearchFactory;
    }
    //es连接配置
    public TransportClient buildElasticSearchFactoryConfig() {
    try {
    //注意这里的端口是TCP端口9300,而非HTTP接口9200
    InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName(“192.168.231.40”),ES_CONNECT_PORT);
    //机器名称,可以首页查询,这个不能出现错误,否则无法连接ES
    Settings settings=Settings.builder().put(“cluster.name”,ES_CLUTER_NAME).build();
    //.addTransportAddress()可以配置多个地址
    client = new PreBuiltTransportClient(settings).addTransportAddress(node);
    return client;
    } catch (Exception e) {
    log.error(“ElasticSearchFactory.buildElasticSearchFactoryConfig es连接异常…”);
    }
    return null;
    }
    void closeElasticSearch(){
    if(client!=null){
    client.close();
    }
    }

}
--------------------------------业务类----------------------------------------------------------------------------------
import org.apache.log4j.Logger;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**

  • @Author xuconghui

  • @Date 2020/10/13 17:38

  • @Version 1.0 es增删改查工具类
    **/
    public class ElasticSearchUtil {
    private static final Logger log = Logger.getLogger(ElasticSearchUtil.class);
    private static final String ES_INDEX = “tao”;
    private static final String ES_TYPE = “book”;

    /**

    • 插入一条数据到es
      */
      public static String addSingleToES(Map<String, Object> reqData,String idKey) {
      if (null == reqData||StringUtil.isEmpty(idKey)) {
      return “”;
      }
      IndexResponse indexResponse = ElasticSearchFactory.get().buildElasticSearchFactoryConfig().prepareIndex(ES_INDEX, ES_TYPE).setSource(getJsonReqData(reqData)).setId(String.valueOf(reqData.get(idKey))).get();
      ElasticSearchFactory.get().closeElasticSearch();
      return indexResponse.getId();
      }

    /**

    • 批量插入多条数据
      /
      public static void addMoreToES(List<Map<String, Object>> reqdataList,String idKey) {
      if(null==reqdataList||reqdataList.size()==0|| StringUtil.isEmpty(idKey)){
      return;
      }
      TransportClient transportClient = ElasticSearchFactory.get().buildElasticSearchFactoryConfig();
      BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
      for (Map<String, Object> map : reqdataList) {
      bulkRequest.add(transportClient.prepareIndex(ES_INDEX, ES_TYPE).setSource(getJsonReqData(map)).setId(String.valueOf(map.get(idKey))));
      }
      bulkRequest.execute().actionGet();
      ElasticSearchFactory.get().closeElasticSearch();
      }
      /
      *
    • 更新一条数据
    • */
      public static void updateDataById(Map<String, Object> reqData,String idKey){

    }
    private static XContentBuilder getJsonReqData(Map<String, Object> reqData) {
    XContentBuilder xContentBuilder = null;
    try {
    xContentBuilder = XContentFactory.jsonBuilder().startObject();
    for (String key : reqData.keySet()) {
    xContentBuilder.field(key, reqData.get(key));
    }
    xContentBuilder.endObject();
    } catch (IOException e) {
    log.error(“ElasticSearchUtil.getJsonReqData reqData 转换json异常”);
    }
    return xContentBuilder;
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值