码迷,mamicode.com
首页 > 编程语言 > 详细

Elasticsearch java api操作(二)(Java High Level Rest Client)

时间:2019-04-28 17:14:20      阅读:880      评论:0      收藏:0      [点我收藏+]

标签:har   特点   author   增加   turn   www.   创建索引   分词   src   

一、说明:

  一、Elasticsearch提供了两个JAVA REST Client版本:

  1、java low level rest client:

  低级别的rest客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有Elasticsearch版本。

  特点:maven引入

  使用介绍: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html

  2、java high rest client:

  高级别的REST客户端,基于低级别的REST客户端,增加了编组请求JSON串、解析响应JSON串等相关API,使用的版本需要保存和ES服务一致的版本,否则会有版本问题。

  从6.0.0开始加入的,目的是以java面向对象的方式进行请求、响应处理。

  每个API支持 同步、异步 两种方式,同步方法之间返回一个结果对象。异步的方法以async为后缀,通过listener参数来通知结果。高级java resy客户端依赖Elasticsearch core pproject

  兼容性说明:

  依赖jdk1.8和Elasticsearch core project

二、Java Low Level Rest Client的使用

1、JAVA High Level Rest Client maven集成

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.3.1</version>
</dependency>

版本:

Elasticsearch 6.3.1

pom文件:

<dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>0.9</version>
        </dependency>
    </dependencies>

给定集群的多个节点地址,将客户端负载均衡地向这个节点地址集发请求:

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * @Author: xiaolaotou
 * @Date: 2019/4/28
 */
public class ClientUtil {
    public static RestHighLevelClient getClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.100",9200,"http"),
                        new HttpHost("192.168.200.101",9200,"http"),
                        new HttpHost("192.168.200.102",9200,"http"))
        );
        return client;
    }
}

2、创建索引

    public static void createindex()throws Exception{
        RestHighLevelClient client = ClientUtil.getClient();
        //1、创建索引,设置索引名
        CreateIndexRequest request = new CreateIndexRequest("mess");
        //2、设置索引的settings
        request.settings(Settings.builder()
                .put("index.number_of_shards",3)//分片数
                .put("index.number_of_replicas",1)//副本数
                .put("analysis.analyzer.default.tokenizer","ik_smart"))//默认分词器
        ;
        //3、设置索引的mappings
        request.mapping("_doc",
                "  {\n" +
                        "    \"_doc\": {\n" +
                        "      \"properties\": {\n" +
                        "        \"message\": {\n" +
                        "          \"type\": \"text\"\n" +
                        "        }\n" +
                        "      }\n" +
                        "    }\n" +
                        "  }",
                XContentType.JSON);
        //4、设置索引的别名
        request.alias(new Alias("mmm"));
        //5、发送方式
//        //5、1采用同步方式发送请求
//        CreateIndexResponse response = client.indices().create(request);
//        //处理响应
//        boolean acknowledged = response.isAcknowledged();
//        boolean shardsAcknowledged = response.isShardsAcknowledged();
//        System.out.println("acknowledged = " + acknowledged);
//        System.out.println("shardsAcknowledged = " + shardsAcknowledged);
//        client.close();
        //5、2采用异步方法发送请求
        ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse createIndexResponse) {
                //处理响应
                boolean acknowledged = createIndexResponse.isAcknowledged();
                boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
                System.out.println("shardsAcknowledged = " + shardsAcknowledged);
                System.out.println("acknowledged = " + acknowledged);
            }
            @Override
            public void onFailure(Exception e) {
                System.out.println("创建索引异常 = " + e.getMessage());
            }
        };
        client.indices().createAsync(request,listener);
        client.close();
    }

技术图片

技术图片

3、index document

 

 

 

 

 

 

待写!!!!!!!!!!!!!!!!!!!!!!!!

Elasticsearch java api操作(二)(Java High Level Rest Client)

标签:har   特点   author   增加   turn   www.   创建索引   分词   src   

原文地址:https://www.cnblogs.com/yfb918/p/10784951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!