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

}
相关推荐
Elasticsearch1 天前
用 AI 驱动的威胁狩猎提升公共部门的网络防御
elasticsearch
yumgpkpm1 天前
CMP(类Cloudera CDP 7.3 404版华为Kunpeng)与其他大数据平台对比
大数据·hive·hadoop·elasticsearch·kafka·hbase·cloudera
秃了也弱了。1 天前
elasticSearch之java客户端详细使用:文档搜索API
java·elasticsearch
yumgpkpm1 天前
Hadoop在AI时代如何实现生态协同? CMP 7.13(或类 Cloudera CDP7.3 的 CMP 7.13 平台,如华为鲲鹏 ARM 版)
大数据·hadoop·elasticsearch·zookeeper·kafka·hbase·cloudera
Elasticsearch1 天前
Elasticsearch 的结构化文档配置 - 递归分块实践
elasticsearch
Elasticsearch1 天前
在 Elasticsearch 中为结构化文档配置递归分块
elasticsearch
失散131 天前
分布式专题——53 ElasticSearch高可用集群架构实战
java·分布式·elasticsearch·架构
LSL666_2 天前
5 Repository 层接口
android·运维·elasticsearch·jenkins·repository
Elastic 中国社区官方博客2 天前
Elasticsearch:如何为 Elastic Stack 部署 E5 模型 - 下载及隔离环境
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
high20112 天前
【Git】-- Rebase 减少 Commit 次数指南
大数据·git·elasticsearch