帮我写一个脚本,要求如下:1、beeline连接进入hive,设置输出格式为csv2。2、批量统计yinzhou库下表名开头为db_的表的数据量。3、并输出仅包含包名和数据量的txt
时间: 2024-03-12 12:44:36 浏览: 20
好的,以下是符合要求的脚本:
```bash
#!/bin/bash
# 连接 Hive 并设置输出格式为 CSV2
beeline -u jdbc:hive2://localhost:10000 -n username -p password << EOF > /dev/null
set hive.cli.print.header=false;
set hive.resultset.use.unique.column.names=false;
set hive.cli.print.row.to.vertical=false;
set hive.cli.print.current.db=false;
set hive.cli.print.header=true;
set csv2;
EOF
# 统计 yinzhou 库下表名以 db_ 开头的表的数据量
tables=$(beeline -u jdbc:hive2://localhost:10000 -n username -p password -e "show tables in yinzhou;" | grep "^db_" | tr '\n' ',' | sed 's/.$//')
for table in $(echo $tables | tr "," "\n")
do
data_size=$(beeline -u jdbc:hive2://localhost:10000 -n username -p password -e "select count(*) from yinzhou.\`$table\`" | awk '{if(NR>2)print}')
echo "$table,$data_size"
done > result.txt
```
这个脚本的具体实现如下:
1. 连接 Hive 并设置输出格式为 CSV2,这样输出的结果就是 CSV 格式,方便后续处理;
2. 使用 `show tables` 命令查询 yinzhou 库中的所有表名,并筛选出以 db_ 开头的表名,并保存到变量 `tables` 中;
3. 使用 for 循环依次查询每个表的数据量,并将包名和数据量以逗号分隔的形式输出到标准输出;
4. 将标准输出重定向到文件 result.txt 中,以便后续处理。
注意,在查询表数据量时,需要使用 `count(*)` 而不是 `count()`,这样可以避免出现统计错误的情况。另外,为了避免表名中包含特殊字符导致的语法错误,需要在表名前后加上反引号。