hive的shell用法(腦子糊塗了,對着腳本第一行是 #!/bin/sh 瘋狂執行hive -f 結果報錯)


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"的方式執行

改動如下:

1 hive -e "
2 set mapred.job.queue.name=pms;
3 
4 explain select 
5     cityid
6 from track_exps
7 where ds = '2015-10-20'
8   and relatedinfo rlike '\\^4\\.\\\d\\+\\.\\\d\\+\\.\\\d\\+\\.\\\d\\+\\$' 
9   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文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM