package com.hope.es; 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 org.junit.Test; import java.net.InetAddress; /** * 创建索引库 * @author newcityman * @date 2020/1/16 - 18:24 */public class ElasticSearchClient { @Test public void createIndex() throws Exception{ //1、创建一个setting对象,相当于一个配置信息,主要配置集群的名称 Settings settings = Settings.builder() .put("cluster.name", "my‐elasticsearch").build(); //2、创建一个客户端client对象 TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301)); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302)); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9303)); //3、使用client对象,创建一个索引库 client.admin().indices().prepareCreate("index_hello").get(); //4、关闭client对象 client.close(); }}
原文地址:https://www.cnblogs.com/newcityboy/p/12203229.html
时间: 2024-10-20 11:56:01