大数据

shell 脚本执行批量查询docker中hive数据表得数量

文 / sptk 来源 / 原创 阅读 / 178 8月前

这里提供了一个示例,shell调用docker中得hive 查询数据库中每张表的数据量,例如 可以用于校验导入数据是否成功. 更便捷的方式,我可以使用pyhive 编写python脚本更方便

#! /bin/bash
# set database name  or you can replace it use args
database=default
#gen query table command
qt_cmd="docker exec -it  hive-server bash -c 'hive -S -e \"show tables from ${database}\" 2>/dev/null'"
echo $qt_cmd

# exec query
# eval transform str to command to excute
tables=$(eval $qt_cmd)
for tb_name in ${tables[@]}; do
    # In Docker containers, the newline character used for output is \r\n,so need to fillter it.
    tb_name=`echo $tb_name | grep -oP '\w+'`

    # gen query table count command
    cmd_sh="docker exec -it hive-server bash -c 'hive -S -e \"select count(1) from default.${tb_name}\" 2>/dev/null'"

    # excute query command 
    cnt=$(eval $cmd_sh)

    # show table name and number of rows in the table data
    echo "$tb_name: $cnt"
done

0

站点声明:站点主要用于个人技术文章。

冀ICP备19037883号
相关侵权、举报、投诉及建议等,请发E-mail:804330969@qq.com