摘要:Hive命令三种执行方式:执行sql多语句方式、执行sql单语句方式和交互方式。
hive -i hive-script.sql
在hive启动cli之前,先执行指定文件(hive-script.sql)中的命令。
允许用户在cli启动时预先执行一个指定文件,比如,有一些常用的环境参数设置,频繁执行的命令,可以添加在初始化文件中
某些参数设置
set mapred.queue.names=queue3;
SET mapred.reduce.tasks=14;
添加udf文件
add JAR ./playdata-hive-udf.jar;
设置Hive的日志级别
hive -hiveconf hive.root.logger=INFO;
hive –f hive-script.sql(适合多语句)
hive -S -f hive-script.sql (不会显示mapreduct的操作过程)
hive -e 'select * from t1'(适合短语句)
hive -S -e 'select * from t1' (不会显示mapreduct的操作过程)
hive -e 'select * from t1' > test.txt (结果导出)
hive 启动
hive>quit; 退出hive
hive> show databases; 查看数据库
hive> create database test; 创建数据库
hive> use default; 使用哪个数据库
hive>create table t1 (key string); 创建表
hive>create table t1(id ) '/wlan'
partitioned by (log_date string) 表示通过log_date进行分区
row format delimited fields terminated by '\t' 表示代表用‘\t’进行分割来读取字段
stored as textfile/sequencefile/rcfile/; 表示文件的存储的格式
#!/bin/bash
# hive id counts
start_date=$1
end_date=$2
parameter=$#
sql=$(cat <<!EOF
set mapred.job.map.capacity=5;
set mapred.job.reduce.capacity=5;
set mapred.reduce.tasks=5;
set hive.exec.reducers.max=5;
select id, count(date), count(distinct date) from test group by id;
!EOF)
echo $sql
hive -e "$sql" > hive_id_count_${start_date}.txt
exitCode=$?
if [ $exitCode -ne 0 ];then
echo "[ERROR] hive execute failed!"
exit $exitCode
fi
start_hive.sh 内容:
#!/bin/bash
# -S 打印输出mapreduce日志
hive \
-hivevar id=1 \
-hivevar col2=2 \
-S -f test.sql
test.sql 内容:
-- 数据库
use tmp;
-- 表名
select *
from test1
where
id='${hivevar:id}' and col2='${hivevar:col2}';