ElasticSearch6.3.1工具封装

package com.es;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ESUtil {

    //集群名
    private static final String CLUSTER_NAME = "my-Es";
    //Es集群中某个节点
    private static final String HOSTNAME = "hdp-1";
    //连接端口号
    private static final int TCP_PORT = 9300;
    //构建setting对象
    private static Settings settings = Settings.builder().put("cluster.name",CLUSTER_NAME).build();
    //TransportClient对象,用于连接ES集群
    private static volatile TransportClient client;

    /**
     * 获取客户端
     * @return
     * @throws Exception
     */
    public static TransportClient getClient() throws Exception {
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        TransportClient client1 = client.addTransportAddresses(new TransportAddress(InetAddress.getByName("hdp-1"), TCP_PORT));
        return client1;
    }

    /**
     * 获取客户端管理员
     * @return
     * @throws Exception
     */
    public static IndicesAdminClient getAdminClient() throws Exception {
        AdminClient admin = getClient().admin();
        IndicesAdminClient indices = admin.indices();
        return indices;
    }

    /**
     * 判断是否存在
     */
    public static boolean isExists(String indexName) throws Exception {
        IndicesExistsRequestBuilder requestBuilder = getAdminClient().prepareExists(indexName);
        IndicesExistsResponse response = requestBuilder.get();
        return response.isExists()?true:false;

    }

    /**
     * 创建索引
     */
    public static boolean createIndex(String indexName) throws Exception {
        CreateIndexRequestBuilder requestBuilder = getAdminClient().prepareCreate(indexName.toLowerCase());
        CreateIndexResponse indexResponse = requestBuilder.get();
        return indexResponse.isAcknowledged()?true:false;
    }

    /**
     * 创建索引
     * @param indexName 索引名
     * @param shards   分片数
     * @param replicas  副本数
     * @return
     */
    public static boolean createIndex(String indexName, int shards, int replicas) throws Exception {
        Settings settings = Settings.builder()
                .put("index.number_of_shards", shards)
                .put("index.number_of_replicas", replicas)
                .build();
        CreateIndexResponse createIndexResponse = getAdminClient()
                .prepareCreate(indexName.toLowerCase())
                .setSettings(settings)
                .execute().actionGet();
        return createIndexResponse.isAcknowledged()?true:false;
    }

    /**
     * 删除索引
     * @param indexName
     * @return
     */
    public static boolean deleteIndex(String indexName) throws Exception {
        DeleteIndexResponse deleteResponse = getAdminClient()
                .prepareDelete(indexName.toLowerCase())
                .execute()
                .actionGet();
        return deleteResponse.isAcknowledged()?true:false;
    }


    /**
     * 测试
     */
    @Test
    public void testExists(){
        try {
            boolean exists = ESUtil.isExists("my-index");
            System.out.println(exists);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testDelete() throws Exception {
        boolean index = ESUtil.deleteIndex("my-index");
        System.out.println(index);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值