ElastaticSearch ---es客户端 TransportClient

TransportClient

TransportClient客户端,官方在es 7.0版本中将弃用TransportClient客户端,且在8.0版本中完全移除它.

es 7.0及以上的版本,请使用 RestHighLevelClient。

如果项目中使用的es版本不高,可以使用 TransportClient。

依赖

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>transport-netty4-client</artifactId>
            <version>5.5.1</version>
        </dependency>

初始化TransportClient 客户端

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;
import java.net.UnknownHostException;


public class EsTransportClient {

    /**
     * 初始化TransportClient 客户端
     *
     * @param clusterName es集群名称
     * @param ip es的ip地址
     * @param port es的端口
     * @return
     */
    public static TransportClient getClient(String clusterName, String ip, int port) {
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();

        TransportClient transportClient = null;
        try {
            transportClient = new PreBuiltTransportClient(settings);
            transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
            return transportClient;
        } catch (UnknownHostException e) {
            log.error("UnknownHostException exception.");
        }
        return transportClient;
    }


    /**
     * 多节点的es集群
     * @param nodes 格式为:ip:端口,多个节点用逗号隔开  ,比如 10.123.666.1:9300,10.123.666.2:9300
     * @param clusterName 集群名称
     * @return
     */
    public static TransportClient getClientByNodes(String nodes, String clusterName) {
        String[] hosts = nodes.split(",");
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .put("client.transport.ping_timeout", "10s")
                .build();
        TransportClient esClient = null;

        try {
            esClient = new PreBuiltTransportClient(settings);
            for (String host : hosts) {
                String[] hostAndPort = host.split(":");
                esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndPort[0]),
                            Integer.parseInt(hostAndPort[1])));
            }
        } catch (NumberFormatException | UnknownHostException e) {
            log.error("getClientByNodes exception.", e);
        }
        return esClient;
    }

}
相关推荐
Lill_bin3 小时前
深入理解ElasticSearch集群:架构、高可用性与数据一致性
大数据·分布式·elasticsearch·搜索引擎·zookeeper·架构·全文检索
RwTo5 小时前
Elasticsearch 聚合搜索
大数据·elasticsearch·搜索引擎·全文检索
求学小火龙6 小时前
ElasticSearch介绍+使用
java·大数据·elasticsearch
檀越剑指大厂6 小时前
【Elasticsearch系列六】系统命令API
大数据·elasticsearch·搜索引擎
bug菌¹12 小时前
滚雪球学SpringCloud[5.1讲]: Spring Cloud Config详解
spring·elasticsearch·spring cloud
Li小李同学Li12 小时前
git学习【持续更新中。。。】
git·学习·elasticsearch
nangonghen13 小时前
通过logstash同步elasticsearch数据
大数据·elasticsearch
檀越剑指大厂13 小时前
【Elasticsearch系列四】ELK Stack
大数据·elk·elasticsearch
2401_8401922714 小时前
ELFK日志分析平台,架构和通信
elk·elasticsearch·架构
cyt涛1 天前
搜索功能技术方案
mysql·elasticsearch·全文检索·canal·索引·数据同步·搜索