- Kibana 教程
- Kibana - 首页
- Kibana - 概览
- Kibana - 环境设置
- Kibana - ELK Stack 简介
- Kibana - 加载示例数据
- Kibana - 管理
- Kibana - 发现
- Kibana - 聚合和指标
- Kibana - 创建可视化
- Kibana - 使用图表
- Kibana - 使用图形
- Kibana - 使用热力图
- 使用坐标地图
- Kibana - 使用区域地图
- 使用仪表和目标
- Kibana - 使用画布
- Kibana - 创建仪表板
- Kibana - Timelion
- Kibana - 开发工具
- Kibana - 监控
- 使用 Kibana 创建报表
- Kibana 有用资源
- Kibana - 快速指南
- Kibana - 有用资源
- Kibana - 讨论
Kibana - 加载示例数据
我们已经了解了如何将数据从 Logstash 上传到 Elasticsearch。我们将在本文中使用 Logstash 和 Elasticsearch 上传数据。但是,关于我们需要使用的具有日期、经度和纬度字段的数据,我们将在接下来的章节中学习。如果我们没有 CSV 文件,我们还将了解如何在 Kibana 中直接上传数据。
在本章中,我们将涵盖以下主题:
- 使用 Logstash 上传具有日期、经度和纬度字段的数据到 Elasticsearch
- 使用开发工具上传批量数据
使用 Logstash 上传具有字段的数据到 Elasticsearch
我们将使用 CSV 格式的数据,这些数据来自 Kaggle.com,该网站提供可用于分析的数据。
此处使用的数据是家庭医疗访问数据,取自 Kaggle.com。
以下是在 CSV 文件中可用的字段:
["Visit_Status","Time_Delay","City","City_id","Patient_Age","Zipcode","Latitude","Longitude", "Pathology","Visiting_Date","Id_type","Id_personal","Number_Home_Visits","Is_Patient_Minor","Geo_point"]
Home_visits.csv 如下所示:
以下是将与 Logstash 一起使用的配置文件:
input {
file {
path => "C:/kibanaproject/home_visits.csv"
start_position => "beginning"
sincedb_path => "NUL"
}
}
filter {
csv {
separator => ","
columns =>
["Visit_Status","Time_Delay","City","City_id","Patient_Age",
"Zipcode","Latitude","Longitude","Pathology","Visiting_Date",
"Id_type","Id_personal","Number_Home_Visits","Is_Patient_Minor","Geo_point"]
}
date {
match => ["Visiting_Date","dd-MM-YYYY HH:mm"]
target => "Visiting_Date"
}
mutate {convert => ["Number_Home_Visits", "integer"]}
mutate {convert => ["City_id", "integer"]}
mutate {convert => ["Id_personal", "integer"]}
mutate {convert => ["Id_type", "integer"]}
mutate {convert => ["Zipcode", "integer"]}
mutate {convert => ["Patient_Age", "integer"]}
mutate {
convert => { "Longitude" => "float" }
convert => { "Latitude" => "float" }
}
mutate {
rename => {
"Longitude" => "[location][lon]"
"Latitude" => "[location][lat]"
}
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "medicalvisits-%{+dd.MM.YYYY}"
}
stdout {codec => json_lines }
}
默认情况下,Logstash 将上传到 Elasticsearch 的所有内容都视为字符串。如果您的 CSV 文件具有日期字段,则需要执行以下操作以获取日期格式。
对于日期字段:
date {
match => ["Visiting_Date","dd-MM-YYYY HH:mm"]
target => "Visiting_Date"
}
对于地理位置,Elasticsearch 理解如下:
"location": {
"lat":41.565505000000044,
"lon": 2.2349995750000695
}
因此,我们需要确保经度和纬度采用 Elasticsearch 所需的格式。所以首先我们需要将经度和纬度转换为浮点数,然后重命名它,以便它作为location JSON 对象的一部分可用,其中包含lat 和lon。此处显示了相应的代码:
mutate {
convert => { "Longitude" => "float" }
convert => { "Latitude" => "float" }
}
mutate {
rename => {
"Longitude" => "[location][lon]"
"Latitude" => "[location][lat]"
}
}
要将字段转换为整数,请使用以下代码:
mutate {convert => ["Number_Home_Visits", "integer"]}
mutate {convert => ["City_id", "integer"]}
mutate {convert => ["Id_personal", "integer"]}
mutate {convert => ["Id_type", "integer"]}
mutate {convert => ["Zipcode", "integer"]}
mutate {convert => ["Patient_Age", "integer"]}
一旦字段得到处理,运行以下命令将数据上传到 Elasticsearch:
- 进入 Logstash 的 bin 目录并运行以下命令。
logstash -f logstash_homevisists.conf
- 完成后,您应该在 Elasticsearch 中看到 Logstash 配置文件中提到的索引,如下所示:
我们现在可以在上面上传的索引上创建索引模式,并进一步用于创建可视化。
使用开发工具上传批量数据
我们将使用 Kibana UI 中的开发工具。开发工具有助于在 Elasticsearch 中上传数据,而无需使用 Logstash。我们可以使用开发工具在 Kibana 中发布、放置、删除和搜索我们想要的数据。
在本节中,我们将尝试在 Kibana 本身加载示例数据。我们可以使用它来练习示例数据,并使用 Kibana 功能进行操作,以深入了解 Kibana。
让我们从以下 URL 获取 JSON 数据,并在 Kibana 中上传。类似地,您可以尝试将任何示例 JSON 数据加载到 Kibana 中。
在开始上传示例数据之前,我们需要在 Elasticsearch 中使用索引的 JSON 数据。当我们使用 Logstash 上传时,Logstash 会负责添加索引,用户不必担心 Elasticsearch 需要哪些索引。
普通 JSON 数据
[
{"type":"act","line_id":1,"play_name":"Henry IV",
"speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"},
{"type":"scene","line_id":2,"play_name":"Henry IV",
"speech_number":"","line_number":"","speaker":"","text_entry":"SCENE I.London. The palace."},
{"type":"line","line_id":3,"play_name":"Henry IV",
"speech_number":"","line_number":"","speaker":"","text_entry":
"Enter KING HENRY, LORD JOHN OF LANCASTER, the
EARL of WESTMORELAND, SIR WALTER BLUNT, and others"}
]
与 Kibana 一起使用的 JSON 代码必须具有以下索引:
{"index":{"_index":"shakespeare","_id":0}}
{"type":"act","line_id":1,"play_name":"Henry IV",
"speech_number":"","line_number":"","speaker":"","text_entry":"ACT I"}
{"index":{"_index":"shakespeare","_id":1}}
{"type":"scene","line_id":2,"play_name":"Henry IV",
"speech_number":"","line_number":"","speaker":"",
"text_entry":"SCENE I. London. The palace."}
{"index":{"_index":"shakespeare","_id":2}}
{"type":"line","line_id":3,"play_name":"Henry IV",
"speech_number":"","line_number":"","speaker":"","text_entry":
"Enter KING HENRY, LORD JOHN OF LANCASTER, the EARL
of WESTMORELAND, SIR WALTER BLUNT, and others"}
请注意,JSON 文件中还有其他数据:{"index":{"_index":"nameofindex","_id":key}}。
要将任何示例 JSON 文件转换为与 Elasticsearch 兼容的格式,这里我们提供了一个小的 PHP 代码,它会将给定的 JSON 文件输出为 Elasticsearch 所需的格式:
PHP 代码
<?php
$myfile = fopen("todo.json", "r") or die("Unable to open file!"); // your json
file here
$alldata = fread($myfile,filesize("todo.json"));
fclose($myfile);
$farray = json_decode($alldata);
$afinalarray = [];
$index_name = "todo";
$i=0;
$myfile1 = fopen("todonewfile.json", "w") or die("Unable to open file!"); //
writes a new file to be used in kibana dev tool
foreach ($farray as $a => $value) {
$_index = json_decode('{"index": {"_index": "'.$index_name.'", "_id": "'.$i.'"}}');
fwrite($myfile1, json_encode($_index));
fwrite($myfile1, "\n");
fwrite($myfile1, json_encode($value));
fwrite($myfile1, "\n");
$i++;
}
?>
我们从https://jsonplaceholder.typicode.com/todos获取了 todo JSON 文件,并使用 PHP 代码将其转换为我们需要在 Kibana 中上传的格式。
要加载示例数据,请打开开发工具选项卡,如下所示:
我们现在将使用上面显示的控制台。我们将获取运行 PHP 代码后获得的 JSON 数据。
在开发工具中用于上传 JSON 数据的命令为:
POST _bulk
请注意,我们正在创建的索引名称为todo。
单击绿色按钮上传数据后,您可以检查索引是否已在 Elasticsearch 中创建,如下所示:
您可以在开发工具本身中检查相同内容,如下所示:
命令:
GET /_cat/indices
如果要搜索索引:todo 中的内容,您可以按照以下步骤操作:
开发工具中的命令
GET /todo/_search
上述搜索的输出如下所示:
它提供 todo 索引中存在的所有记录。我们获得的总记录数为 200。
在 todo 索引中搜索记录
我们可以使用以下命令执行此操作:
GET /todo/_search
{
"query":{
"match":{
"title":"delectusautautem"
}
}
}
我们能够获取与我们提供的标题匹配的记录。