系列文章目录
文章目录
前言
本文我们要讲述:
Elasticsearch;
Kibana
在下攸攸太上,我每次写代码必定是班里最快的。
一、Elasticsearch、Kibana介绍
Elasticsearch和Kibana都是由Elastic公司提供的开源工具,用于处理和可视化数据。
1. Elasticsearch
Elasticsearch是一个基于Lucene的分布式搜索和分析引擎。它被设计用于快速、可扩展地搜索、分析和存储大量数据。
2. Elasticsearch特点
分布式架构:可以水平扩展以处理大规模数据,并提供高可用性和容错性。
实时搜索:支持对数据的实时索引和搜索,适用于实时应用程序和分析场景。
多种数据类型支持:可以处理结构化、半结构化和非结构化数据,包括文本、地理空间数据等。
强大的查询语言:支持复杂的查询和聚合操作,提供灵活的搜索和分析功能。
3. Kibana
Kibana是一个用于数据可视化和管理的开源工具,通常与Elasticsearch配合使用。
4. Kibana特点
数据可视化:提供丰富的图表、地图和仪表板构建功能,用于展示和分析Elasticsearch中的数据。
实时监控:可以实时监控数据、查询性能和系统指标。
用户友好的界面:提供直观的用户界面,使用户可以轻松地创建和定制数据可视化和仪表板。
集成Elastic Stack:与Elasticsearch、Logstash和Beats等工具无缝集成,构建完整的数据处理和可视化解决方案。
总的来说,Elasticsearch和Kibana通常结合使用,Elasticsearch用于存储和分析数据,而Kibana用于可视化和管理这些数据,为用户提供了强大的数据处理和可视化能力。
二、Elasticsearch、Kibana安装
1. 下载镜像
docker pull elasticsearch:7.6.2
docker pull kibana:7.6.2
漫长的等待后:
2. 创建路径
创建/usr/local/docker/es路径和其下config和data两个文件夹
3. 分配权限
cd /usr/local/docker/es/
ll
chmod -R 777 /usr/local/docker/es/
chmod -R 777 /usr/local/docker/es/config
chmod -R 777 /usr/local/docker/es/data
4. 添加访问权限
echo "http.host: 0.0.0.0" > /usr/local/docker/es/config/elasticsearch.yml
我们打开elasticsearch.yml进行查看
写入成功
5. 启动Elasticsearch
docker run --name es -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /usr/local/docker/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /usr/local/docker/es/data:/usr/share/elasticsearch/data \
-v /usr/local/docker/es/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.6.2
6. 访问Elasticsearch
http://192.168.43.143:9200/
访问网址查看版本信息
{
"name" : "d33a8f9523dc",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Gzf3tkYRRyuHlkSiBzkQyA",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
7. 启动Kibana
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://192.168.43.143:9200 -p 5601:5601 -d kibana:7.6.2
三、Elasticsearch、Kibana使用
http://192.168.43.143:5601
可能出现Kibana Is Not Ready Yet字样,无需慌乱,闲暇片刻,喝喝水,溜溜神,回神后再次刷新,即可出现如下界面
点击上图Try our sample data按钮
点击左下角【≡+】(哈哈,赛博拟声词!)
点击Dev Tools,在弹出的页面里点击Dismiss
将如下代码写入左侧文本框
# 增 /student/_doc/1 student是表名,_doc是文档类型的记录,1是主键的值;
POST /student/_doc/1
{
"id":"1",
"name":"张三",
"age":21,
"scoreList":[
{"id":"1","name":"数学", "score":95},
{"id":"2","name":"语文", "score":98},
{"id":"3","name":"外语", "score":90}
]
}
POST /student/_doc/2
{
"id":"2",
"name":"李四",
"age":22,
"scoreList":[
{"id":"4","name":"数学", "score":85},
{"id":"5","name":"语文", "score":88},
{"id":"6","name":"外语", "score":80}
]
}
POST /student/_doc/3
{
"id":"3",
"name":"王五",
"age":23,
"scoreList":[
{"id":"7","name":"数学", "score":75},
{"id":"8","name":"语文", "score":78},
{"id":"9","name":"外语", "score":70}
]
}
# 查
GET /student/_doc/3
# 查所有
GET /student/_search
# 改
PUT /student/_doc/3
{
"id":"3",
"name":"王六",
"age":26,
"scoreList":[
{"id":"7","name":"数学", "score":65},
{"id":"8","name":"语文", "score":78},
{"id":"9","name":"外语", "score":70}
]
}
# 删
DELETE /student/_doc/3
将鼠标放在第二行会出现两个按钮
点击运行(click to send request)按钮
四、Elasticsearch实现
1. 创建Maven项目
项目结构:
2. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<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.6.13</spring-boot.version>
<jwt.version>0.7.0</jwt.version>
<fastjson.version>1.2.60</fastjson.version>
<spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-web-servlet</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
<!--2.资源拷贝的插件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.html</include>
<include>**/*.js</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
3. 创建EsConfig配置类
package com.jjy.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EsConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("192.168.1.101", 9200, "http"));
return new RestHighLevelClient(restClientBuilder);
}
}
4. 创建Score实体类
package com.jjy.entry;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
public class Score {
private String id;
private String name;
private Double score;
}
5. 创建Student实体类
package com.jjy.entry;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.stereotype.Component;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
public class Student {
private String name;
private Integer age;
private String id;
private List<Score> scoreList;
}
6. 创建EsApplication启动类
package com.jjy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EsApplication {
public static void main(String[] args) {
SpringApplication.run(EsApplication.class, args);
}
}
7. 创建EsTest测试类
package com.jjy.test;
import com.alibaba.fastjson.JSON;
import com.jjy.EsApplication;
import com.jjy.entry.Score;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import com.jjy.entry.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class)
public class EsTest {
@Autowired
private RestHighLevelClient client;
/**
* 保存数据
*/
@Test
public void save() throws IOException {
/*student相当于表名,将数据保存到哪里*/
IndexRequest indexRequest = new IndexRequest("student");
Student student = new Student();
student.setAge(20);
student.setId("5");
student.setName("钱六");
Score score1 = new Score();
score1.setName("数学");
score1.setScore(89.0);
score1.setId("shuxue");
Score score2 = new Score();
score2.setName("语文");
score2.setScore(121.0);
score2.setId("yuwen");
Score score3 = new Score();
score3.setName("外语");
score3.setScore(124.0);
score3.setId("waiyu");
List<Score> scoreList = new ArrayList<>();
scoreList.add(score1);
scoreList.add(score2);
scoreList.add(score3);
student.setScoreList(scoreList);
/*保存数据也是向es发送了rest请求,保存需要一个json的数据,将对象转化成JSON串*/
String studentJson = JSON.toJSONString(student);
indexRequest.source(studentJson, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
/**
* 查询数据
*/
@Test
public void select() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/*查询的字段要加上keyword,表示按照数据中这个字段值查询,不加就查询不到;id主键除外!*/
TermQueryBuilder termQuery = QueryBuilders.termQuery("name.keyword", "张三");
searchSourceBuilder.query(termQuery);
searchRequest.source(searchSourceBuilder);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(search);
}
}
8. 创建application.properties配置文件
spring.application.name=elasticsearch
9. 创建application.yml配置文件
server:
port: 100
10. 创建bootstrap.yml配置文件
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.43.143:8848
config:
server-addr: 192.168.43.143:8848 #
file-extension: yaml
namespace: 7e56a725-f51a-4603-b421-49add0ae9c4b # 命名空间ID,粘贴复制过来即可。
shared-configs:
- data-id: redissession1.yaml # 配置详情内:Data ID,粘贴复制过来即可。
group: DEFAULT_GROUP # 配置详情内:Group 粘贴复制过来即可。
inetutils:
preferred-networks: 192.168.56 # 服务管理内,public内点击详情,可以查看,需要项目启动后能查看,一般默认192.168.56
11. 启动虚拟机环境
需要启动nacos和redis
12. 测试运行
总结
本文讲述了:
Elasticsearch:用于存储分析数据;
Kibana:用于可视化使用Elasticsearch
在下攸攸太上,你们可能忘了,但我依然是ൠ♄§星的外星人。