设为首页 收藏本站
查看: 1441|回复: 0

[经验分享] 初探ELK-elasticsearch使用小结

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-13 08:54:05 | 显示全部楼层 |阅读模式
                      初探ELK-elasticsearch使用小结
2016/9/12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
一、安装
1、jdk 和 环境变量
支持jdk-1.7以上,推荐jdk-1.8
在环境变量配置:JAVA_HOME


2、安装
有2种方式下载,推荐缓存rpm包到本地yum源
1)直接使用rpm
wget https://download.elastic.co/elas ... ticsearch-2.4.0.rpm

2)使用yum源
[iyunv@vm220 ~]# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1

[iyunv@vm220 ~]# yum install elasticsearch

3)启动服务
[iyunv@vm220 ~]# chkconfig elasticsearch on
[iyunv@vm220 ~]# service elasticsearch start


3、调整配置
[iyunv@vm220 ~]# mkdir -p /data/elasticsearch
[iyunv@vm220 ~]# chown elasticsearch:elasticsearch /data/elasticsearch
[iyunv@vm220 ~]# grep ^[^#] /etc/elasticsearch/elasticsearch.yml
cluster.name: es-test
node.name: node-1
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
[iyunv@vm220 ~]# service elasticsearch restart  



二、使用 REST API
1、能干啥
Check your cluster, node, and index health, status, and statistics
Administer your cluster, node, and index data and metadata
Perform CRUD (Create, Read, Update, and Delete) and search operations against your indexes
Execute advanced search operations such as paging, sorting, filtering, scripting, aggregations, and many others

使用 curl 来操作 API 的方式:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>


2、管理
1)健康状态
[iyunv@vm220 ~]# curl 'localhost:9200/_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
1473669150 16:32:30  es-test green           1         1      0   0    0    0        0             0                  -                100.0%

health 的状态包括:green, yellow, red.
Green means everything is good (cluster is fully functional),
yellow means all data is available but some replicas are not yet allocated (cluster is fully functional)
red means some data is not available for whatever reason

2)列出节点
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/nodes?v'
host      ip        heap.percent ram.percent load node.role master name   
127.0.0.1 127.0.0.1            6          16 0.00 d         *      node-1

3)列出索引
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size


3、CRUD操作
1)创建索引
[iyunv@vm220 ~]# curl -XPUT 'localhost:9200/customer?pretty'
{
  "acknowledged" : true
}

创建了一个索引“customer”,且告知返回时使用一个 pretty-print 的方式(json)

再次列出索引
[iyunv@vm220 ~]# curl 'localhost:9200/_cat/indices?v'
health status index    pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer   5   1          0            0       650b           650b

请对比一下之前的执行结果。
请注意,这里的 health 是 yellow,因为我们目前只有一个es节点,没有副本,未做到高可用。

2)给上述索引创建一个 doc
索引 customer 类型为: "external" ,ID为:1

[iyunv@vm220 ~]# curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'

3)获取 doc
[iyunv@vm220 ~]# curl -XGET 'localhost:9200/customer/external/1?pretty'

4)重建索引
[iyunv@vm220 ~]# curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "Kelly Doe"
}'

5)创建索引时,不指定ID,将随机生成
[iyunv@vm220 ~]# curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
  "name": "Calvin Kern"
}'


6)删除索引
[iyunv@vm220 ~]# curl -XDELETE 'localhost:9200/customer?pretty'


7)更新 doc 中的数据
[iyunv@vm220 ~]# curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Eric Mood" }
}'

更新并增加:
[iyunv@vm220 ~]# curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Eric Mood", "age": 110 }
}'

8)删除索引太直接了,如何只删除其中某个doc呢?
[iyunv@vm220 ~]# curl -XDELETE 'localhost:9200/customer/external/1?pretty'


4、查看数据
1)导入测试数据:
wget https://github.com/bly2k/files/blob/master/accounts.zip?raw=true -O accounts.zip
unzip accounts.zip
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'

2)search
查看所有的数据:
方式一:
curl 'localhost:9200/bank/_search?q=*&pretty'

方式二:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match_all": {} }
}'

查看指定的数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}'


筛选数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}'


汇总数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state"
      }
    }
  }
}'


备注:具体内容请参考官网doc






ZYXW、参考
1、官网
https://www.elastic.co/guide/en/ ... p-repositories.html
https://www.elastic.co/guide/en/ ... -configuration.html
https://www.elastic.co/guide/en/ ... tup-dir-layout.html
https://www.elastic.co/guide/en/ ... g_your_cluster.html




                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-271483-1-1.html 上篇帖子: 初探ELK-kibana使用小结 下篇帖子: 初探ELK-logstash使用小结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表