随着业务量的不断上升,最近一段时间需要对生产环境中的elasticsearch集群中的历史索引数据做迁移,而在做迁移前需要对被迁移的elasticsearch索引数据做统计用于迁移后的验证统计,所以就写了一个脚本用于es数据中查询历史索引的量生成报表文件,而在其中有使用过jq工具用于取数,jq的介绍可以见本文最后。
#!/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接口实现各种不同的数据统计
jq : Linux下json的命令行工具
JSON是前端编程经常用到的格式,对于PHP或者Python,解析JSON都不是什么大事,尤其是PHP的json_encode和json_decode,干的相当的漂亮。Linux下也有处理处理JSON的神器:jq。
对于JSON格式而言,jq就像sed/awk/grep这些神器一样的方便,而也,jq没有乱七八糟的依赖,只需要一个binary文件jq,就足矣。下面我们看下jq的使用。
1 格式化JSON
-
manu@manu:~/code/php/json$ cat json_raw.txt
-
{"name":"Google","location":{"street":"1600 Amphitheatre Parkway","city":"Mountain View","state":"California","country":"US"},"employees":[{"name":"Michael","division":"Engineering"},{"name":"Laura","division":"HR"},{"name":"Elise","division":"Marketing"}]}
上面的JSON是PHP json_encode之后,echo出来的字符串,很明显,可读性太差。前一阵子写文档,需要将前后段JSON写入文档,我当时是用是网上的JSON格式化工具做的。事实上,jq就可以检查JSON的合法性,并把JSON格式化成更友好更可读的格式:
-
cat json_raw.txt | jq .
看到上图,将一团乱麻的JSON格式化成个更可读的形式。其实背后另外检查了JSON的合法性。如果JSON不合法,jq .会报错。我故意写个错误的JSON:
-
manu@manu:~/code/php/json$ cat json_err.txt
-
{"name":"Google","location":{"street":"1600 Amphitheatre Parkway","city":"Mountain View","state":"California","country":"US"},"employees":[{"name":"Michael","division":"Engineering"}{"name":"Laura","division":"HR"},{"name":"Elise","division":"Marketing"}]}
上面JSON中加粗和斜体部分,遗漏了一个逗号,所以这个JSON是错误的,jq轻松的可以轻松的检查出来:
-
manu@manu:~/code/php/json$ cat json_err.txt |jq .
-
parse error: Expected separator between values at line 1, column 183
2 JSON parse
如上图json,jq如何解析JSON,根据key获取value?
-
{
-
“key_1”:"value_1",
-
“key_2”:"value_2",
-
}
如何根据key获取value?
-
jq '.key'
解析不存在的元素,会返回null
-
echo '{"foo": 42, "bar": "less interesting data"}' | jq .nofoo
-
null
3 JSON nested parse(嵌套解析)
cat json_raw.txt | jq '.location.state'
"California"
4 JSON parse array
cat json_raw.txt | jq '.employees[1].name'
"Laura"
5 内建函数
jq还有一些内建函数如 key,has
key是用来获取JSON中的key元素的:
-
cat json_raw.txt | jq 'keys'
-
[
-
"employees",
-
"location",
-
"name"
-
]
has是用来是判断是否存在某个key:
-
cat json_raw.txt | jq 'has("name")'
-
true
-
-
cat json_raw.txt | jq 'has("noexisted")'
-
false
与JSON相关的我就暂时介绍到这
基于CentOS 6.9搭建ELK环境指南 http://www.linuxidc.com/Linux/2017-07/145636.htm
Linux日志分析ELK环境搭建 http://www.linuxidc.com/Linux/2017-07/145494.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-09/147002.htm