Elasticsearch教程(六) elasticsearch Client创建

Elasticsearch  创建Client有几种方式。

首先在 Elasticsearch  的配置文件 elasticsearch.yml中。定义cluster.name。如下:

cluster.name: sojson-application

创建方式一:

import static org.elasticsearch.node.NodeBuilder.*;
//节点方式创建。
Node node = nodeBuilder().clusterName("yourclustername").node();
Client client = node.client();
/*
还有很多节点方式的创建方式,查看下面的官网地址。
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/node-client.html
*/

创建方式二:

/**
 * 指定 ip地址创建
 */
// on startup
Client client = TransportClient.builder().build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));

// on shutdown
client.close();

创建方式三:

//按集群名称创建
Settings settings = Settings.settingsBuilder()
        .put("cluster.name", "sojson-application").build();
Client client = TransportClient.builder().settings(settings).build();
//Add transport addresses and do something with the client...

创建方式四:

//同一内网Ip段,嗅的方式自己查找,组成集群。
Settings settings = Settings.settingsBuilder()
        .put("client.transport.sniff", true).build();
TransportClient client = TransportClient.builder().settings(settings).build();
/*
客户端允许嗅其余的集群,它将数据节点添加到列表的机器使用。在这种情况下要注意,将使用的IP地址的其他节点开始(“publish”地址)。启用它,设置client.transport.sniff为 true:
*/

其实还有很多方式。具体使用哪种,看自己需求

工具类:

package com.sojson.core.elasticsearch.utils;

import java.net.InetAddress;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import com.sojson.common.utils.LoggerUtils;
import com.sojson.core.config.IConfig;

public class ESTools {
    
    public final static Client client =  build();
    
    public final static Class clazz = ESTools.class;
    

    
    /**
     * 创建一次
     * @return
     */
    private static Client build(){
        if(null != client){
            return client;
        }
        Client client = null;
        String ip = IConfig.get("es_ip");
        LoggerUtils.fmtDebug(clazz, "获取ESIP地址:%s", ip);
        try {
            LoggerUtils.fmtDebug(clazz, "创建Elasticsearch Client 开始");
            Settings settings = Settings
                .settingsBuilder()
                    .put("cluster.name","sojson-application")
                        .put("client.transport.sniff", true)
                            .build();
            client = TransportClient.builder().settings(settings).build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), 9300));
            LoggerUtils.fmtDebug(clazz, "创建Elasticsearch Client 结束");
        } catch (Exception e) {
            LoggerUtils.fmtError(clazz, e, "创建Client异常");
        }
        return client;
    }
    
    /**
     * 关闭
     */
    public static void close(){
        if(null != client){
            try {
                client.close();
            } catch (Exception e) {
                
            }
        }
    }
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值