elasticsearch自动按天创建索引脚本

本文介绍了如何解决Elasticsearch中单个索引数据量过大导致查询困难的问题,提出通过按天创建索引来优化查询效率。同时分享了一个用于自动化创建索引的脚本,并提供了导出映射信息的方法。在测试环境或初始化ES集群时,如果需要清除所有数据,可以关闭elastic进程并删除nodes下的数据。
摘要由CSDN通过智能技术生成

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

# 1.创建今天和明天的索引
# 2.删除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 --
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值