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自动按天创建索引脚本的更多相关文章

  1. ElasticSearch(java) 创建索引

    搜索]ElasticSearch Java Api(一) -创建索引 标签: elasticsearchapijavaes 2016-06-19 23:25 33925人阅读 评论(30) 收藏 举报 ...

  2. Elasticsearch 使用集群 - 创建索引

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  3. kibana自动创建索引

    一般索引按月.季或年为单位创建索引.我这里写成logstash-www-2019-03,www是URL的二级域名.格式类型完全根据自己方便就行. 当ELK集群中的索引过多时,我这里有100多个不同的日 ...

  4. sqlserver 生成脚本执行创建索引

    create or alter proc SP_CreateIndex as begin if exists(select * from sys.objects where name='execsql ...

  5. Docker安装ElasticSearch 以及使用LogStash实现索引库和数据库同步

    1:下载 ElasticSearch 镜像 docker pull docker.io/elasticsearch:5.6.8 2:创建 ElasticSearch 容器: 注意:5.0默认分配jvm ...

  6. MySQL如何创建一个好索引?创建索引的5条建议【宇哥带你玩转MySQL 索引篇(三)】

    MySQL如何创建一个好索引?创建索引的5条建议 过滤效率高的放前面 对于一个多列索引,它的存储顺序是先按第一列进行比较,然后是第二列,第三列...这样.查询时,如果第一列能够排除的越多,那么后面列需 ...

  7. elasticsearch 5.6.4自动创建索引与mapping映射关系 +Java语言

    由于业务上的需求 ,最近在研究elasticsearch的相关知识 ,在网上查略了大部分资料 ,基本上对elasticsearch的数据增删改都没有太大问题 ,这里就不做总结了  .但是,在网上始终没 ...

  8. linux下实现shell脚本自动连接mongodb数据库并创建索引

    在linux下创建shell脚本

  9. elasticsearch创建索引

    1.通过elasticsearch-head 创建 (1)登录localhost:9100 (2)点击复合查询 (3)输入内容 (4)勾选易读,点击验证是否是JSON格式 (5)点击提交请求,返回 { ...

随机推荐

  1. 039、Data Volume 之 bind mount (2019-02-28 周四)

    参考https://www.cnblogs.com/CloudMan6/p/7142150.html     Date Volume 本质上是Dokcer host文件系统中的目录或者文件,能够直接被 ...

  2. adduser Ubuntu添加sudo用户

    第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到s ...

  3. Mac 键盘符号 及VSCode快捷键 说明

    Mac 键盘符号说明 ⌘ == Command ⇧ == Shift ⇪ == Caps Lock ⌥ == Option ⌃ == Control ↩ == Return/Enter ⌫ == De ...

  4. windows生成dump文件

    windows下程序有时突然崩溃了,偶发性的崩溃很难找.于是就需要保存崩溃时的dump信息了. 下面是关于如何生成dmp文件的代码. 头文件 #pragma once #include <win ...

  5. Python之进程 3 - 进程池和multiprocess.Poll

    一.为什么要有进程池? 在程序实际处理问题过程中,忙时会有成千上万的任务需要被执行,闲时可能只有零星任务.那么在成千上万个任务需要被执行的时候,我们就需要去创建成千上万个进程么?首先,创建进程需要消耗 ...

  6. org.springframework.web.util.NestedServletException : Handler processing failed; nested exception is java.lang.StackOverflowError

    1 ,错误原因,循环冗余检查      result.setNearUsers(userList);            Page page = new Page();            pag ...

  7. url编码解码的问题(urlencode/quote)

    import urllib.parse params = { "wd":"hello人工智能" } # 将字典形式的进行编码 query_str = urlli ...

  8. vue学习之template标签

    HTML5提供的新标签,具有以下特性: 1.该元素天生display:none,同时模板元素内部内容也是隐藏的 2.位置任意,可以在<head>中,也可以在<body>或者&l ...

  9. Python 8 - Socket编程进阶

    本节内容:    1.Socket语法及相关    2.SocketServer实现多并发 Socket语法及相关 socket 概念 socket本质上就是在2台网络互通的电脑之间架设一个通道,两台 ...

  10. 基于netty实现单聊、群聊功能

    学习资料 https://juejin.im/book/5b4bc28bf265da0f60130116/section/5b6a1a9cf265da0f87595521 收获: 转载 1. Nett ...