Elasticsearch+Kibana分布式存储引擎

1.ElaticSearch****介绍

ElaticSearch ,简称为 ES , ES 是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检
索数据;本身扩展性很好,可以扩展到上百台服务器,处理 PB 级别的数据。 ES 也使用 Java 开发并使用
Lucene 作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 Restful Api 来隐藏
Lucene 的复杂性,从而让全文搜索变得简单。
Kibana 也是一个开源和免费的工具, Kibana 可以为 Logstash 和 ElasticSearch 提供的日志分析友好的
Web 界面,可以帮助汇总、分析和搜索重要数据日志(也就是将搜索出的数据进行可视化)。

2.下载elasticsearch

https://www.elastic.co/guide/en/elasticsearch/reference/7.6/zip-windows.html
安装 Elasticsearch
把下载下来的压缩包解压到系统盘,建议解压到 D 盘,如图

进入bin文件里,双击elasticsearch.bat启动

浏览器输入http://localhost:9200/,显示下图则表示安装成功


安装 kibana
为了方便使用 elastic search 的 Query DSL(Domain Specified Language ,领域专用语言 ) ,需要安装一
下 Elasticsearch 的 Kibana 可视化控制台管理工具。
下载

解压到D盘,打开文件夹

在config文件夹下的kibana.yml文件,找到以下内容并取消注释

server.port: 5601
server.host: "localhost"

这个可以随意取名

server.name: "ABC"

elasticsearch 服务器的地址

elasticsearch.hosts: ["http://localhost:9200"]

页面使用中文

i18n.locale: "zh-CN"

打开文件夹下的bin目录,双击kibaba.bat启动kibaba

启动完成后,在浏览器地址栏输入http://localhost:5601/app/kibana#/dev_tools/console

3.Springboot整合Elasticsearch

修改 pom.xml

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>
<groupId>com.hz</groupId>
<artifactId>Elasticsearch01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Elasticsearch01</name>
<description>Elasticsearch01</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<spring-boot.version>2.7.6</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--运行旧版 JUnit 测试 -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
<scope>runtime</scope>
</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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.hz.Elasticsearch01Application</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

实体类 Song.java

java 复制代码
package com.hz.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "songs")
public class Song {
@Id
@Field(type= FieldType.Keyword)
private String id;
/**
* 歌曲名
*/
@Field(type= FieldType.Text, analyzer = "ik_max_word")
private String name;
/**
* 描述信息
*/
@Field(type= FieldType.Text, analyzer = "ik_max_word")
private String note;
/**
* 歌手
*/
@Field(type= FieldType.Text, analyzer = "ik_max_word")
private String singer;
}

SongMapper 文件

java 复制代码
package com.hz.mapper;
import com.hz.entity.Song;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface SongMapper {
List<Song> selectAll();
}

创建 Elasticsearch 的查询接口

java 复制代码
package com.hz.repository;
import com.hz.entity.Song;
import
org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SongRepository extends ElasticsearchRepository<Song, String> {
List<Song> findByName(String name);
}

4.使用DSL

DSL 就是 Elasticsearch 特有的查询语言
无条件查询,默认返回 10 条数据
GET /songs/_search

{
"query": {
"match_all": {}
}
}
hits 里是查询结果信息, hits.total.value 表示符合查询条件的总记录数, hits.hits 表示的是返回的数据,
_source 里是具体的数据。
返回结果
{
"took" : 1 ,
"timed_out" : false ,
"_shards" : {
"total" : 4 ,
"successful" : 4 ,
"skipped" : 0 ,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 894 ,
"relation" : "eq"
},
"max_score" : 1.0 ,
"hits" : [
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "space:default" ,
"_score" : 1.0 ,
"_source" : {
"space" : {
"name" : " 默认值 " ,
"description" : " 这是您的默认工作区! " ,
"color" : "#00bfb3" ,
"disabledFeatures" : [ ],
"_reserved" : true
},
"type" : "space" ,
"references" : [ ],
"migrationVersion" : {
"space" : "6.6.0"
},
"updated_at" : "2024-11-24T08:40:53.388Z"
}
},
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "config:7.6.2" ,
"_score" : 1.0 ,
"_source" : {
"config" : {
"buildNum" : 29199
},
"type" : "config" ,
"references" : [ ],
"updated_at" : "2024-11-24T08:41:03.810Z"
}
},
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "telemetry:telemetry" ,
"_score" : 1.0 ,
"_source" : {
"telemetry" : {
"userHasSeenNotice" : true
},
"type" : "telemetry" ,
"references" : [ ],
"updated_at" : "2024-11-24T08:41:19.232Z"
}
},
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "maps-telemetry:maps-telemetry" ,
"_score" : 1.0 ,
"_source" : {
"maps-telemetry" : {
"settings" : {
"showMapVisualizationTypes" : false
},
"indexPatternsWithGeoFieldCount" : 0 ,
"mapsTotalCount" : 0 ,
"timeCaptured" : "2024-11-24T08:42:09.877Z" ,
"attributesPerMap" : {
"dataSourcesCount" : {
"min" : 0 ,
"max" : 0 ,
"avg" : 0
},
"layersCount" : {
"min" : 0 ,
"max" : 0 ,
"avg" : 0
},
"layerTypesCount" : { },
"emsVectorLayersCount" : { }
}
},
"type" : "maps-telemetry" ,
"references" : [ ],
"updated_at" : "2024-11-24T08:42:09.877Z"
}
},
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "ui-metric:kibana-user_agent:Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Edg/131.0.0.0" ,
"_score" : 1.0 ,
"_source" : {
"ui-metric" : {
"count" : 1
},
"type" : "ui-metric" ,
"references" : [ ],
"updated_at" : "2024-11-24T08:42:38.740Z"
}
},
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "ui-metric:console:opened_app" ,
"_score" : 1.0 ,
"_source" : {
"ui-metric" : {
"count" : 2
},
"type" : "ui-metric" ,
"updated_at" : "2024-11-24T09:31:25.704Z"
}
},
{
"_index" : ".kibana_1" ,
"_type" : "_doc" ,
"_id" : "ui-metric:console:GET_search" ,
"_score" : 1.0 ,
"_source" : {
"ui-metric" : {
"count" : 2
},
"type" : "ui-metric" ,
"updated_at" : "2024-11-24T09:32:56.668Z"
}
},
{
"_index" : ".kibana_task_manager_1" ,
"_type" : "_doc" ,
"_id" : "task:Lens-lens_telemetry" ,
"_score" : 1.0 ,
"_source" : {
"migrationVersion" : {
"task" : "7.6.0"
},
"task" : {
"taskType" : "lens_telemetry" ,
"retryAt" : null ,
"runAt" : "2024-11-24T16:00:00.000Z" ,
"startedAt" : null ,
"state" : """{" runs ":1," byDate ":{}," suggestionsByDate ":{}," saved ":
{" saved_30_days ":
{}," saved_overall_total ":0," saved_30_days_total ":0," saved_90_days_total ":0}}""" ,
"params" : "{}" ,
"ownerId" : null ,
"scheduledAt" : "2024-11-24T08:40:55.891Z" ,
"attempts" : 0 ,
"status" : "idle"
},
"references" : [ ],
"updated_at" : "2024-11-24T08:40:59.309Z" ,
"type" : "task"
}
},
{
"_index" : ".kibana_task_manager_1" ,
"_type" : "_doc" ,
"_id" : "task:oss_telemetry-vis_telemetry" ,
"_score" : 1.0 ,
"_source" : {
"migrationVersion" : {
"task" : "7.6.0"
},
"task" : {
"taskType" : "vis_telemetry" ,
"retryAt" : null ,
"runAt" : "2024-11-24T16:00:00.000Z" ,
"startedAt" : null ,
"state" : """{" runs ":1}""" ,
"params" : "{}" ,
"ownerId" : null ,
"scheduledAt" : "2024-11-24T08:40:55.890Z" ,
"attempts" : 1 ,
"status" : "idle"
},
"references" : [ ],
"updated_at" : "2024-11-24T08:40:59.165Z" ,
"type" : "task"
}
},
{
"_index" : "songs" ,
"_type" : "_doc" ,
"_id" : "20210522154331" ,
"_score" : 1.0 ,
"_source" : {
"_class" : "com.hz.entity.Song" ,
"id" : "20210522154331" ,
"name" : " 爱一点 " ,
"singer" : " 王力宏、章子怡 "
}
}
]
}
}
指定返回的数据条数
通过 size 指定需要返回的结果数,以下查询语句将会返回 20 条数据,而非默认的 10 条
GET /songs/_search
{
"query": {
"match_all": {}
},
"size": 20
}
指定查询字段
_source 是一个数组,指定需要返回哪些字段,设置为 false 则不会返回数据。
GET /songs/_search
{
"query": {
"match_all": {}
},
"size": 5,
"_source": ["name", "singer", "note"]
}
分页查询
通过 from+size 实现分页查询,下面查询了第 6-10 条记录,相当于 mysql 中的 limit 5, 5 (和 mysql 类似,
from 默认为 0 )
GET /songs/_search
{
"query": {
"match_all": {}
},
"from": 5,
"size": 5
}

删除索引

DELETE /songs

相关推荐
超级阿飞1 小时前
利用Kubespray安装生产环境的k8s集群-实施篇
elasticsearch·容器·kubernetes
大叔_爱编程4 小时前
wx030基于springboot+vue+uniapp的养老院系统小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计
计算机学姐6 小时前
基于微信小程序的驾校预约小程序
java·vue.js·spring boot·后端·spring·微信小程序·小程序
黄名富6 小时前
Kafka 日志存储 — 日志索引
java·分布式·微服务·kafka
Ase5gqe6 小时前
大数据-259 离线数仓 - Griffin架构 修改配置 pom.xml sparkProperties 编译启动
xml·大数据·架构
史嘉庆6 小时前
Pandas 数据分析(二)【股票数据】
大数据·数据分析·pandas
DM很小众6 小时前
Kafka 和 MQ 的区别
分布式·kafka
小白的一叶扁舟7 小时前
深入剖析 JVM 内存模型
java·jvm·spring boot·架构
sjsjsbbsbsn7 小时前
基于注解实现去重表消息防止重复消费
java·spring boot·分布式·spring cloud·java-rocketmq·java-rabbitmq
苹果醋37 小时前
golang 编程规范 - Effective Go 中文
java·运维·spring boot·mysql·nginx