ElsticSearch的7.0.0用java的HightClient远程连接集群

1.导入pom包

        <!--es-->
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.0.0</version>
        </dependency>
<!--        heightClient-->
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.0.0</version>
        </dependency>

2.代码 :连接集群以及单节点

package com.zhangzeyuan.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.client.*;

import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @Author zeyuan zhang
 * @Date 2024/12/19 13:22
 * @Version 1.0
 */

public class TestHeightClient {
    @Test
    public void testES() throws IOException {
        //单节点查询:
//        RestHighLevelClient highClient = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("localhost", 9290, "http")
//                )
//        );
        //配置一个集群查询
       // String clusterName = "myes-cluster";clusterName
        // 定义多个初始主机地址 这里的ip要和真实的节点和端口号对应上,es默认的port是9200
        HttpHost httpHost1 = new HttpHost("localhost", 9290, "http");
        HttpHost httpHost2 = new HttpHost("localhost", 9291, "http");
        HttpHost httpHost3 = new HttpHost("localhost", 9292, "http");
        // 创建 RestClientBuilder,设置初始主机地址
        RestClientBuilder builder = RestClient.builder(httpHost1,httpHost2,httpHost3);

        RestHighLevelClient highClient = new RestHighLevelClient(builder);
        GetIndexResponse _cattestIndex = highClient.indices().get(
                new GetIndexRequest().indices("test_route"), RequestOptions.DEFAULT
        );

        System.out.println(_cattestIndex.toString());
        highClient.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值