Kibana 加载样本数据


我们已经了解了如何将数据从 logstash 上传到 elasticsearch。我们将在这里使用 logstash 和 elasticsearch 上传数据。但是关于我们需要使用的具有日期、经度和纬度字段的数据,我们将在接下来的章节中学习。如果我们没有 CSV 文件,我们还将了解如何在 Kibana 中直接上传数据。

在本章中,我们将涵盖以下主题:

  • 在 Elasticsearch 中使用 Logstash 上传具有日期、经度和纬度字段的数据
  • 使用开发工具上传批量数据

使用 Logstash 上传在 Elasticsearch 中具有字段的数据


我们将使用 CSV 格式的数据,同样来自 Kaggle.com,它处理可用于分析的数据。

The data 家庭医疗访问 此处使用的是从网站 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如下:

Home visits

以下是logstash要使用的conf文件:

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
}

所以我们需要确保我们有弹性搜索需要的格式的经度和纬度。所以首先我们需要将经度和纬度转换为浮点数,然后重命名它,以便它可以作为 location json 对象与 lat and 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 conf 文件中提到的索引,如下所示:
logstash conf

我们现在可以在上面上传的索引上创建索引模式,并进一步使用它来创建 可视化。

使用开发工具上传批量数据


我们将使用 Kibana UI 中的开发工具。开发工具有助于在 Elasticsearch 中上传数据,而无需使用 Logstash。我们可以使用开发工具在 Kibana 中发布、放置、删除、搜索我们想要的数据。

在本节中,我们将尝试在 Kibana 本身中加载示例数据。我们可以使用它来练习示例数据并使用 Kibana 功能来更好地理解 Kibana。

让我们从以下 url 中获取 json 数据并将其上传到 Kibana。同样,你可以尝试在 Kibana 中加载任何示例 json 数据。

在我们开始上传示例数据之前,我们需要将带有索引的 json 数据用于 elasticsearch。当我们使用 logstash 上传它时,logstash 会注意添加索引,用户不必担心弹性搜索所需的索引。

正常的 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"}

请注意,jsonfile 中还有一个附加数据: {"index":{"_index":"nameofindex","_id":key}} .

要转换任何与 elasticsearch 兼容的示例 json 文件,我们在 php 中有一个小代码,它将输出给定为 elasticsearch 所需格式的 json 文件:

PHP Code

<?php
    $myfile = fopen("todo.json", "r") or die("Unable to open file!"); // 你的 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 并使用php代码转换成我们需要在Kibana中上传的格式。

要加载示例数据,请打开开发工具选项卡,如下所示:

Dev tools

我们现在将使用如上所示的控制台。我们将获取通过 php 代码运行后得到的 json 数据。

开发工具上传json数据的命令为:

POST _bulk

请注意,我们正在创建的索引的名称是 todo .

Upload json data

Dev tools index

点击绿色按钮数据上传后,可以在elasticsearch中查看是否创建了索引,如下:

Created index

你可以在开发工具本身中检查相同的内容,如下所示:

Command:

GET /_cat/indices
Dev Tools Command

如果你想在你的 index:todo 中搜索一些东西,你可以这样做,如下所示:

开发工具中的命令

GET /todo/_search
Dev Tools Search

上述搜索的输出如下图:

Dev Tools 输出

它给出了 todoindex 中存在的所有记录。我们得到的总记录是 200。

在 todo 索引中搜索记录


我们可以使用以下命令来做到这一点:

GET /todo/_search
{
    "query":{
        "match":{
            "title":"delectusautautem"
        }
    }
}
Record todo Index

Fetch records

我们能够获取与我们给出的标题匹配的记录。