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;
    }

}
相关推荐
橘子在努力5 小时前
【橘子ES】如何本地调试ES源码
elasticsearch·搜索引擎
柳贯一(逆流河版)7 小时前
ElasticSearch 实战:全文检索与数据聚合分析的完整指南
大数据·elasticsearch·全文检索
Jammingpro8 小时前
【Git版本控制】Git初识、安装、仓库初始化与仓库配置(含git init、git config与配置无法取消问题)
java·git·elasticsearch
失散1311 小时前
分布式专题——44 ElasticSearch安装
java·分布式·elasticsearch·架构
arvin_xiaoting13 小时前
#zsh# #Ubuntu# 一键安装zsh、oh-my-zsh、常用插件
linux·ubuntu·elasticsearch
bemyrunningdog14 小时前
IntelliJ IDEA合并分支到master全攻略
大数据·elasticsearch·intellij-idea
dessler15 小时前
Elasticsearch(ES)分片(Shard)和 副本分片(Replica Shard)
linux·运维·elasticsearch
paj12345678916 小时前
elasticsearch-8.12.2集群部署
java·elasticsearch
liliangcsdn1 天前
如何基于ElasticsearchRetriever构建RAG系统
大数据·elasticsearch·langchain
相与还1 天前
IDEA和GIT实现cherry pick拣选部分变更到新分支
git·elasticsearch·intellij-idea