【ES】JavaAPI学习-04

【ES】JavaAPI学习-04,第1张

前言

本节主要实现批量插入,批量删除。

实现

复制ESTest_Doc_Insert.java,取名为ESTest_Doc_Insert_Batch

则有ESTest_Doc_Insert_Batch.java

package com.zwy.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

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

        //
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 批量插入数据
        BulkRequest request = new BulkRequest();

        // 定义数据,添加入request
        request.add(new IndexRequest().index("users").id("1001").source(XContentType.JSON, "name", "张三"));
        request.add(new IndexRequest().index("users").id("1002").source(XContentType.JSON, "name", "李四"));
        request.add(new IndexRequest().index("users").id("1003").source(XContentType.JSON, "name", "王五"));

        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook());
        System.out.println(response.getItems());


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

    }
}


ESTest_Doc_Delete_Batch.java

package com.zwy.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

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

        //
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 批量删除数据
        BulkRequest request = new BulkRequest();

        // 定义删除,添加入request
        request.add(new DeleteRequest().index("users").id("1001"));
        request.add(new DeleteRequest().index("users").id("1002"));
        request.add(new DeleteRequest().index("users").id("1003"));

        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook());
        System.out.println(response.getItems());


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

    }
}

文件结构

文件结构如下:

运行

运行ESTest_Doc_Insert_Batch.java


查看POSTMAN:

这里写一个GET 请求,地址为

http://127.0.0.1:9200/users/_search


插入成功!

运行ESTest_Doc_Delete_Batch.java


在发送一下刚刚的get请求


删除成功!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/730772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存