1. 程式人生 > >ElasticSearch(四)kibana實現CURD

ElasticSearch(四)kibana實現CURD

host 但是 iba cluster pen etime html intel int

一. kibana安裝

1.到官網或是用brew下載kibana 安裝包,這邊我們選擇在官網下載對應的安裝包 https://www.elastic.co/cn/downloads/kibana

2.解壓縮到對應的目錄下,我們解壓縮到了 /usr/local/kibana-6.5.2-darwin-x86_64

解壓縮後的相關目錄如下:

?  kibana-6.5.2-darwin-x86_64 ls
LICENSE.txt  bin          node         package.json webpackShims
NOTICE.txt   config       node_modules plugins
README.txt   data         optimize     src

3.快速啟動

cd 到對應的bin目錄下,執行nohup sh kibana &

查看啟動日誌:

1 {"type":"response","@timestamp":"2018-12-19T11:20:30Z","tags":[],"pid":8563,"method":"post","statusCode":200,"req":{"url":"/api/console/proxy?path=_template&method=GET","method":"post","headers":{"host":"localhost:5601","connection":"keep-alive","content-length":"0","accept":"text/plain, */*; q=0.01","origin":"http://localhost:5601","kbn-version":"6.5.2","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36","referer":"http://localhost:5601/app/kibana
","accept-encoding":"gzip, deflate, br","accept-language":"zh-CN,zh;q=0.9,en;q=0.8"},"remoteAddress":"::1","userAgent":"::1","referer":"http://localhost:5601/app/kibana"},"res":{"statusCode":200,"responseTime":22,"contentLength":9},"message":"POST /api/console/proxy?path=_template&method=GET 200 22ms - 9.0B"}

可以看到綁定端口為5601

4.查看進程

執行ps -ef|grep node

 501  8563  2099   0  6:43下午 ttys000    0:27.66 ./../node/bin/node --no-warnings ./../src/cli

5.瀏覽器訪問 http://localhost:5601/app/kibana 進入Dev Tools界面

二.CURD

1.document數據格式

面向文檔的搜索分析引擎

(1)應用系統的數據結構都是面向對象的,復雜的
(2)對象數據存儲到數據庫中,只能拆解開來,變為扁平的多張表,每次查詢的時候還得還原回對象格式,相當麻煩
(3)ES是面向文檔的,文檔中存儲的數據結構,與面向對象的數據結構是一樣的,基於這種文檔數據結構,es可以提供復雜的索引,全文檢索,分析聚合等功能
(4)es的document用json數據格式來表達

2.簡單的集群管理

(1)快速檢查集群的健康狀況

es提供了一套api,叫做cat api,可以查看es中各種各樣的數據

GET /_cat/health?v

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1545272412 02:20:12  elasticsearch green           1         1      1   1    0    0        0             0                  -                100.0%

如何快速了解集群的健康狀況?green、yellow、red?

green:每個索引的primary shard和replica shard都是active狀態的

yellow:每個索引的primary shard都是active狀態的,但是部分replica shard不是active狀態,處於不可用的狀態

red:不是所有索引的primary shard都是active狀態的,部分索引有數據丟失了

(2)快速查看集群中有哪些索引

health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1 4OzsBQhJROuREyl8ZadWCQ   1   0          3            0     11.9kb         11.9kb

(3)簡單的索引操作

創建索引:PUT /test_index?pretty

health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1  4OzsBQhJROuREyl8ZadWCQ   1   0          3            0     11.9kb         11.9kb
yellow open   test_index QmrZBLhPQkG41DSt-rLzfQ   5   1          0            0      1.1kb          1.1kb

刪除索引:DELETE /test_index?pretty

health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1 4OzsBQhJROuREyl8ZadWCQ   1   0          3            0     11.9kb         11.9kb

3、document的CRUD操作

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1545275286 03:08:06  elasticsearch yellow          1         1      6   6    0    0        5             0                  -                 54.5%

(1)新增:新增文檔,建立索引

PUT /index/type/id
{
  "json數據"
}

PUT /ecommerce/product/1
{
    "name" : "gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

PUT /ecommerce/product/2
{
    "name" : "jiajieshi yagao",
    "desc" :  "youxiao fangzhu",
    "price" :  25,
    "producer" :      "jiajieshi producer",
    "tags": [ "fangzhu" ]
}

PUT /ecommerce/product/3
{
    "name" : "zhonghua yagao",
    "desc" :  "caoben zhiwu",
    "price" :  40,
    "producer" :      "zhonghua producer",
    "tags": [ "qingxin" ]
}

es會自動建立index和type,不需要提前創建,而且es默認會對document每個field都建立倒排索引,讓其可以被搜索

(2)查詢:檢索文檔

GET /index/type/id
GET /ecommerce/product/1

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

(3)修改:替換文檔

PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}


PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao"
}

替換方式有一個不好,即使必須帶上所有的field,才能去進行信息的修改

(4)修改:更新文檔

POST /ecommerce/product/1/_update
{
  "doc": {
    "name": "jiaqiangban gaolujie yagao"
  }
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 8,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

(5)刪除:刪除文檔

DELETE /ecommerce/product/1

{
  "found": true,
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 9,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "found": false
}

ElasticSearch(四)kibana實現CURD