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

}
相关推荐
彭于晏Yan29 分钟前
Springboot实现连接多个ElasticSearch数据库
数据库·spring boot·elasticsearch
何中应1 小时前
Easy-Es整合
大数据·spring boot·elasticsearch·搜索引擎
float_六七2 小时前
Git忽略规则终极指南
大数据·git·elasticsearch
摇滚侠3 小时前
ElasticSearch 是干什么的,从百度搜索、B 站搜索功能、京东搜索功能,淘宝搜索功能,理解 ElasticSearch 实现了什么功能
elasticsearch·百度·dubbo
灵机一物5 小时前
灵机一物AI智能电商小程序(已上线)-从零构建高可用智能电商客服:LangGraph+LlamaIndex+ES三层检索RAG系统实战|彻底解决大模型幻觉
elasticsearch·大模型实战·rag·llamaindex·langgraph·电商客服·零幻觉问答
凸头7 小时前
RedisSearch 和 Elasticsearch 的 HNSW向量索引对比
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客16 小时前
Elastic 为什么捐赠其 OpenTelemetry PHP 发行版
大数据·开发语言·elasticsearch·搜索引擎·信息可视化·全文检索·php
wuyikeer20 小时前
Java进阶(ElasticSearch的安装与使用)
java·elasticsearch·jenkins
我爱学习好爱好爱1 天前
Logstash 数据管道测试案例:从 Filebeat 接收日志并输出至黑屏幕与 Elasticsearch(基于Rocky Linux 9.6)
大数据·linux·elasticsearch