hive命令的三种执行方式

hive命令的3种调用方式 

方式1:hive –f  /root/shell/hive-script.sql(适合多语句)

 hive-script.sql类似于script一样,直接写查询命令就行

不进入交互模式,执行一个hive script 

这里可以和静音模式-S联合使用,通过第三方程序调用,第三方程序通过hive的标准输出获取结果集。

 $HIVE_HOME/bin/hive -S -f /home/my/hive-script.sql (不会显示mapreduct的操作过程)

那么问题来了:如何传递参数呢?

demo如下:

start_hql.sh  内容:

#!/bin/bash
# -S 打印输出mapreduce日志
hive \
  -hivevar id=1  \
  -hivevar col2=2  \
  -S -f test.sql

test.sql 内容:
-- 数据库
use tmp; 
-- 表名
select * 
from tmp_jzl_20140725_test11
where 
id='${hivevar:id}' and col2='${hivevar:col2}';
 

方式2:hive -e  'sql语句'(适合短语句)

直接执行sql语句 

例如:
[root@cloud4 shell]# hive -e 'select * from t1'
静音模式:

[root@cloud4 shell]# hive -S -e 'select * from t1'  (用法与第一种方式的静音模式一样,不会显示mapreduce的操作过程)
此处还有一亮点,用于导出数据到linux本地目录下
例如:

[root@cloud4 shell]# hive -e 'select * from t1'  > test.txt
有点类似pig导出分析结果一样,都挺方便的

方式3:hive (直接使用hive交互式模式)

都挺方便的
介绍一种有意思的用法:
1.sql的语法

 

#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/;  表示文件的存储的格式

posted @ 2018-09-06 11:20  BoomOoO  阅读(14478)  评论(0编辑  收藏  举报