索引表和ES的一点点思考 - CSDN博客
- -在电商项目中,物理库存系统是个极其重要的系统,订单支付后,就会开始来占用物理库存. 一般情况下,库存系统都是要分库的,因为主要的操作是写操作,例如占用/释放/取消等写操作. 使用分库可以降低数据库写的压力. 尽管写操作为主,但是读操作也是有的. 比如说,库存占用的时候,得先查询是否有库存,而这个查询操作并不都会带上分库因子(用于路由到具体的某个数据库),而是一些比较宽松的查询条件,这些查询条件对应的数据可能分布在不同的数据库上.
#/bin/bash
ES_URL="http://127.0.0.1:9200" #填写你的es对外http连接地址
ES_USER="username" #name代表你的你的es用户名
ES_PASSWORD="password" #password代表你的es用户密码
delete_index() {
echo -e "\nstart close and delete $1 days index"
beforeday=$(date -d '-'$1'days' +'%Y%m%d')
for i in `cat ./del_es_index_$1`; do
echo -e "\nstart to detect index of $i"
index_seq=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "$i" | sort -n | sed -r 's/$i_(.*)/\1/'`
for j in `echo $index_seq`;do
indexday=`echo ${j:0-10}`
indexFormatDate=`echo $indexday | head -n 1 | sed 's/\.//g'`
if [[ $beforeday -ge $indexFormatDate ]]; then
echo -e "\n$(date '+%Y-%m-%d %H:%M:%S') es_index match successful,当前索引:\e[0;35m$j\e[0m"
curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$j/_close"
echo -e "$(date '+%Y-%m-%d %H:%M:%S') 索引\e[0;35m$j\e[0m, 关闭完成"
if [[ ` echo $? ` == 0 ]];then
curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$j"
echo -e "$(date '+%Y-%m-%d %H:%M:%S') 索引\e[0;35m$j\e[0m, 删除完成"
else
echo -e "$(date '+%Y-%m-%d %H:%M:%S') 索引\e[0;35m$j\e[0m, 关闭失败,无法进行删除"
fi
else
echo -e "\n$(date '+%Y-%m-%d %H:%M:%S') es_index match fail,当前索引:\e[0;35m$j\e[0m"
echo -e "$(date '+%Y-%m-%d %H:%M:%S') 索引\e[0;35m$j\e[0m, 没有过期,不需要关闭,over.."
fi
done
done
}
delete_index 7 # 索引保留7天
delete_index 15 # 索引保留15天
delete_index 3 # 索引保留3天