您当前的位置: 首页 >  Java

java持续实践

暂无认证

  • 3浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

es 7.x JavaAPI 索引创建

java持续实践 发布时间:2022-01-15 11:35:10 ,浏览量:3

文章目录
      • java api 创建索引

java api 创建索引
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

/**
 * 类名称:ESTest_Client
 * 类描述: 创建索引
 *
 * @author:  
 * 创建时间:2022/1/11 21:05
 */
public class ESTest_Index_Create {

    public static void main(String[] args) throws Exception {

        // 创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
        CreateIndexRequest request = new CreateIndexRequest("sysuser");
        CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);

        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println("创建索引操作结果:" + acknowledged);

        // 关闭ES客户端
        esClient.close();
    }
}

运行程序后, 有报错提示 : No log4j2 configuration file found resources目录下, 增加配置



    
        
            
        
        
            
                %d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n
            
            
                
                
            
            
        
    
    
        
            
        
    

查询所有的索引: get请求 http://127.0.0.1:9200/_cat/indices?v 可以看到创建成功. 如果再次运行上面的程序, 可以看到提示报错: 索引已经存在.

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.1988s