SpringBoot + Elasticsearch + Kibana (7.8.1)入门应用

一、下载安装

  1. ElasticSearch 下载地址

…/elasticsearch-7.8.1/bin 的 elasticsearch.bat
…/elasticsearch-7.8.1/config/elasticsearch.yml

//# 主要配置 
network.host: 127.0.0.1
http.port: 9200
//# 解决跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*"
  1. Kibana下载地址 一个客户端数据展示项目

kibana-7.8.1-windows-x86_64\bin\kibana.bat
kibana-7.8.1-windows-x86_64\config\kibana.yml

// # es地址
elasticsearch.hosts: ["http://127.0.0.1:9200/"]
// # 汉化
i18n.locale: "zh-CN"
  1. elasticsearch-head-master下载地址 使用这个客户端需要nodejs环境,需要提前下载。是另一个客户端数据展示项目。可以只单用kibana,也可以配合使用。

使用教程参考github readme

注意:kibana 和 ES 版本要完全一致;jkd8 以上可用

二、使用kibana操作Es的指令示例

put
get

三、Springboot + Es 集成项目搭建

  1. 新建一个空SpringBoot项目。
  2. pom.xml
<?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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yxpweb</groupId>
    <artifactId>elks</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>elks</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注意:引入elasticsearch-rest-high-level-client(7.8.1)的自带版本和我们下载的ES版本不一致,需要手动添加elasticsearch(7.8.1)和elasticsearch-rest-client(7.8.1),否则会有报错,无法使用。用自己引入7.8.1的将原来的6.8.6覆盖。
在这里插入图片描述

  1. 新建User.java 的 pojo 类
@Component
public class User {
    private String name;
    private int age;

    public User(){

    }

    public User(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

如果用Lombok可以不用getset 方法。但是不建议使用Lombok。

四、使用的@Test注解做初步测试。

  1. 创建Client注入到spring
@Configuration
@Component
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
    }
}

在这里插入图片描述

  1. 测试包下创建测试类 com.yxpweb.elks.ElksApplicationTests,注入客户端。
    在这里插入图片描述
@SpringBootTest
class ElksApplicationTests {

    @Autowired
    RestHighLevelClient restHighLevelClient;
}

以下代码添加在ElksApplicationTests 类中。

  1. 创建索引 yxpweb-elk
@Test
    void testCreateIndex() {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("yxpweb-elk");
        try {
            CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            System.out.println(createIndexResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  1. 判断索引是否存在
@Test
    void testExistIndex() throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest("yxpweb-elk");
        boolean createIndexResponse = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }
  1. 通过id删除索引中的数据
DELETE /yxpweb_user/_doc/1
@Test
    void testDeleteDocumentIndex() throws IOException{
        DeleteRequest deleteIndexRequest = new DeleteRequest("yxpweb-elk", "1");
        DeleteResponse deleteResponse = restHighLevelClient.delete(deleteIndexRequest, RequestOptions.DEFAULT);
       //DeleteResponse[index=yxpweb_user,type=_doc,id=1,version=11,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]
        System.out.println(deleteResponse);
    }

即使数据已经被删除,依然会返回结果。version号会被更新。

  1. 删除索引
@Test
    void testDeleteIndex() throws IOException{
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("yxpweb-elk");
        deleteIndexRequest.timeout(TimeValue.timeValueMinutes(2));
        deleteIndexRequest.timeout("2m");

        AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);

        System.out.println(acknowledgedResponse.isAcknowledged());

    }
  1. 添加文档
@Test
    void testAddDocument() throws IOException {
        User yxp = new User("yxp", 33);
        IndexRequest indexRequest = new IndexRequest("yxpweb_user");

        indexRequest.id("1");
        indexRequest.timeout(TimeValue.timeValueSeconds(1));
        indexRequest.timeout("1s");

        indexRequest.source(JSON.toJSONString(yxp), XContentType.JSON);

        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());


    }

更多操作 参考 API

Spring Boot是一个开源的Java框架,用于构建独立的、可扩展的、基于微服务的应用程序。它提供了一种快速、简单的方式来构建和部署应用程序,也简化了与各种数据库、消息队列和其他外部系统进行集成的过程。同时,Spring Boot提供了一套强大的开发工具和功能,方便开发人员进行开发、测试和部署。 Elasticsearch是一个基于Lucene的分布式搜索和分析引擎,它被广泛应用于构建实时搜索、数据分析和数据存储的解决方案。Elasticsearch具有高性能、可扩展、可靠和易于使用的特点,可以处理大规模的数据,并提供全文搜索、聚合分析和实时监控等功能。 Oracle是一种关系型数据库管理系统(RDBMS),它是全球领先的企业级数据库解决方案之一。Oracle提供了高度可靠和安全的数据管理功能,同时支持事务处理、并发性控制、数据恢复和备份等重要特性。它还提供了丰富的管理和开发工具,方便开发人员进行数据库的设计、开发和管理。 在使用Spring Boot开发应用程序时,可以通过集成Elasticsearch和Oracle来满足不同的需求。使用Elasticsearch,可以轻松地实现全文搜索、聚合分析和实时监控等功能。而Oracle数据库可以用于存储结构化数据,并提供事务处理、数据完整性和安全性等特性。通过使用这两个技术,可以构建出高效、可靠和安全的应用程序,满足不同场景下的各种需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值