3,kibana的搭建

一 Kibana的安装及配置
  Kibana的安装:
  Kibana包含前端展示、es操作简化
  yum localinstall kibana-7.6.2-x86_64.rpm -y

Kibana配置修改kibana.yml,连接es的用户名密码需要正确
	server.port: 5601
	server.host: "0.0.0.0"
	elasticsearch.hosts: ["http://xxx:9200", "http://xxx:9200"]
	elasticsearch.username: "elastic"
	elasticsearch.password: "sjgpwd"
	logging.dest: /tmp/kibana.log

Kibana的启动和访问
  systemctl enable kibana
  systemctl restart kibana
  检查端口、访问kibana、登录尝试


Kibana简化ES的操作
验证集群是否成功
  curl -u elastic:sjgpwd http://xxx:9200  -> GET /
  curl -u elastic:sjgpwd http://xxx:9200/_cat/nodes?v  -> GET /_cat/nodes?v
  curl -u elastic:sjgpwd http://xxx:9200/_cat/indices?v  -> GET /_cat/indices?v
  由于地址、用户名、密码已经配置在Kibana,所以可以直接简化访问

Kibana提示功能
  GET /_cat
  GET /_cat/nodes?v

插入数据-X PUT
PUT /sjg/_doc/1
{
  "name":"sjg", 
  "age": 30
}

ES查询数据
GET /sjg/_doc/1
GET /sjg/_search?q=*

写入随机id
POST /sjg/_doc
{
  "name":"sjg", 
  "age": 20
}

ES修改数据
POST /sjg/_update/1
{
  "doc": {
    "age": 28
  }
}

修改所有的数据
POST /sjg/_update_by_query
{
  "script": {
    "source": "ctx._source['age']=28" 
  },
  "query": {
    "match_all": {}
  }
}

增加字段
POST /sjg/_update_by_query
{
  "script":{
    "source": "ctx._source['city']='hangzhou'"
  },
  "query":{
    "match_all": {}
  }
}

ES删除数据
DELETE /sjg/_doc/1
DELETE /sjg
DELETE /sjg*


二 索引的分片数及副本数设置
索引的分片数及副本数设置:两台es,最多一个副本
PUT /sjg
{
  "settings": {
  "number_of_shards" : 2,
  "number_of_replicas" : 0
  }
}

分片确认
  获取分片信息 GET /sjg/_search_shards
  根据id查询在哪个分片 GET /sjg/_search_shards?routing=yBRjn3MB4u0rZ3IOTB8p

索引创建后分片不可修改,副本数量可修改
PUT /sjg/_settings
{
  "number_of_shards" : 4,
  "number_of_replicas": 1
}

索引模板
  如果要每个索引都要单独设置分片数、分片副本数,操作会比较麻烦
  索引模板可以针对一批索引设置分片、副本,例如可针对sjg*设置
  内置索引模板:GET _template

简单索引模板创建
PUT _template/sjgtemplate
{
  "index_patterns": ["sjg*"],   
  "settings":{   
    "number_of_shards": 2,
    "number_of_replicas": 0
  }
}

先插入数据,再检查索引模板是否生效
POST /sjg1/_doc
{
  "name":"sjg2", 
  "age": 30
}

POST /sjg2/_doc
{
  "name":"sjg2", 
  "age": 30
}


三 Python程序操作ES集群
安装Python扩展
yum install python36 -y
pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
pip3 install elasticsearch==7.6.0 -i https://mirrors.aliyun.com/pypi/simple/

添加数据
from elasticsearch import Elasticsearch
es = Elasticsearch(['http://elastic:sjgpwd@192.168.238.90:9200', 'http://elastic:sjgpwd@192.168.238.92:9200'])
body = {"name": "sjgpython", "age": 29}
es.index(index='sjg', body=body)
print('Insert Success')

查询数据
print(es.search(index='sjg'))

删除索引
print(es.indices.delete(index='sjg'))

循环添加数据
import time
from elasticsearch import Elasticsearch
es = Elasticsearch(['http://elastic:sjgpwd@192.168.238.90:9200', 'http://elastic:sjgpwd@192.168.238.92:9200'])
for i in range(10000):
  body = {"name": "sjg{0}".format(i), "count": i}
  es.index(index='sjg', body=body)
  time.sleep(0.5)
  print('insert {0}'.format(i))

 

posted @ 2020-10-14 21:31  pwcc  阅读(237)  评论(0)    收藏  举报