HIVE精讲数据分析之路

数仓--Hive-面试之向Hive中传入变量的方式

2019-05-10  本文已影响44人  李小李的路

Hive向程序中传递变量的方式

hive接收外部变量方式详解

数据准备

use test;
 
---学生信息表
create table IF NOT EXISTS student(
sno bigint comment '学号' ,
sname string comment '姓名' ,
sage bigint comment '年龄' ,
pdate string comment '入学日期'
)
COMMENT '学生信息表'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
 
LOAD DATA LOCAL INPATH
'/home/users/root/testdata_student'
INTO TABLE student;
1 name1 21 20130901
2 name2 22 20130901
3 name3 23 20130901
4 name4 24 20130901
5 name5 25 20130902
6 name6 26 20130902
7 name7 27 20130902
8 name8 28 20130902
9 name9 29 20130903
10 name10 30 20130903
11 name11 31 20130903
12 name12 32 20130904
13 name13 33 20130904

1-shell中设置变量,hive -e中直接使用

#!/bin/bash
tablename="student"
limitcount="8"
hive -S -e "use test; select * from ${tablename} limit ${limitcount};"

注意

#!/bin/bash
tablename="student"
limitcount="8"
 
hive -S \
    -hiveconf enter_school_date="20130902" \
    -hiveconf min_age="26" \
    -e \
    "    use test; \
        select * from ${tablename} \
        where \
            pdate='${hiveconf:enter_school_date}' \
            and \
            sage>'${hiveconf:min_age}' \
        limit ${limitcount};"

方法2:使用-hiveconf定义,在SQL文件中使用

#!/bin/bash
 
hive -hiveconf enter_school_date="20130902" -hiveconf min_ag="26" -f testvar.sql
use test;
select * from student
where
pdate='${hiveconf:enter_school_date}'
and
sage > '${hiveconf:min_ag}'
limit 8;

方法3:使用hivevar,再sql文件中使用

#!/bin/bash
hive -f testvar.sql
use test;
set hivevar:center_school_date="20130920";
set hivevar:min_age="26";
select * from student
where
pdate='${hivevar:enter_school_date}'
and
sage > '${hivevar:min_ag}'
limit 8;

总结

上一篇 下一篇

猜你喜欢

热点阅读