hive 传递变量的两种方式
在使用hive开发数据分析代码时,经常会遇到需要改变运行参数的情况,比如select语句中对日期字段值的设定,可能不同时间想要看不同日期的数据,这就需要能动态改变日期的值。如果开发量较大、参数多的话,使用变量来替代原来的字面值非常有必要,本文总结了几种可以向hive的SQL中传入参数的方法,以满足类似的需要。
准备测试表和测试数据
第一步先准备测试表和测试数据用于后续测试:
1
2
3
|
hive> create database test;
OK
Time taken: 2.606 seconds
|
然后执行建表和导入数据的sql文件:
1
2
3
4
5
6
7
8
9
10
11
|
[czt@www.crazyant.net testHivePara]$ hive -f student.sql
Hive history file=/tmp/crazyant.net/hive_job_log_czt_201309131615_1720869864.txt
OK
Time taken: 2.131 seconds
OK
Time taken: 0.878 seconds
Copying data from file:/home/users/czt/testdata_student
Copying file: file:/home/users/czt/testdata_student
Loading data to table test.student
OK
Time taken: 1.76 seconds
|
其中student.sql内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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/czt/testdata_student'
INTO TABLE student;
|
testdata_student测试数据文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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中直接使用
测试的shell文件名:
1
2
3
4
5
|
#!/bin/bash
tablename="student"
limitcount="8"
hive -S -e "use test; select * from ${tablename} limit ${limitcount};"
|
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
|
[czt@www.crazyant.net testHivePara]$ sh -x shellhive.sh
+ tablename=student
+ limitcount=8
+ hive -S -e 'use test; select * from student limit 8;'
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
|
由于hive自身是类SQL语言,缺乏shell的灵活性和对过程的控制能力,所以采用shell+hive的开发模式非常常见,在shell中直接定义变量,在hive -e语句中就可以直接引用;
注意:使用-hiveconf定义,在hive -e中是不能使用的
修改一下刚才的shell文件,采用-hiveconf的方法定义日期参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/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};"
|
运行会失败,因为该脚本在shell环境中运行的,于是shell试图去解析${hiveconf:enter_school_date}和${hiveconf:min_age}变量,但是这两个SHELL变量并没有定义,所以会以空字符串放在这个位置。
运行时该SQL语句会被解析成下面这个样子:
1
|
+ hive -S -hiveconf enter_school_date=20130902 -hiveconf min_age=26 -e 'use test; explain select * from student where pdate='\'''\'' and sage>'\'''\'' limit 8;'
|
方法2:使用-hiveconf定义,在SQL文件中使用
因为换行什么的很不方便,hive -e只适合写少量的SQL代码,所以一般都会写很多hql文件,然后使用hive –f的方法来调用,这时候可以通过-hiveconf定义一些变量,然后在SQL中直接使用。
先编写调用的SHELL文件:
1
2
3
|
#!/bin/bash
hive -hiveconf enter_school_date="20130902" -hiveconf min_ag="26" -f testvar.sql
|
被调用的testvar.sql文件内容:
1
2
3
4
5
6
7
8
|
use test;
select * from student
where
pdate='${hiveconf:enter_school_date}'
and
sage > '${hiveconf:min_ag}'
limit 8;
|
执行过程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[czt@www.crazyant.net testHivePara]$ sh -x shellhive.sh
+ hive -hiveconf enter_school_date=20130902 -hiveconf min_ag=26 -f testvar.sql
Hive history file=/tmp/czt/hive_job_log_czt_201309131651_2035045625.txt
OK
Time taken: 2.143 seconds
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Kill Command = hadoop job -kill job_20130911213659_42303
2013-09-13 16:52:00,300 Stage-1 map = 0%, reduce = 0%
2013-09-13 16:52:14,609 Stage-1 map = 28%, reduce = 0%
2013-09-13 16:52:24,642 Stage-1 map = 71%, reduce = 0%
2013-09-13 16:52:34,639 Stage-1 map = 98%, reduce = 0%
Ended Job = job_20130911213659_42303
OK
7 name7 27 20130902
8 name8 28 20130902
Time taken: 54.268 seconds
|
hive 传递变量的两种方式的更多相关文章
- 向docker镜像中传递变量的两种方式
测试用到的python文件: #!/usr/bin/env python3 #conding: utf-8 from http.server import HTTPServer, BaseHTTPRe ...
- C++函数传递数组的两种方式
数组与指针. 传首地址过去,然后通过地址输出数组元素. 1.一维数组 #include<iostream> using namespace std; #include <cstrin ...
- smarty获取变量的两种方式
从上一篇随笔中,我们知道smarty可以通过assign()的方法注册变量,从而在前段读取变量:我们也可以从配置文件中获取变量,来具体看一下: 1.在configs文件夹中建一个test.conf文件 ...
- 【linux】linux 下 shell命令 执行结果赋值给变量【两种方式】
方法1:[通用方法] 使用Tab键上面的反引号 例子如下: find命令 模糊查询在/apps/swapping目录下 查找 文件名中包含swapping并且以.jar结尾的文件 使用反引号 引住命令 ...
- Spring使用JMS传递消息的两种方式
方式一:同步收发消息,使用JMS template 消费者阻塞等待消息的到来. 方式二:异步收发消息,使用message listener container 消费者提供一个listener,注册一个 ...
- hibernate createQuery查询传递参数的两种方式
String hql = "from InventoryTask it where it.orgId=:orgId"; Session session = getSession() ...
- 第一课:Centos下配置java环境变量的两种方式(jdk1.8)
配置java环境(yum安装) 1.查出java1.8的全部版本 yum list java-1.8* 2.安装你需要的java1.8 版本(安装的名字根据查询出来的结果输入这里只是举例) yum i ...
- mybatis 传递参数的两种方式与模糊匹配 很重要
- Laravel向视图传递变量的两种方法
//方法一 return view('home.user')->with('datas', $datas); //方法二 return view('home.user.my-indent',co ...
随机推荐
- java登陆验证码与JS无刷新验证
最近公司的项目的登陆模块由我负责,所以就做了个登陆小功能进行练手,其包括了用jQuery对用户名和密码进行不为null验证,和出于安全性考虑加了一个验证码的校验 别的不说先上代码 controller ...
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Javaweb实现的优优图书商城(含源码)
原文地址:http://www.cnblogs.com/liaoyu/p/uushop.html 源码地址:https://github.com/liaoyu/uushop 贴出一个大学时做的小项目, ...
- 机器人学 —— 轨迹规划(Introduction)
轨迹规划属于机器人学中的上层问题,其主要目标是计划机器人从A移动到B并避开所有障碍的路线. 1.轨迹计划的对象 轨迹规划的对象是map,机器人通过SLAM获得地map后,则可在地图中选定任意两点进行轨 ...
- PCL—低层次视觉—点云分割(最小割算法)
1.点云分割的精度 在之前的两个章节里介绍了基于采样一致的点云分割和基于临近搜索的点云分割算法.基于采样一致的点云分割算法显然是意识流的,它只能割出大概的点云(可能是杯子的一部分,但杯把儿肯定没分割出 ...
- js中的this怎么理解
本博客供自己学习备忘, js中的this感觉很混乱,目前还有不少地方搞得不是很清楚,看到一篇不错的文章,先摘下来 this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象 ...
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
题意:给出n个工作,已知每个工作需要的时间last,以及截止时间end,(必须在截止时间之前完成)问最多能够完成多少个工作 首先预处理,将这n件任务按照截止时间从小到大排序 然后用一个cur记录当前做 ...
- BZOJ1049: [HAOI2006]数字序列
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题解: ydc的题解:http://pan.baidu.com/share/link?u ...
- Liunx常用的特殊环境变量
[weiqiang.liu@l~]$ sh variable xiaoqiang xiaoxuenumber:2scname:variablefirst:xiaoqiangsecond:xiaoxue ...
- BZOJ 1861 书架
(╯-_-)╯╧╧ 此处为错误代码. #include<iostream> #include<cstdio> #include<cstring> #include& ...