hive导出数据
hive -e “sql语句” > 路径
这个方法最为常见,sql的查询结果将直接保存到/home/output/out.txt中
hive -e "select user, login_timestamp from user_login" > /home/output/out.txt
当sql脚本过多时,也可以使用 -f sql文件名 ,按下面的方式执行查询,并保存结果
hive -f file.sql > /home/hadoop/output/cai/out.txt
导出csv文件
hive导出数据默认分隔符为"\t
",需要转换成",
"
在执行语句后加入 | tr "\t" ","
hive -e "select * from table " | tr "\t" ","> /home/output/out.csv
有些文件包含中文在导出csv后可能回出现乱码情况
导出数据包含标题
增加:
set hive.cli.print.header=true;
hive -e "set hive.cli.print.header=true; select user, login_timestamp from user_login" > /home/output/out.txt
hive报错
sql没错但是任务失败
可能原因是被更高资源的任务抢占了,导致失败次数超过设定的失败次数,进而报错。尝试通过下面代码解决。
set hive.vectorized.execution.enabled=false;
设置非严格模式
- 分区
- 排序limit
- 可以产生笛卡尔积
set hive.mapred.mode = strict;
set hive.mapred.mode = nonstrict;
hive展示
表的字段名显示
set hive.cli.print.header=true;
显示数据库名称
set hive.cli.print.current.db=true;
hive url解码
select reflect("java.net.URLDecoder", "decode", "%E4%B8%AD%E5%9B%BD", "UTF-8");