1. 程式人生 > >Elasticsearch集群上線經驗技術點Welcome to my ELK world!(2)

Elasticsearch集群上線經驗技術點Welcome to my ELK world!(2)

delete 裝包 hang mailto 創建 name 筆記 product 查詢

一、安裝win上的ELK(linux的和ubuntu的請參考我的另一篇文章http://blog.51cto.com/13120271/2308902)
1、安裝JDK,至少1.8.0_73以上版本,java -version
2、下載和解壓縮Elasticsearch安裝包,目錄結構
3、啟動Elasticsearch:bin\elasticsearch.bat,es本身特點之一就是開箱即用,如果是中小型應用,數據量少,操作不是很復雜,直接啟動就可以用了

4、檢查ES是否啟動成功:http://localhost:9200/?pretty
(資源包下載位置:https://pan.baidu.com/s/1tkygFTulBE-5Yypzt0CL8Q 密碼:ygql)

name: node名稱
cluster_name: 集群名稱(默認的集群名稱就是elasticsearch)
version.number: 5.2.0,es版本號

{
"name" : "4onsTYV",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "nKZ9VK_vQdSQ1J0Dx9gx1Q",
"version" : {
"number" : "5.2.0",
"build_hash" : "24e05b9",

"build_date" : "2017-01-24T19:52:35.800Z",
"build_snapshot" : false,
"lucene_version" : "6.4.0"
},
"tagline" : "You Know, for Search"
}

5、修改集群名稱:elasticsearch.yml
6、下載和解壓縮Kibana安裝包,使用裏面的開發界面,去操作elasticsearch,作為我們學習es知識點的一個主要的界面入口
7、啟動Kibana:bin\kibana.bat

8、進入Dev Tools界面
9、GET _cluster/health

二、電商網站商品管理:集群健康檢查

1、document數據格式
2、電商網站商品管理案例:背景介紹
3、簡單的集群管理
4、商品的CRUD操作(document CRUD操作)


1、document數據格式

面向文檔的搜索分析引擎

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

public class Employee {

private String email;
private String firstName;
private String lastName;
private EmployeeInfo info;
private Date joinDate;

}

private class EmployeeInfo {

private String bio; // 性格
private Integer age;
private String[] interests; // 興趣愛好

}

EmployeeInfo info = new EmployeeInfo();
info.setBio("curious and modest");
info.setAge(30);
info.setInterests(new String[]{"bike", "climb"});

Employee employee = new Employee();employee.setEmail("[email protected]");
br/>employee.setEmail("[email protected]");
employee.setLastName("zhang");
employee.setInfo(info);
employee.setJoinDate(new Date());

employee對象:裏面包含了Employee類自己的屬性,還有一個EmployeeInfo對象

兩張表:employee表,employee_info表,將employee對象的數據重新拆開來,變成Employee數據和EmployeeInfo數據
employee表:email,first_name,last_name,join_date,4個字段
employee_info表:bio,age,interests,3個字段;此外還有一個外鍵字段,比如employee_id,關聯著employee表

{
"email": "[email protected]",
"first_name": "san",
"last_name": "zhang",
"info": {
"bio": "curious and modest",
"age": 30,
"interests": [ "bike", "climb" ]
},
"join_date": "2017/01/01"
}

我們就明白了es的document數據格式和數據庫的關系型數據格式的區別


2、電商網站商品管理案例背景介紹

有一個電商網站,需要為其基於ES構建一個後臺系統,提供以下功能:

(1)對商品信息進行CRUD(增刪改查)操作
(2)執行簡單的結構化查詢
(3)可以執行簡單的全文檢索,以及復雜的phrase(短語)檢索
(4)對於全文檢索的結果,可以進行高亮顯示
(5)對數據進行簡單的聚合分析


3、簡單的集群管理

(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
1488006741 15:12:21 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488007113 15:18:33 elasticsearch green 2 2 2 1 0 0 0 0 - 100.0%

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1488007216 15:20:16 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%

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

green:每個索引的primary shard和replica shard都是active狀態的
yellow:每個索引的primary shard都是active狀態的,但是部分replica shard不是active狀態,處於不可用的狀態
red:不是所有索引的primary shard都是active狀態的,部分索引有數據丟失了

為什麽現在會處於一個yellow狀態?

我們現在就一個筆記本電腦,就啟動了一個es進程,相當於就只有一個node。現在es中有一個index,就是kibana自己內置建立的index。由於默認的配置是給每個index分配5個primary shard和5個replica shard,而且primary shard和replica shard不能在同一臺機器上(為了容錯)。現在kibana自己建立的index是1個primary shard和1個replica shard。當前就一個node,所以只有1個primary shard被分配了和啟動了,但是一個replica shard沒有第二臺機器去啟動。

做一個小實驗:此時只要啟動第二個es進程,就會在es集群中有2個node,然後那1個replica shard就會自動分配過去,然後cluster status就會變成green狀態。

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

GET /_cat/indices?v

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb

(3)簡單的索引操作

創建索引:PUT /test_index?pretty

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_index XmS9DTAtSkSZSwWhhGEKkQ 5 1 0 0 650b 650b
yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb

刪除索引:DELETE /test_index?pretty

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana rUm9n9wMRQCCrRDEhqneBg 1 1 1 0 3.1kb 3.1kb


4、商品的CRUD操作

(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
}
}

我的風格,其實有選擇的情況下,不太喜歡念ppt,或者照著文檔做,或者直接粘貼寫好的代碼,盡量是純手敲代碼

(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集群上線經驗技術點Welcome to my ELK world!(2)