隨著業務量的不斷上升,最近一段時間需要對生產環境中的elasticsearch集群中的歷史索引數據做遷移,而在做遷移前需要對被遷移的elasticsearch索引數據做統計用於遷移後的驗證統計,所以就寫了一個腳本用於es數據中查詢歷史索引的量生成報表文件,而在其中有使用過jq工具用於取數,jq的介紹可以查看http://jim123.blog.51cto.com/4763600/1966964:
#!/bin/bash #es_count_report.sh #used for elasticsearch monthly numbers index #you must install jq and curl #writer jim #2017.09.19 #Position parameter judgment datetime=$(date +"%Y%m") if [ $# -lt 1 ];then echo "Please enter the date 'year month'" echo "ex> $0 ${datetime}" exit 1 fi if [ $# -gt 1 ]; then echo "The input host address are too much" echo "ex> $0 ${datetime}" exit 1 fi #這裏在elasticsearch中取數時有用到curl和jq rpm -qa | grep jq && rpm -qa | grep curl if [ $? -ne 0 ];then yum -y install jq curl fi es_ip="192.168.2.200" es_port="9200" monthtime=$1 #elasticsearch的相關信息及傳入的時間 data_index="data-${monthtime}" index_name_all=$(curl -s "http://${es_ip}:${es_port}/_cat/indices?v" | grep ${data_index} | awk '{print $3}' | sort) report_file="$(pwd)/index_num_"${monthtime}".txt" cat /dev/null > $report_file #至空生成一個新文件用於記錄 for i in $index_name_all do index_num=$(curl -s -XGET "http://${es_ip}:${es_port}/${i}/poll/_search/?pretty" -d '{"_source":true,"size": 0}'|jq '.hits.total') && echo "$i:$index_num" >> $report_file done
總之在平時可以根據elasticsearch的api接口實現各種不同的數據統計
本文出自 “Jim的技術隨筆” 博客,謝絕轉載!
Tags: quot elasticsearch 遷移 索引 用於 curl
文章來源: