ES_API操作

5 篇文章 0 订阅

ES_API读写操作

新建工程并导入依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.6</version>
</dependency>

<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.3</version>
</dependency>

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>4.5.2</version>
</dependency>

<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>commons-compiler</artifactId>
    <version>2.7.8</version>
</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.0</version>
</dependency>

1. 写数据

1)单条数据写入

		//1.创建ES客户端构建器
        JestClientFactory factory = new JestClientFactory();

        //2.创建ES客户端连接地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        //3.设置ES连接地址
        factory.setHttpClientConfig(httpClientConfig);

        //4.获取ES客户端连接
        JestClient jestClient = factory.getObject();

        //5.构建ES插入数据对象
        Index index = new Index.Builder("{\n" +
                "  \"id\":\"1002\",\n" +
                "  \"movie_name\":\”姜子牙\”\n" +
                "}").index("movie_test1").type("_doc").id("1002").build();

        //6.执行插入数据操作
        jestClient.execute(index);

        //7.关闭连接
        jestClient.shutdownClient();

使用bean对象的方式插入数据

 		//5.构建ES插入数据对象
 		MovieInfo movieInfo = new MovieInfo(1004, "《阿甘正传》");

        //Index代表对一行记录的插入操作 PUT /index/type/id {}
        Index index = new Index.Builder(movieInfo)
                .index("movie_test1")
                .type("_doc")
                .id("1003")
                .build();

       //6.执行插入数据操作
       jestClient.execute(index);

创建movie的bean对象

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class MovieInfo {
    private Integer id;
    private String name;
}

2. 批量数据写入

public class WriteBulk {

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

        //1.创建客户端对象
        JestClientFactory clientFactory = new JestClientFactory();

        //2.设置连接参数
        HttpClientConfig clientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        clientFactory.setHttpClientConfig(clientConfig);

        //3.从JestClientFactory中获取一个ES的客户端对象
        JestClient client = clientFactory.getObject();

        //4.批量数据准备(可以在一个批次中向多个表插入数据)
        MovieInfo movieInfo = new MovieInfo(1007, "《心灵奇旅》");

        Employee employee = new Employee(1200, "Mike", "eat", '男', 3000.0, 30);

        //5.构建ES插入数据对象
        //Index代表对一行记录的插入操作 PUT /index/type/id {}
        Index index = new Index.Builder(movieInfo)
                .index("move_index1")
                .type("movie_type")
                .id("4")
                .build();
        Index index2 = new Index.Builder(employee)
                .index("test")
                .type("emps")
                .id("20")
                .build();
        Delete delete = new Delete.Builder("3")
                .index("move_index1")
                .type("movie_type")
                .build();

        Bulk bulk = new Bulk.Builder()
                .addAction(index)
                .addAction(index2)
                .addAction(delete)
                .build();
        //.defaultIndex().defaultType() 当前批量操作都是向一个表,在bulk中放入的每个 Action对象,{action:{metadata}}

        
        //6.调用客户端提供的API进行插入数据操作
        client.execute(bulk);

        //7.关闭连接
        client.shutdownClient();

    }

3. 读数据

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

        //1.创建ES客户端连接池
        JestClientFactory clientFactory = new JestClientFactory();

        //2.设置连接的集群地址参数
        HttpClientConfig clientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        //3.设置ES连接地址
        clientFactory.setHttpClientConfig(clientConfig);

        //4.从JestClientFactory中获取一个ES的客户端对象
        JestClient client = clientFactory.getObject();

        //5.构建查询数据对象
        String requestBody="{\n" +
                "  \"query\": {\n" +
                "    \"match\": {\n" +
                "      \"hobby\": \"购物\"\n" +
                "    }\n" +
                "  },\n" +
                "  \"aggs\": {\n" +
                "    \"gendercount\": {\n" +
                "      \"terms\": {\n" +
                "        \"field\": \"gender.keyword\",\n" +
                "        \"size\": 10\n" +
                "      },\n" +
                "      \"aggs\": {\n" +
                "        \"avgage\": {\n" +
                "          \"avg\": {\n" +
                "            \"field\": \"age\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
        //GET /index/type
        Search search = new Search.Builder(requestBody)
                .addIndex("test")
                .addType("emps")
                .build();

        
        //6.执行查询操作
        SearchResult result = client.execute(search);

        //7.解析查询结果
        //取出total和maxScore
        System.out.println(result.getTotal());
        System.out.println(result.getMaxScore());

        //取出命中的记录
        List<SearchResult.Hit<Employee, Void>> hits = result.getHits(Employee.class);

        for (SearchResult.Hit<Employee, Void> hit : hits) {
            //获取 _source
            System.out.println(hit.source);
        }

        //获取聚合的结果
        MetricAggregation aggregations = result.getAggregations();

        //根据聚合的字段名称取出结果
        TermsAggregation gendercount = aggregations.getTermsAggregation("gendercount");

        //聚合的数据存放在bulks
        List<TermsAggregation.Entry> buckets = gendercount.getBuckets();

        for (TermsAggregation.Entry bucket : buckets) {
            //dou_count: getCount()
            System.out.println(bucket.getKey() +"  "+bucket.getCount()
                +"  "+bucket.getAvgAggregation("avgage").getAvg());
        }


        //8.关闭连接
        client.shutdownClient();

    }

简化:使用api构造查询的请求体

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

        //1.创建ES客户端连接池
    
        //2.设置连接的集群地址参数
      
        //3.设置ES连接地址

        //4.从JestClientFactory中获取一个ES的客户端对象
       
         //5.构建查询数据对象
         /*
                    使用api构造查询的请求体
                    SearchSourceBuilder:  可以构造一个Search中的_source对象,构造json串
                    QueryBuilder: 封装了Query{} 中的所有的信息,需要根据查询的类型来构造不同的QueryBuilder
                    AggregationBuilder: 封装了 aggs{} 中的所有的信息,通过AggregationBuilders根据聚合的类型来构造
         */
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("gendercount")
                .field("gender.keyword")
                .size(10)
                .subAggregation(AggregationBuilders.avg("avgage").field("age"));

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("hobby", "购物");

        SearchSourceBuilder searchBuilder = new SearchSourceBuilder()
                .query(matchQueryBuilder)
                .aggregation(aggregationBuilder);

        //GET /index/type
        Search search = new Search.Builder(searchBuilder.toString())
                .addIndex("test")
                .addType("emps")
                .build();

        
        
		//6.执行查询操作
        SearchResult result = client.execute(search);

        //7.解析查询结果
       
        //8.关闭连接
        client.shutdownClient();

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值