ES初学

因阿里云es有多多限制,自建下es,使用docker安装

查询镜像

1
2
3
4
5
6
#搜索镜像
docker search elasticsearch
docker search kibana
#查看所有历史版本
curl https://registry.hub.docker.com/v1/repositories/elasticsearch/tags| tr -d '[\[\]" ]' | tr '}' '\n'| awk -F: -v image='elasticsearch' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'
curl https://registry.hub.docker.com/v1/repositories/elastic/kibana/tags| tr -d '[\[\]" ]' | tr '}' '\n'| awk -F: -v image='kibana' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'

拉取镜像

1
2
docker pull elasticsearch:7.10.1
docker pull kibana:7.10.1

创建数据文件夹

1
2
3
4
5
mkdir -p /opt/elasticsearch/config
mkdir -p /opt/elasticsearch/data

echo "http.host: 0.0.0.0" >/opt/elasticsearch/config/elasticsearch.yml
chmod -R 777 /opt/elasticsearch/

启动容器

1
2
3
4
5
6
7
#首次启动
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -d elasticsearch:7.10.1
#查看容器中配置文件路径
find / -name elasticsearch.yml
#删除容器
docker container ls -all
docker rm {$containerid}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ES
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /opt/elasticsearch/config/elasticsearch.yml:/var/lib/docker/overlay2/cb02e3fbbb80dfb252f1e7aaa1b35307f1424cbe21baf11bf36c7d895c572ff3/diff/usr/share/elasticsearch/config/elasticsearch.yml \
-v /opt/elasticsearch/data:/var/lib/docker/overlay2/cb02e3fbbb80dfb252f1e7aaa1b35307f1424cbe21baf11bf36c7d895c572ff3/diff/usr/share/elasticsearch/data \
-v /opt/elasticsearch/plugins:/var/lib/docker/overlay2/cb02e3fbbb80dfb252f1e7aaa1b35307f1424cbe21baf11bf36c7d895c572ff3/diff/usr/share/elasticsearch/plugins \
-d elasticsearch:7.10.1

#设置开机启动elasticsearch
docker update elasticsearch --restart=always

#Kibana
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://10.0.16.15:9200 -p 5601:5601 \
-e ES_JAVA_OPTS="-Xms32m -Xmx128m" \
-d kibana:7.10.1

#设置开机启动kibana
docker update kibana --restart=always

查看容器运行状态

1
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}\t{{.Status}}"

开启xpack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#进入容器
docker attach elasticsearch
#进不去的话换这个
docker exec -it elasticsearch /bin/bash
#修改配置文件
cd config
vi elasticsearch.yml
#添加保存
xpack.security.enabled: true
#重启elasticsearch容器
docker restart elasticsearch
#重新进入
docker exec -it elasticsearch /bin/bash
#初始化密码
cd bin
./elasticsearch-setup-passwords interactive
890425
#此处需要出入n个密码
1
2
3
4
5
#修改kibana配置文件
#进入容器
docker exec -it kibana /bin/bash
cd config
vi kibana.yml

新增连接es的地址、账号密码,添加汉化配置

重启

1
docker restart kibana

安装插件方法

1
2
3
4
5
6
7
8
9
#下载
wget https://d3g5vo6xdbdb9a.cloudfront.net/downloads/elasticsearch-plugins/opendistro-sql/opendistro_sql-1.13.2.0.zip
#复制
docker cp opendistro_sql-1.13.2.0.zip elasticsearch:/usr/share/elasticsearch/plugins
#进入容器目录
docker exec -it elasticsearch /bin/bash
cd bin
#执行
./elasticsearch-plugin install file:///usr/share/elasticsearch/plugins/opendistro_sql-1.13.2.0.zip

客户端(类似、替代工具等)

es-head 最好用

下载地址:

https://raw.githubusercontent.com/mobz/elasticsearch-head/master/crx/es-head.crx

dbeave、dbvisualizer等都需要破解白金版,还不能通过验证

与关系型数据库对应关系

MySQL ElasticSearch
Table Index
Table Type
Row Document
Column Field
Schema Mapping
Index Everything is indexed
SQL Query DSL
select * from … GET http://…
update table set … POST http://…
group by、avg、sum Aggregations
去重 distinct cardinality
数据迁移 reindex

Mapping 映射

模板、动态模板

Mapping参数

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html#mapping-params

Field字段类型

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#mapping-types


ES初学
https://blog.wongcw.cn/2022/06/18/ES初学/
作者
wangcw
发布于
2022年6月18日
许可协议