IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Spring Boot整合ElasticSearch实现索引和文档的CRUD(与Kibana语法结合) -> 正文阅读

[大数据]Spring Boot整合ElasticSearch实现索引和文档的CRUD(与Kibana语法结合)

1.创建SpringBoot项目,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.liu</groupId>
    <artifactId>esapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>esapi</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.13.3</elasticsearch.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2.创建ES客户端配置类,注意使用@Bean时需要指定bean的名字,因为ES默认会自动初始化客户端对象,否则在使用@Autowired依赖注入时,会有多个同一类型的bean,导入注入失败。

@Configuration
public class ClientConfig {
    @Bean("client")
    public RestHighLevelClient getRestHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                //es服务的地址和端口,如果有多个es(es集群)可以链式编程,添加es地址
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
        );
        return client;
    }
}

3.创建实体类,在插入文档时需要

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private Integer age;
    private String address;
}

4.索引和文档的CRUD单元测试

@SpringBootTest
@Slf4j
class EsapiApplicationTests {



    @Autowired
    private RestHighLevelClient client;


    /**
     * 创建索引(索引库)
     *
     *
     * kibana返回结果
     *
     *
     * PUT /test
     *
     * {
     *   "acknowledged" : true,
     *   "shards_acknowledged" : true,
     *   "index" : "test"
     * }
     *
     */
    @Test
    public void createIndex() throws Exception{
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("index_one");
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        log.info(createIndexResponse.index());
        log.info(Boolean.toString(createIndexResponse.isAcknowledged()));
        log.info(Boolean.toString(createIndexResponse.isShardsAcknowledged()));
    }


    /**
     *
     * GET /test
     *
     *
     * {
     *   "test" : {
     *     "aliases" : { },
     *     "mappings" : { },
     *     "settings" : {
     *       "index" : {
     *         "routing" : {
     *           "allocation" : {
     *             "include" : {
     *               "_tier_preference" : "data_content"
     *             }
     *           }
     *         },
     *         "number_of_shards" : "1",
     *         "provided_name" : "test",
     *         "creation_date" : "1626834365541",
     *         "number_of_replicas" : "1",
     *         "uuid" : "CDJ24TLGRIe2aCVxblKuTw",
     *         "version" : {
     *           "created" : "7130399"
     *         }
     *       }
     *     }
     *   }
     * }
     *
     *
     * 查找索引库,判断这个库是否存在
     *
     */
    @Test
    public void getIndex() throws Exception{

        GetIndexRequest getIndexRequest = new GetIndexRequest("index_one");
        boolean flag = client.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
        if(flag){
            log.info("index_one索引库存在");
        }else{
            log.info("index_one索引库不存在");
        }

    }


    /**
     * 索引库删除
     *
     *
     * DELETE /test
     *
     * {
     *   "acknowledged" : true
     * }
     */
    @Test
    public void deleteIndex() throws Exception{
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("index_one");
        AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        //判断  true表示删除成功
        if(acknowledgedResponse.isAcknowledged()){
            log.info("删除成功");
        }else{
            log.info("删除失败");
        }
    }


    /**
     * 创建文档
     * PUT /index_one/_doc/1
     * {
     *   "name":"刘红飞",
     *   "age":20,
     *   "address":"山西省临汾市洪洞县"
     * }
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "1",
     *   "_version" : 1,
     *   "result" : "created",
     *   "_shards" : {
     *     "total" : 2,
     *     "successful" : 1,
     *     "failed" : 0
     *   },
     *   "_seq_no" : 0,
     *   "_primary_term" : 1
     * }
     *
     *
     */
    @Test
    public void createDocument() throws Exception{
       User user = new User("张文文",20,"山西省太原市");
       //需要操作哪个索引库
       IndexRequest indexRequest = new IndexRequest("index_one");
       indexRequest.id("2");
       //将对象转换为json字符串存储
       indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
       IndexResponse indexResponse = client.index(indexRequest,RequestOptions.DEFAULT);
       log.info("------对应kibana中的API请求返回结果");
       log.info(indexResponse.getIndex());
       log.info(indexResponse.getType());
       log.info(indexResponse.getId());
       log.info(String.valueOf(indexResponse.getVersion()));
       log.info(indexResponse.getResult().getLowercase());
       ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
       log.info(String.valueOf(shardInfo.getSuccessful()));
       indexResponse.status();
    }


    /**
     * 获取文档
     *
     * GET /index_one/_doc/1
     *
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "1",
     *   "_version" : 1,
     *   "_seq_no" : 0,
     *   "_primary_term" : 1,
     *   "found" : true,
     *   "_source" : {
     *     "name" : "刘红飞",
     *     "age" : 20,
     *     "address" : "山西省临汾市洪洞县"
     *   }
     * }
     */
    @Test
    public void getDocument() throws Exception{

        //查询所有时,进行遍历,的每一个都是map,将map放到list里,最后再通过list展示所有

        GetRequest getRequest = new GetRequest("index_one","1");
        if(client.exists(getRequest,RequestOptions.DEFAULT)){
            GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
            //返回的是单个文档的数据,key为字段名称,value为字段的值
            Map<String,Object> map = getResponse.getSource();
            log.info(String.valueOf(map.get("name")));
            log.info(String.valueOf(map.get("age")));
            log.info(String.valueOf(map.get("address")));
        }else{
             log.info("当前文档不存在");
        }
    }


    /**
     *
     *更新文档
     *
     * POST /index_one/_doc/1/_update
     * {
     *   "doc":{
     *     "age":30
     *   }
     * }
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "1",
     *   "_version" : 2,
     *   "result" : "updated",
     *   "_shards" : {
     *     "total" : 2,
     *     "successful" : 1,
     *     "failed" : 0
     *   },
     *   "_seq_no" : 2,
     *   "_primary_term" : 1
     * }
     *
     *
     */
    @Test
    public void updateDocument() throws Exception{
        UpdateRequest updateRequest = new UpdateRequest("index_one","1");
        User user = new User();
        user.setAge(10000);
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);
        log.info("更新结果:" + updateResponse.getResult().getLowercase());
    }


    /**
     * 删除文档
     *
     * DELETE /index_one/_doc/2
     *
     *
     * {
     *   "_index" : "index_one",
     *   "_type" : "_doc",
     *   "_id" : "2",
     *   "_version" : 4,
     *   "result" : "deleted",
     *   "_shards" : {
     *     "total" : 2,
     *     "successful" : 1,
     *     "failed" : 0
     *   },
     *   "_seq_no" : 6,
     *   "_primary_term" : 1
     * }
     *
     */
    @Test
    public void deleteDocument() throws Exception{
        DeleteRequest deleteRequest = new DeleteRequest("index_one","2");
        DeleteResponse deleteResponse = client.delete(deleteRequest,RequestOptions.DEFAULT);
        log.info("删除结果:" + deleteResponse.status().name());
        log.info("删除结果:" + deleteResponse.getResult().getLowercase());
    }
}

最后顺便给大家解释下索引和文档的含义:
1.索引:又叫索引库,相当于MySQL中的表
2.文档:相当于MySQL表中的数据
一个索引库中有多个文档,也就是相当于表中有多条数据,
文档中的每个字段可以称之为域(lucene中的叫法),域类型和域值

ElasticSearch的数据可视化客户端可以选择 ElasticSearch Head如下,非常清晰好用
在这里插入图片描述

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-07-22 14:16:32  更:2021-07-22 14:16:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/6 10:07:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码