hive脚本的执行方式

hive脚本的执行方式大致有三种:

  • hive控制台执行;
  • hive -e "SQL"执行;
  • hive -f SQL文件执行;
    参考hive用法:
usage: hive
-d,--define <key=value> Variable subsitution to apply to hive
commands. e.g. -d A=B or --define A=B
--database <databasename> Specify the database to use
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
-h <hostname> connecting to Hive Server on remote host
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable subsitution to apply to hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-p <port> connecting to Hive Server on port number
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the
console)
1.1. hive控制台执行

顾名思义,是进入hive控制台以后,执行sql脚本,例如:

hive> set mapred.job.queue.name=pms;
hive> select page_name, tpa_name from pms.pms_exps_prepro limit 2;
Total MapReduce jobs = 1
Launching Job 1 out of 1
...
Job running in-process (local Hadoop)
2015-10-23 10:06:47,756 null map = 100%, reduce = 0%
2015-10-23 10:06:48,863 null map = 23%, reduce = 0%
2015-10-23 10:06:49,946 null map = 38%, reduce = 0%
2015-10-23 10:06:51,051 null map = 72%, reduce = 0%
2015-10-23 10:06:52,129 null map = 100%, reduce = 0%
Ended Job = job_local1109193547_0001
Execution completed successfully
Mapred Local Task Succeeded . Convert the Join into MapJoin
OK
APP首页 APP首页_价格比京东低
APP首页 APP首页_价格比京东低
Time taken: 14.279 seconds
hive>
1.2. hive -e "SQL"方式执行

利用hive -e "SQL"的方式进入hive控制台并直接执行sql脚本,例如:

hive -e "
set mapred.job.queue.name=pms;
set mapred.job.name=[HQL]exps_prepro_query; select page_name, tpa_name
from pms.pms_exps_prepro
limit 2;"
1.3. hive -f SQL文件方式执行

执行sql文件中的sql脚本,例如:

pms_exps_prepro.sql文件内容如下:

set mapred.job.queue.name=pms;
set hive.exec.reducers.max=48;
set mapred.reduce.tasks=48;
set mapred.job.name=[HQL]pms_exps_prepro; drop table if exists pms.pms_exps_prepro;
create table pms.pms_exps_prepro as
select
a.provinceid,
a.cityid,
a.ieversion,
a.platform,
'${date}' as ds
from track_exps a;

上述文件中的sql脚本接收一个日期,接收参数写法类似${date},执行时如下执行:

date=2015-10-22
hive -f pms_exps_prepro.sql --hivevar date=$date
date=2015-10-22
hive -f pms_exps_prepro.sql --hivevar date=$date

2. hive转义字符的问题

下面以一个业务场景阐述关于hive转义字符的问题

track_exps记录曝光数据,现在小A希望获取2015-10-20有效的曝光数据
其中有效的曝光记录是指,

  • relatedinfo字段满足数字.数字.数字.数字.数字的格式,
    例如4.4.5.1080100.1

extfield1字段满足request-字符串,section-数字的格式,
例如request-b470805b620900ac492bb892ad7e955e,section-4
对于这个问题,小A写出了如下sql脚本:

select
*
from track_exps
where ds = '2015-10-20'
and relatedinfo rlike '^4.\d+.\d+.\d+.\d+$'
and extfield1 rlike '^request.+section-\d+$';

但是由于正则表达式是被包含在sql里面,所以里面的特殊字符需要转义

2.1. hive -e "SQL"的方式执行

改动如下:

 hive -e "
set mapred.job.queue.name=pms; explain select
cityid
from track_exps
where ds = '2015-10-20'
and relatedinfo rlike '\\^4\\.\\\d\\+\\.\\\d\\+\\.\\\d\\+\\.\\\d\\+\\$'
and extfield1 rlike '\\^request\\.\\+section\\-\\\d\\+\\$';"

查看执行计划,可以确定正则表达式解析正确了:

...
predicate:
expr: ((relatedinfo rlike '^4.\d+.\d+.\d+.\d+$') and (extfield1 rlike '^request.+section-\d+$'))
type: boolean
...

分析如下:

在hive -e “SQL"的执行方式中,”‘正则表达式’",正则表达式先被一个单引号括起来,再被一个双引号括起来的,所以正则表达式里面,\^的第一个\用来解析第二个\,第二个\才真正起到了转义的作用

2.2. hive -f SQL文件的方式执行

改动如下:

pms_exps_prepro.sql文件内容如下:

select
*
from track_exps
where ds = '2015-10-20'
and relatedinfo rlike '\^4\.\\d\+\.\\d\+\.\\d\+\.\\d\+\$'
and extfield1 rlike '\^request\.\+section\-\\d\+\$';

分析如下:

不同于hive -e "SQL"的执行方式,因为是sql文件,所以正则表达式只被一个单引号括起来而已,一个\就起到了转义的作用了

注意:今天脑子突然糊涂了,对着脚本第一行是 #!/bin/sh 疯狂执行hive -f 结果报错,很愚蠢的问题就是,这样的文件应该是Linux的执行方式 是:sh 文件名  而不是hive -f sql文件

hive的shell用法(脑子糊涂了,对着脚本第一行是 #!/bin/sh 疯狂执行hive -f 结果报错)的更多相关文章

  1. 1.2 位于Shell脚本第一行的#!

    学习<shell脚本学习指南>一书,记录总结,便于自己回忆,希望对你有帮助! 2.4 自给自足的脚本:位于第一行的 #! 1.Shell脚本执行过程 当Shell执行一个程序时,会要求UN ...

  2. shell执行${var:m:n}报错Bad substitution解决办法

    Ubuntu系统下,执行字符串截取脚本时,总是报错:Bad substitution,脚本非常简单如下: #!/bin/sh str1="hello world!" :} 执行后报 ...

  3. shell脚本第一课

    shell脚本的文件名一般是以.sh结尾,也可以以其他格式如.txt,甚至不加后缀. 脚本的第一行的#!/bin/bash表示指定脚本执行时的解析器. #!/bin/bash #文件名:test.sh ...

  4. Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient报错,问题排查

    背景 最近在整合pyspark与hive,新安装spark-2.3.3以客户端的方式访问hive数据,运行方式使用spark on yarn,但是在配置spark读取hive数据的时候,这里直接把hi ...

  5. Linux编程 9 (shell类型,shell父子关系,子shell用法)

    一. shell类型 1.1  交互式 bin/ shell程序 当用户登录到某个虚拟控制台终端或是在GUI中启动终端仿真器时,默认的shell程序就会开始运行.系统启动什么样的shell程序取决于你 ...

  6. Hive学习之路 (十八)Hive的Shell操作

    一.Hive的命令行 1.Hive支持的一些命令 Command Description quit Use quit or exit to leave the interactive shell. s ...

  7. Hive(八)Hive的Shell操作与压缩存储

    一.Hive的命令行 1.Hive支持的一些命令 Command Description quit Use quit or exit to leave the interactive shell. s ...

  8. Hive 基本语法操练(五):Hive 的 JOIN 用法

    Hive 的 JOIN 用法 hive只支持等连接,外连接,左半连接.hive不支持非相等的join条件(通过其他方式实现,如left outer join),因为它很难在map/reduce中实现这 ...

  9. Hive row_number() 等用法

    1.row_number() over()排序功能: (1) row_number() over()分组排序功能: 在使用 row_number() over()函数时候,over()里头的分组以及排 ...

随机推荐

  1. Linux (x86) Exploit 开发系列教程之二(整数溢出)

    (1)漏洞代码 //vuln.c #include <stdio.h> #include <string.h> #include <stdlib.h> void s ...

  2. python--接口自动化测试(接口状态)

    本节开始,开始介绍python的接口自动化测试,首先需要搭建python开发环境,到https://www.python.org/下载python 版本直接安装就以了,建议 下载python2.7.1 ...

  3. Python习题002

    作业1:判断某一个字符串是否是小数 def is_float(string): string1 = str(string) if string1.count('.') > 1: #检测字符串小数 ...

  4. Python开发【第七章】:异常处理

    一.异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! #异常处理 list = [&qu ...

  5. HTML中关于动态创建的标签无法绑定js事件的解决方法:.on()方法的 [.selector]

    在前端页面的时候,会经常遇到用JavaScript动态创建出来的Button按钮或其他标签无法使用点击事件的问题.如下代码,使用jquery在body中动态创建一个class为demo的Button按 ...

  6. tiny-Spring【2】逐步step分析-新加入特性

    tiny-Spring是黄亿华大佬自己写的一个集合IOC和AOP于一身的一种轻量级[教学用]Spring框架,它的github库地址为:https://github.com/code4craft/ti ...

  7. c#基础知识梳理(三)

    上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10824638.html 一.方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C ...

  8. POJ1065(Wooden Sticks)--贪心

    木棍 时间限制: 1000MS   内存限制: 10000K 提交总数: 27336   接受: 11857 描述 有一堆木棍.每根杆的长度和重量是预先已知的.这些木棍将由木工机器逐一加工.它需要一些 ...

  9. 2 webpack 4 加vue搭建开发环境最终配置

    1 package.json { "name": "c", "version": "1.0.0", "desc ...

  10. K2 BPM_携手捷普:让流程立于云端,臻于至上_全球领先的工作流引擎

    在工业4.0地催化下,新一代信息技术与高科制造业深度融合,正在引发影响深远的产业变革,形成了新的生产方式.产业形态.商业模式和经济增长点. 捷普作为世界上最大型的电子制造服务公司之一,正站在新的历史发 ...