elasticsearch自动按天创建索引脚本
elasticsearch保存在一个索引中数据量太大无法查询,现在需要将索引按照天来建,查询的时候关联查询即可
有时候es集群创建了很多索引,删不掉,如果是测试环境或者初始化es集群(清空所有数据),可以直接关掉elastic进程,然后删除nodes下面的所有数据,再次启动集群即可,记录一下避免忘记
导出mapping信息放到/root/index_mapping目录下
1.导出的语句
yum install epel-release -y
yum install nodejs -y
yum install nodejs npm -y
npm install elasticdump -y
/root/node_modules/elasticdump/bin/elasticdump --ignore-errors=true --scrollTime=120m --bulk=true --input=http://10.30.138.62:9200/.kibana --output=mapping.json --type=mapping
2.创建索引的脚本如下
#!/bin/bash # .创建今天和明天的索引
# .删除3天以前的索引 today_date=`date '+%Y%m%d'`
tomorrow_date=`date -d tomorrow +%Y%m%d`
# 需要导入索引的es集群服务器ip
es_ip=10.10.33.84 # 找到具体的index目录和index,对这个index进行处理
# start log
echo "${today_date} create index start">> /data/scripts/create_index.log
for FULLPATH in `ls /root/index_mapping/*.json`;
do
# 文件名
FILE=${FULLPATH##*/}
# 去掉文件名后缀
FILE_NAME=${FILE%%.*}
# 找到类似push:user:req的索引名称
FILE_INDEX=`echo $FILE_NAME|sed 's/_/:/g'`
FILE_INDEX_NAME=${FILE_INDEX%:*}
echo "create index ${FILE_INDEX_NAME} start" >> /data/scripts/create_index.log
/root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:${today_date} --type=mapping
/root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:${tomorrow_date} --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170905 --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170904 --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170903 --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170906 --type=mapping
done
# end log
echo "${today_date} create index end">> /data/scripts/create_index.log three_daysago=`date -d '3 days ago' +%Y%m%d`
echo $three_daysago
echo "delete index ${three_daysago} start" >> /data/scripts/create_index.log
curl -XDELETE "http://10.10.33.84:9200/*${three_daysago}*"
echo "delete index ${three_daysago} end" >> /data/scripts/create_index.log
某些索引特殊处理,改进后的脚本
#!/bin/bash # .创建今天和明天的索引
# .删除3天以前的索引 today_date=`date '+%Y%m%d'`
tomorrow_date=`date -d tomorrow +%Y%m%d`
today_month=`date '+%Y%m'`
# 需要导入索引的es集群服务器ip
es_ip=10.10.33.84 # 找到具体的index目录和index,对这个index进行处理
# start log
echo "${today_date} create index start">> /data/scripts/create_index.log
for FULLPATH in `ls /root/index_mapping/*.json`;
do
# 文件名
FILE=${FULLPATH##*/}
# 去掉文件名后缀
FILE_NAME=${FILE%%.*}
# 找到类似push:user:req的索引名称
FILE_INDEX=`echo $FILE_NAME|sed 's/_/:/g'`
FILE_INDEX_NAME=${FILE_INDEX%:*}
echo "create index ${FILE_INDEX_NAME} start" >> /data/scripts/create_index.log
/root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:${today_date} --type=mapping
/root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:${tomorrow_date} --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170905 --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170904 --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170903 --type=mapping
# /root/node_modules/elasticdump/bin/elasticdump --input=${FULLPATH} --output=http://${es_ip}:9200/${FILE_INDEX_NAME}:20170906 --type=mapping
done
# end log
echo "${today_date} create index end">> /data/scripts/create_index.log three_daysago=`date -d '3 days ago' +%Y%m%d`
echo $three_daysago
echo "delete index ${three_daysago} start" >> /data/scripts/create_index.log
# 索引保留3天
curl -XDELETE "http://${es_ip}:9200/voice*${three_daysago}*"
curl -XDELETE "http://${es_ip}:9200/script*${three_daysago}*"
curl -XDELETE "http://${es_ip}:9200/push*${three_daysago}*"
curl -XDELETE "http://${es_ip}:9200/advert*${three_daysago}*"
curl -XDELETE "http://${es_ip}:9200/user*${three_daysago}*"
curl -XDELETE "http://${es_ip}:9200/speech*${three_daysago}*"
curl -XDELETE "http://${es_ip}:9200/user*${three_daysago}*"
echo "delete index ${three_daysago} end" >> /data/scripts/create_index.log
# bin开头的索引保留7天
seven_daysago=`date -d '7 days ago' +%Y%m%d`
echo $seven_daysago
echo "delete index ${seven_daysago} start" >> /data/scripts/create_index.log
curl -XDELETE "http://${es_ip}:9200/bin*${seven_daysago}*"
echo "delete index ${seven_daysago} end" >> /data/scripts/create_index.log curl -XPUT "http://${es_ip}:9200/*${today_month}*/_settings" -d '{"number_of_replicas": 0}'
echo "setting replication 0 ${today_date}" >> /data/scripts/create_index.log
查看索引结构的命令:
[root@u04es01 ~]# curl 10.19.142.99:/bin:user:task:/_mapping?pretty
{
"bin:user:task:20171230" : {
"mappings" : {
"_default_" : {
"properties" : {
"appId" : {
"type" : "keyword",
"store" : true
},
"taskFlag" : {
"type" : "integer",
"store" : true
},
"taskId" : {
"type" : "keyword",
"store" : true
},
"time" : {
"type" : "date",
"store" : true,
"format" : "yyyy-MM-dd HH:mm:ss.SSS.Z"
},
"uuid" : {
"type" : "keyword",
"store" : true
}
}
}
}
}
}
elasticsearch自动按天创建索引脚本的更多相关文章
- ElasticSearch(java) 创建索引
搜索]ElasticSearch Java Api(一) -创建索引 标签: elasticsearchapijavaes 2016-06-19 23:25 33925人阅读 评论(30) 收藏 举报 ...
- Elasticsearch 使用集群 - 创建索引
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- kibana自动创建索引
一般索引按月.季或年为单位创建索引.我这里写成logstash-www-2019-03,www是URL的二级域名.格式类型完全根据自己方便就行. 当ELK集群中的索引过多时,我这里有100多个不同的日 ...
- sqlserver 生成脚本执行创建索引
create or alter proc SP_CreateIndex as begin if exists(select * from sys.objects where name='execsql ...
- Docker安装ElasticSearch 以及使用LogStash实现索引库和数据库同步
1:下载 ElasticSearch 镜像 docker pull docker.io/elasticsearch:5.6.8 2:创建 ElasticSearch 容器: 注意:5.0默认分配jvm ...
- MySQL如何创建一个好索引?创建索引的5条建议【宇哥带你玩转MySQL 索引篇(三)】
MySQL如何创建一个好索引?创建索引的5条建议 过滤效率高的放前面 对于一个多列索引,它的存储顺序是先按第一列进行比较,然后是第二列,第三列...这样.查询时,如果第一列能够排除的越多,那么后面列需 ...
- elasticsearch 5.6.4自动创建索引与mapping映射关系 +Java语言
由于业务上的需求 ,最近在研究elasticsearch的相关知识 ,在网上查略了大部分资料 ,基本上对elasticsearch的数据增删改都没有太大问题 ,这里就不做总结了 .但是,在网上始终没 ...
- linux下实现shell脚本自动连接mongodb数据库并创建索引
在linux下创建shell脚本
- elasticsearch创建索引
1.通过elasticsearch-head 创建 (1)登录localhost:9100 (2)点击复合查询 (3)输入内容 (4)勾选易读,点击验证是否是JSON格式 (5)点击提交请求,返回 { ...
随机推荐
- 【转】javascript代码混淆和压缩
隐藏 JavaScript 源代码?不,你只能混淆和压缩JavaScript源代码 http://www.yaohaixiao.com/tools/confuse-and-compressing-ja ...
- 二叉树的python可视化和常用操作代码
二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...
- 使用JAVA数组实现顺序表
1,引入了JAVA泛型类,因此定义了一个Object[] 类型的数组,从而可以保存各种不同类型的对象. 2,默认构造方法创建了一个默认大小为16的Object数组:带参数的构造方法创建一个指定长度的O ...
- C#调用C++导出类的一个实例
一直认为带导出类dll的只有VC自己可以调用,其它编程语言无法调用,今天看到一篇文章才知道自己错了.https://blog.csdn.net/huiyouyongdeyu2011/article/d ...
- webpack-config.js 内容讲解
当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) var path = require('path') m ...
- lua 立即执行函数
背景 不同文件中,lua提供模块写法, 使用local修饰,可以将变量或者函数,声明为模块内有效,例如 模块暴漏变量使用 return 表的方式. local aafunc = function() ...
- qt5学习目录
qt5下载与安装,VS2017的环境配置 q ...
- Git学习一:基本用法
git config:配置相关信息 git clone:复制仓库 git init:初始化仓库 git add:添加更新内容到索引中 git diff:比较内容 git status:获取当前项目状况 ...
- jupyter notebook出现cannot import name 'create_prompt_application'问题(Died Kernel)
应该是在安装其它python第三方库时更新了prompt-toolkit版本,降级到下面的版本即可: sudo pip install 'prompt-toolkit==1.0.15'
- 第27月第18天 epoll lt et
1. While the usage of epoll when employed as a level-triggered interface does have the same semantic ...