㈠ hive sql 怎麼把集合轉化成字元串
hive sql 怎麼把集合轉化成字元串
hive字元串函數
1. 字元串長度函數:length
語法: length(string A)
返回值: int
說明:返回字元串A的長度
㈡ Hive支持的數據類型
#整型
TINYINT — 微整型,只佔用1個位元組,只能存儲0-255的整數。
SMALLINT– 小整型,佔用2個位元組,存儲范圍–32768 到 32767。
INT– 整型,佔用4個位元組,存儲范圍-2147483648到2147483647。
BIGINT– 長整型,佔用8個位元組,存儲范圍-2^63到2^63-1。
#布爾型
BOOLEAN — TRUE/FALSE
#浮點型
FLOAT– 單精度浮點數。
DOUBLE– 雙精度浮點數。
#字元串型
STRING– 不設定長度。
Structs:一組由任意數據類型組成的結構。比如,定義一個欄位C的類型為STRUCT {a INT; b STRING},則可以使用a和C.b來獲取其中的元素值;
Maps:和Java中的Map相同,即存儲K-V對的;
Arrays:數組;
復雜數據類型的聲明必須使用尖括弧指明其中數據欄位的類型。定義三列,每列對應一種復雜的數據類型,如下所示。
TEXTFILE //文本,默認值
SEQUENCEFILE // 二進制序列文件
RCFILE //列式存儲格式文件 Hive0.6以後開始支持
ORC //列式存儲格式文件,比RCFILE有更高的壓縮比和讀寫效率,Hive0.11以後開始支持
PARQUET //列出存儲格式文件,Hive0.13以後開始支持
#參考博客:
http://lxw1234.com/archives/2015/06/238.htm
http://www.cnblogs.com/zlslch/p/5659714.html
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types
#
㈢ SQL查詢中,如何判斷一個字元串欄位的內容的長度
實現的方法和詳細的操作步驟如下:
1、首先,打開sql查詢器,並連接相應的資料庫表,例如store表,如下圖所示。
㈣ impala與hive區別之漢字字元串長度的不同問
hive主要是走maprece。這個是hadoop框架的一個應用,使用java寫的,,Impalad分為Java前端與C++處理後端
㈤ Hive sql及窗口函數
hive函數:
1、根據指定條件返回結果:case when then else end as
2、基本類型轉換:CAST()
3、nvl:處理空欄位:三個str時,是否為空可以指定返回不同的值
4、sql通配符: https://www.w3school.com.cn/sql/sql_wildcards.asp
5、count(1)與COUNT(*):返回行數
如果表沒有主鍵,那麼count(1)比count(*)快;
如果有主鍵,那麼count(主鍵,聯合主鍵)比count(*)快;
count(1)跟count(主鍵)一樣,只掃描主鍵。count(*)跟count(非主鍵)一樣,掃描整個表。明顯前者更快一些。
性能問題:
1.任何情況下SELECT COUNT(*) FROM tablename是最優選擇,(指沒有where的情況);
2.盡量減少SELECT COUNT(*) FROM tablename WHERE COL = 『value』 這種查詢;
3.杜絕SELECT COUNT(COL) FROM tablename WHERE COL2 = 『value』 的出現。
count(expression):查詢 is_reply=0 的數量: SELECT COUNT(IF(is_reply=0,1,NULL)) count FROM t_iov_help_feedback;
6、distinct與group by
distinct去重所有distinct之後所有的欄位,如果有一個欄位值不一致就不作為一條
group by是根據某一欄位分組,然後查詢出該條數據的所需欄位,可以搭配 where max(time)或者Row_Number函數使用,求出最大的一條數據
7、使用with 臨時表名 as() 的形式,簡單的臨時表直接嵌套進sql中,復雜的和需要復用的表寫到臨時表中,關聯的時候先找到關聯欄位,過濾條件最好在臨時表中先過濾後關聯
處理json的函數:
split(json_array_string(schools), '\\|\\|') AS schools
get_json_object(school, '$.id') AS school_id,
字元串函數:
1、instr(』源字元串』 , 『目標字元串』 ,』開始位置』,』第幾次出現』)
instr(sourceString,destString,start,appearPosition)
1.sourceString代表源字元串; destString代表要從源字元串中查找的子串;
2.start代表查找的開始位置,這個參數可選的,默認為1;
3.appearPosition代表想從源字元中查找出第幾次出現的destString,這個參數也是可選的, 默認為1
4.如果start的值為負數,則代表從右往左進行查找,但是位置數據仍然從左向右計算。
5.返回值為:查找到的字元串的位置。如果沒有查找到,返回0。
最簡單例子: 在abcd中查找a的位置,從第一個字母開始查,查找第一次出現時的位置
select instr(『abcd』,』a』,1,1) from al; —1
應用於模糊查詢:instr(欄位名/列名, 『查找欄位』)
select code,name,dept,occupation from staff where instr(code, 『001』)> 0;
等同於 select code, name, dept, occupation from staff where code like 『%001%』 ;
應用於判斷包含關系:
select ccn,mas_loc from mas_loc where instr(『FH,FHH,FHM』,ccn)>0;
等同於 select ccn,mas_loc from mas_loc where ccn in (『FH』,』FHH』,』FHM』);
2、substr(string A,int start,int len)和 substring(string A,int start,int len),用法一樣
substr(time,1,8) 表示將time從第1位開始截取,截取的長度為8位
第一種用法:
substr(string A,int start)和 substring(string A,int start),用法一樣
功效:返回字元串A從下標start位置到結尾的字元串
第二種用法:
substr(string A,int start,int len)和 substring(string A,int start,int len),用法一樣
功效:返回字元串A從下標start位置開始,長度為len的字元串
3、get_json_object(form_data,'$.學生姓名') as student_name
json_tuple 函數的作用:用來解析json字元串中的多個欄位
4、split(full_name, '\\.') [5] AS zq; 取的是數組里的第六個
日期(時間)函數:
1、to_date(event_time) 返回日期部分
2、date_sub:返回當前日期的相對時間
當前日期:select curdate()
當前日期前一天:select date_sub(curdate(),interval 1 day)
當前日期後一天:select date_sub(curdate(),interval -1 day)
date_sub(from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss'), 14) 將現在的時間總秒數轉為標准格式時間,返回14天之前的時間
時間戳>>>>日期:
from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss') 將現在的時間總秒數轉為標准格式時間
from_unixtime(get_json_object(get_json_object(form_data,'$.挽單時間'),'$.$date')/1000) as retain_time
unix_timestamp('2019-08-15 16:40:00','yyyy-MM-dd HH:mm:ss') --1565858400
日期>>>>時間戳:unix_timestamp()
date_format:yyyy-MM-dd HH:mm:ss 時間轉格式化時間
select date_format('2019-10-07 13:24:20', 'yyyyMMdd000000')-- 20191007000000select date_format('2019-10-07', 'yyyyMMdd000000')-- 20191007000000
1.日期比較函數: datediff語法: datediff(string enddate,string startdate)
返回值: int
說明: 返回結束日期減去開始日期的天數。
舉例: hive> select datediff('2016-12-30','2016-12-29'); 1
2.日期增加函數: date_add語法: date_add(string startdate, intdays)
返回值: string
說明: 返回開始日期startdate增加days天後的日期。
舉例: hive>select date_add('2016-12-29',10); 2017-01-08
3.日期減少函數: date_sub語法: date_sub (string startdate,int days)
返回值: string
說明: 返回開始日期startdate減少days天後的日期。
舉例: hive>select date_sub('2016-12-29',10); 2016-12-19
4.查詢近30天的數據
select * from table where datediff(current_timestamp,create_time)<=30;
create_time 為table里的欄位,current_timestamp 返回當前時間 2018-06-01 11:00:00
3、trunc()函數的用法:當前日期的各種第一天,或者對數字進行不四捨五入的截取
日期:
1.select trunc(sysdate) from al --2011-3-18 今天的日期為2011-3-18
2.select trunc(sysdate, 'mm') from al --2011-3-1 返回當月第一天.
上月1號 trunc(add_months(current_date(),-1),'MM')
3.select trunc(sysdate,'yy') from al --2011-1-1 返回當年第一天
4.select trunc(sysdate,'dd') from al --2011-3-18 返回當前年月日
5.select trunc(sysdate,'yyyy') from al --2011-1-1 返回當年第一天
6.select trunc(sysdate,'d') from al --2011-3-13 (星期天)返回當前星期的第一天
7.select trunc(sysdate, 'hh') from al --2011-3-18 14:00:00 當前時間為14:41
8.select trunc(sysdate, 'mi') from al --2011-3-18 14:41:00 TRUNC()函數沒有秒的精確
數字:TRUNC(number,num_digits) Number 需要截尾取整的數字。Num_digits 的默認值為 0。TRUNC()函數截取時不進行四捨五入
11.select trunc(123.458,1) from al --123.4
12.select trunc(123.458,-1) from al --120
4、round():四捨五入:
select round(1.455, 2) #結果是:1.46,即四捨五入到十分位,也就是保留兩位小數
select round(1.5) #默認四捨五入到個位,結果是:2
select round(255, -1) #結果是:260,即四捨五入到十位,此時個位是5會進位
floor():地板數
ceil()天花板數
5、
6.日期轉年函數: year語法: year(string date)
返回值: int
說明: 返回日期中的年。
舉例:
hive> select year('2011-12-08 10:03:01') from al;
2011
hive> select year('2012-12-08') fromal;
2012
7.日期轉月函數: month語法: month (string date)
返回值: int
說明: 返回日期中的月份。
舉例:
hive> select month('2011-12-08 10:03:01') from al;
12
hive> select month('2011-08-08') fromal;
8
8.日期轉天函數: day語法: day (string date)
返回值: int
說明: 返回日期中的天。
舉例:
hive> select day('2011-12-08 10:03:01') from al;
8
hive> select day('2011-12-24') fromal;
24
9.日期轉小時函數: hour語法: hour (string date)
返回值: int
說明: 返回日期中的小時。
舉例:
hive> select hour('2011-12-08 10:03:01') from al;
10
10.日期轉分鍾函數: minute語法: minute (string date)
返回值: int
說明: 返回日期中的分鍾。
舉例:
hive> select minute('2011-12-08 10:03:01') from al;
3
11.日期轉秒函數: second語法: second (string date)
返回值: int
說明: 返回日期中的秒。
舉例:
hive> select second('2011-12-08 10:03:01') from al;
1
12.日期轉周函數: weekofyear語法: weekofyear (string date)
返回值: int
說明: 返回日期在當前的周數。
舉例:
hive> select weekofyear('2011-12-08 10:03:01') from al;
49
查看hive表在hdfs中的位置:show create table 表名;
在hive中hive2hive,hive2hdfs:
HDFS、本地、hive -----> Hive:使用 insert into | overwrite、loaddata local inpath "" into table student;
Hive ----> Hdfs、本地:使用:insert overwrite | local
網站訪問量統計:
uv:每用戶訪問次數
ip:每ip(可能很多人)訪問次數
PV:是指頁面的瀏覽次數
VV:是指你訪問網站的次數
sql:
基本函數:
count、max、min、sum、avg、like、rlike('2%'、'_2%'、%2%'、'[2]')(java正則)
and、or、not、in
where、group by、having、{ join on 、full join} 、order by(desc降序)
sort by需要與distribut by集合結合使用:
hive (default)> set maprece.job.reces=3; //先設置rece的數量
insert overwrite local directory '/opt/mole/datas/distribute-by'
row format delimited fields terminated by '\t'
先按照部門編號分區,再按照員工編號降序排序。
select * from emp distribute by deptno sort by empno desc;
外部表 create external table if not exists dept
分區表:create table dept_partition ( deptno int, dname string, loc string ) partitioned by ( month string )
load data local inpath '/opt/mole/datas/dept.txt' into table default.dept_partition partition(month='201809');
alter table dept_partition add/drop partition(month='201805') ,partition(month='201804');
多分區聯合查詢:union
select * from dept_partition2 where month='201809' and day='10';
show partitions dept_partition;
desc formatted dept_partition;
二級分區表:create table dept_partition2 ( deptno int, dname string, loc string ) partitioned by (month string, day string) row format delimited fields terminated by '\t';
分桶抽樣查詢:分區針對的是數據的存儲路徑;分桶針對的是數據文件
create table stu_buck(id int, name string) clustered by(id) into 4 bucketsrow format delimited fields terminated by '\t';
設置開啟分桶與rece為1:
set hive.enforce.bucketing=true;
set maprece.job.reces=-1;
分桶抽樣:select * from stu_bucktablesample(bucket x out of y on id);
抽取,桶數/y,x是從哪個桶開始抽取,y越大 抽樣數越少,y與抽樣數成反比,x必須小於y
給空欄位賦值:
如果員工的comm為NULL,則用-1代替或用其他欄位代替 :select nvl(comm,-1) from emp;
case when:如何符合記為1,用於統計、分組統計
select dept_id, sum(case sex when '男' then 1 else 0 end) man , sum(case sex when '女' then 1 else 0 end) woman from emp_sex group by dept_id;
用於組合歸類匯總(行轉列):UDAF:多轉一
concat:拼接查詢結果
collect_set(col):去重匯總,產生array類型欄位,類似於distinct
select t.base, concat_ws('|',collect_set(t.name)) from (select concat_ws(',',xingzuo,blood_type) base,name from person_info) t group by t.base;
解釋:先第一次查詢得到一張沒有按照(星座血型)分組的表,然後分組,使用collect_set將名字組合成數組,然後使用concat將數組變成字元串
用於拆分數據:(列轉行):UDTF:一轉多
explode(col):將hive一列中復雜的array或者map結構拆分成多行。
lateral view 側面顯示:用於和UDTF一對多函數搭配使用
用法:lateral view udtf(expression) tablealias as cate
cate:炸開之後的列別名
temptable :臨時表表名
解釋:用於和split, explode等UDTF一起使用,它能夠將一列數據拆成多行數據,在此基礎上可以對拆分後的數據進行聚合。
開窗函數:
Row_Number,Rank,Dense_Rank over:針對統計查詢使用
Row_Number:返回從1開始的序列
Rank:生成分組中的排名序號,會在名詞s中留下空位。3 3 5
dense_rank:生成分組中的排名序號,不會在名詞中留下空位。3 3 4
over:主要是分組排序,搭配窗口函數使用
結果:
SUM、AVG、MIN、MAX、count
preceding:往前
following:往後
current row:當前行
unbounded:unbounded preceding 從前面的起點, unbounded following:到後面的終點
sum:直接使用sum是總的求和,結合over使用可統計至每一行的結果、總的結果、當前行+之前多少行/之後多少行、當前行到往後所有行的求和。
over(rowsbetween 3/current ) 當前行到往後所有行的求和
ntile:分片,結合over使用,可以給數據分片,返回分片號
使用場景:統計出排名前百分之或n分之一的數據。
lead,lag,FIRST_VALUE,LAST_VALUE
lag與lead函數可以返回上下行的數據
lead(col,n,dafault) 用於統計窗口內往下第n行值
第一個參數為列名,第二個參數為往下第n行(可選,默認為1),第三個參數為默認值(當往下第n行為NULL時候,取默認值,如不指定,則為NULL)
LAG(col,n,DEFAULT) 用於統計窗口內往上第n行值
第一個參數為列名,第二個參數為往上第n行(可選,默認為1),第三個參數為默認值(當往上第n行為NULL時候,取默認值,如不指定,則為NULL)
使用場景:通常用於統計某用戶在某個網頁上的停留時間
FIRST_VALUE:取分組內排序後,截止到當前行,第一個值
LAST_VALUE:取分組內排序後,截止到當前行,最後一個值
范圍內求和: https://blog.csdn.net/happyrocking/article/details/105369558
cume_dist,percent_rank
–CUME_DIST :小於等於當前值的 行數 / 分組內總行數
–比如,統計小於等於當前薪水的人數,占總人數的比例
percent_rank:分組內當前行的RANK值-1/分組內總行數-1
總結:
在Spark中使用spark sql與hql一致,也可以直接使用sparkAPI實現。
HiveSql窗口函數主要應用於求TopN,分組排序TopN、TopN求和,前多少名前百分之幾。
與Flink窗口函數不同。
Flink中的窗口是用於將無線數據流切分為有限塊處理的手段。
window分類:
CountWindow:按照指定的數據條數生成一個 Window,與時間無關。
TimeWindow:按照時間生成 Window。
1. 滾動窗口(Tumbling Windows):時間對齊,窗口長度固定,不重疊::常用於時間段內的聚合計算
2.滑動窗口(Sliding Windows):時間對齊,窗口長度固定,可以有重疊::適用於一段時間內的統計(某介面最近 5min 的失敗率來報警)
3. 會話窗口(Session Windows)無時間對齊,無長度,不重疊::設置session間隔,超過時間間隔則窗口關閉。
㈥ hive怎麼將字元型轉為數值型
hive字元串函數
1. 字元串長度函數:length
語法: length(string A)
返回值: int
說明:返回字元串A的長度
舉例:
hive> select length('abcedfg') from lxw_al;
7
2. 字元串反轉函數:reverse
語法: reverse(string A)
返回值: string
說明:返回字元串A的反轉結果
舉例:
hive> select reverse(abcedfg') from lxw_al;
gfdecba
3. 字元串連接函數:concat
語法: concat(string A, string B…)
返回值: string
說明:返回輸入字元串連接後的結果,支持任意個輸入字元串
舉例:
hive> select concat('abc','def','gh') from lxw_al;
abcdefgh
4. 帶分隔符字元串連接函數:concat_ws
語法: concat_ws(string SEP, string A, string B…)
返回值: string
說明:返回輸入字元串連接後的結果,SEP表示各個字元串間的分隔符
舉例:
hive> select concat_ws(',','abc','def','gh') from lxw_al;
abc,def,gh
5. 字元串截取函數:substr,substring
語法: substr(string A, int start),substring(string A, int start)
返回值: string
說明:返回字元串A從start位置到結尾的字元串
舉例:
hive> select substr('abcde',3) from lxw_al;
cde
hive> select substring('abcde',3) from lxw_al;
cde
hive> selectsubstr('abcde',-1) from lxw_al; (和ORACLE相同)
e
6. 字元串截取函數:substr,substring
語法: substr(string A, int start, int len),substring(string A, intstart, int len)
返回值: string
說明:返回字元串A從start位置開始,長度為len的字元串
舉例:
hive> select substr('abcde',3,2) from lxw_al;
cd
hive> select substring('abcde',3,2) from lxw_al;
cd
hive>select substring('abcde',-2,2) from lxw_al;
de
7. 字元串轉大寫函數:upper,ucase
語法: upper(string A) ucase(string A)
返回值: string
說明:返回字元串A的大寫格式
舉例:
hive> select upper('abSEd') from lxw_al;
ABSED
hive> select ucase('abSEd') from lxw_al;
ABSED
8. 字元串轉小寫函數:lower,lcase
語法: lower(string A) lcase(string A)
返回值: string
說明:返回字元串A的小寫格式
舉例:
hive> select lower('abSEd') from lxw_al;
absed
hive> select lcase('abSEd') from lxw_al;
absed
9. 去空格函數:trim
語法: trim(string A)
返回值: string
說明:去除字元串兩邊的空格
舉例:
hive> select trim(' abc ') from lxw_al;
abc
10. 左邊去空格函數:ltrim
語法: ltrim(string A)
返回值: string
說明:去除字元串左邊的空格
舉例:
hive> select ltrim(' abc ') from lxw_al;
abc
11. 右邊去空格函數:rtrim
語法: rtrim(string A)
返回值: string
說明:去除字元串右邊的空格
舉例:
hive> select rtrim(' abc ') from lxw_al;
abc
12. 正則表達式替換函數:regexp_replace
語法: regexp_replace(string A, string B, string C)
返回值: string
說明:將字元串A中的符合java正則表達式B的部分替換為C。注意,在有些情況下要使用轉義字元,類似oracle中的regexp_replace函數。
舉例:
hive> select regexp_replace('foobar', 'oo|ar', '') from lxw_al;
fb
13. 正則表達式解析函數:regexp_extract
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字元串subject按照pattern正則表達式的規則拆分,返回index指定的字元。
舉例:
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) fromlxw_al;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) fromlxw_al;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) fromlxw_al;
foothebar
注意,在有些情況下要使用轉義字元,下面的等號要用雙豎線轉義,這是java正則表達式的規則。
select data_field,
regexp_extract(data_field,'.*?bgStart\\=([^&]+)',1) as aaa,
regexp_extract(data_field,'.*?contentLoaded_headStart\\=([^&]+)',1) as bbb,
regexp_extract(data_field,'.*?AppLoad2Req\\=([^&]+)',1) as ccc
from pt_nginx_loginlog_st
where pt = '2012-03-26'limit 2;
14. URL解析函數:parse_url
語法: parse_url(string urlString, string partToExtract [, stringkeyToExtract])
返回值: string
說明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
舉例:
hive> selectparse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') fromlxw_al;
facebook.com
hive> selectparse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY','k1') from lxw_al;
v1
15. json解析函數:get_json_object
語法: get_json_object(string json_string, string path)
返回值: string
說明:解析json的字元串json_string,返回path指定的內容。如果輸入的json字元串無效,那麼返回NULL。
舉例:
hive> select get_json_object('{"store":
> {"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
> "bicycle":{"price":19.95,"color":"red"}
> },
> "email":"amy@only_for_json_udf_test.net",
> "owner":"amy"
> }
> ','$.owner') from lxw_al;
amy
16. 空格字元串函數:space
語法: space(int n)
返回值: string
說明:返回長度為n的字元串
舉例:
hive> select space(10) from lxw_al;
hive> select length(space(10)) from lxw_al;
10
17. 重復字元串函數:repeat
語法: repeat(string str, int n)
返回值: string
說明:返回重復n次後的str字元串
舉例:
hive> select repeat('abc',5) from lxw_al;
abcabcabcabcabc
18. 首字元ascii函數:ascii
語法: ascii(string str)
返回值: int
說明:返回字元串str第一個字元的ascii碼
舉例:
hive> select ascii('abcde') from lxw_al;
97
19. 左補足函數:lpad
語法: lpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行左補足到len位
舉例:
hive> select lpad('abc',10,'td') from lxw_al;
tdtdtdtabc
注意:與GP,ORACLE不同,pad 不能默認
20. 右補足函數:rpad
語法: rpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行右補足到len位
舉例:
hive> select rpad('abc',10,'td') from lxw_al;
abctdtdtdt
21. 分割字元串函數: split
語法: split(string str, stringpat)
返回值: array
說明: 按照pat字元串分割str,會返回分割後的字元串數組
舉例:
hive> select split('abtcdtef','t') from lxw_al;
["ab","cd","ef"]
22. 集合查找函數:find_in_set
語法: find_in_set(string str, string strList)
返回值: int
說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字元串。如果沒有找該str字元,則返回0
舉例:
hive> select find_in_set('ab','ef,ab,de') from lxw_al;
2
hive> select find_in_set('at','ef,ab,de') from lxw_al;
0
㈦ hive中的字元串提取
在進行數據分析時,尤其要對網頁進行分析時,我們往往要對其中部分的數據進行抽取,這個就需要靠hive的函數來完成了。
首先要講的是split函數,這個函數的作用是對字元竄進行分割,基本用法為:split(string str, string pat) ,返回值為一個數組array,因此要取值得話需要用到切片,即[數字],其中第一個str是要切分的字元串,第二個pat是以什麼字元進行切割。來看案例吧。
有的時候切割不是一下就能完成,那麼就多去嵌套幾次split就好了。
返回字元串從某個位置開始固定長度的子串,和substring功能相同,基本用法為substr(string A, int start, int len ),還是來看例子。值得注意的是,substr(str,0,2)和substr(str,1,2)的功能都是一樣的,都是從第一個位置開始。
這個函數是個神器,可以解析url結構,返回我們想要的東西。基本用法為parse_url(string urlString, string partToExtract [, string keyToExtract]),其中partToExtract的有效值包括HOST,PATH, QUERY, REF, PROTOCOL, AUTHORITY,FILE和USERINFO等,具體我就不一一解釋了,大家使用時可以自行網路。重點說一下,當第二個參數是QUERY時,第三個參數就可以使用了,這個是參數提取最有用的方法了,還是以案例來說明。
這個函數是最終的大殺器了,以上都解決不了你的問題的時候,有了這個一切都可以解決,使用這個函數的基礎是正則表達式基礎要會一些。這個函數的基本用法是regexp_extract(string subject, string pattern, int index),第一個參數是待處理的字元串,第二個參數是寫好的正則,第三個表達式一般用不上可以忽略掉。來看例子:
有了以上函數,相信應該能滿足大家對於hive進行字元串提取的一切要求了。
㈧ hive sql bigint類型為空能用不等於空字元串嗎
hive sql bigint類型為空能用不等於空字元串。
int為數字類型,這種欄位會有個默認值,就是0,有很多人會用int欄位來做對比,那麼這個欄位的值就會有0和1,而在你的語句中,0就是空,但不是null,所以就會這樣了。
用replace函數替換 Replace() 功能將一個字元串中指定個數的字元串替換為另一個字元串。 語法Replace(string1,start,n,string2) 參數string1:string類型,指定要使用string2替換其中一部分內容的字元串start。
表示範圍:
C語言沒有規定各種整數類型的表示範圍,也就是說,沒有規定各種整數的二進制編碼長度,對於int和long,只規定了long類型的表示範圍不小於int,但也允許它們的表示範圍相同。具體C語言會對整型和長整型規定表示方式和表示範圍。
使用技巧:標准函數INT(X)其基本功能是得到一個不大於X的最大整數,如INT(3.59)=3,INT(-2.01)=-3。INT函數是一個用途很廣的函數,在教學中能有目的的分列其使用技巧。
㈨ Hive中常用的字元串操作
創建虛表:
語法: length(string A)
返回值: int
說明:返回字元串A的長度
語法: reverse(string A)
返回值: string
說明:返回字元串A的反轉結果
語法: concat(string A, string B…)
返回值: string
說明:返回輸入字元串連接後的結果,支持任意個輸入字元串
語法: concat_ws(string SEP, string A, string B…)
返回值: string
說明:返回輸入字元串連接後的結果,SEP表示各個字元串間的分隔符
語法: substr(string A, int start),substring(string A, int start)
返回值: string
說明:返回字元串A從start位置到結尾的字元串
語法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
說明:返回字元串A從start位置開始,長度為len的字元串
語法: upper(string A) ucase(string A)
返回值: string
說明:返回字元串A的大寫格式
語法: lower(string A) lcase(string A)
返回值: string
說明:返回字元串A的小寫格式
語法: trim(string A)
返回值: string
說明:去除字元串兩邊的空格
語法: ltrim(string A)
返回值: string
說明:去除字元串左邊的空格
語法: rtrim(string A)
返回值: string
說明:去除字元串右邊的空格
語法: regexp_replace(string A, string B, string C)
返回值: string
說明:將字元串A中的符合java正則表達式B的部分替換為C。注意,在有些情況下要使用轉義字元,類似oracle中的regexp_replace函數。
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字元串subject按照pattern正則表達式的規則拆分,返回index指定的字元。
第三個參數:
0 是顯示與之匹配的整個字元串
1 是顯示第一個括弧裡面的
2 是顯示第二個括弧裡面的欄位
語法: parse_url(string urlString, string partToExtract [, string keyToExtract])
返回值: string
說明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
語法: get_json_object(string json_string, string path)
返回值: string
說明:解析json的字元串json_string,返回path指定的內容。如果輸入的json字元串無效,那麼返回NULL。
語法: space(int n)
返回值: string
說明:返回長度為n的空字元串
語法: repeat(string str, int n)
返回值: string
說明:返回重復n次後的str字元串
語法: ascii(string str)
返回值: int
說明:返回字元串str第一個字元的ascii碼
語法: lpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行左補足到len位
語法: rpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行右補足到len位
語法: split(string str, string pat)
返回值: array
說明: 按照pat字元串分割str,會返回分割後的字元串數組
語法: find_in_set(string str, string strList)
返回值: int
說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字元串。如果沒有找該str字元,則返回0
返回:int。substr在str中第一次出現的位置,若任何參數為null返回null,若substr不在str中返回0,Str中第一個字元的位置為1
說明:C1 被搜索的字元串
C2 希望搜索的字元串
I 搜索的開始位置,默認為1
J 出現的位置,默認為1
24、使用兩個分隔符將文本拆分為鍵值對:str_to_map(text[, delimiter1, delimiter2])
返回:map
Delimiter1將文本分成K-V對,Delimiter2分割每個K-V對。對於delimiter1默認分隔符是',',對於delimiter2默認分隔符是'='
25、unix_timestamp() 返回當前時間戳。另外,current_timestamp() 也有同樣作用。
unix_timestamp(string date) 返回 date 對應的時間戳,date 格式必須為 yyyy-MM-dd HH:mm:ss。
unix_timestamp(string date, string format) 返回 date 對應的時間戳,date 格式由 format 指定。
26、from_unixtime(int/bigint timestamp) 返回 timestamp 時間戳對應的日期,格式為 yyyy-MM-dd HH:mm:ss。
from_unixtime(int/bigint timestamp, string format) 返回 timestamp 時間戳對應的日期,格式由 format 指定。