Hive-hivesql粗略操作

hivesql

1 建表

create  table if not exists stu2(id int ,name string) 
row format delimited fields terminated by '\t' 
stored as textfile 
location '/user/stu2';

row format delimited fields terminated by ‘\t’ 指定字段分隔符,默认分隔符为 ‘\001’
stored as 指定存储格式
location 指定存储位置

2 DQL

2.1 聚合函数

  1. 非空集合总体变量函数: var_pop
  2. 非空集合样本变量函数: var_samp
  3. 总体标准偏离函数: stddev_pop
  4. 中位数函数: percentile

2.2 关系运算

  1. is null
  2. is null
  3. like:”_”表示任意单个字符,而字符”%”表示任意数量的字符
  4. REGEXP正则操作

2.3 条件运算

1.if(boolean testCondition, T valueTrue, T valueFalseOrNull)

2.case when then:

语法: case when a then b [when c then d]* [else e] end
语法: case a when b then c [when d then e]* [else f] end

2.4 日期函数

1.获取当前UNIX时间戳函数: unix_timestamp

hive> select unix_timestamp() from tableName;
1616906976


hive>  select unix_timestamp('2021-03-08 14:21:15') from tableName;
1615184475

hive>  select unix_timestamp('2021-03-08 14:21:15','yyyyMMdd HH:mm:ss') from tableName;
1615184475
  1. UNIX时间戳转日期函数: from_unixtime
hive> select from_unixtime(1616906976,'yyyyMMdd') from tableName;
20210328
  1. 日期时间转日期函数: to_date

说明: 返回日期时间字段中的日期部分。

hive> select to_date('2021-03-28 14:03:01') from tableName;
2021-03-28
  1. 日期转周函数: weekofyear
hive> select weekofyear('2020-12-08 10:03:01') from tableName;
49
  1. 日期比较函数: datediff

返回的是相差的天数

hive> select datediff('2020-12-08','2012-05-09') from tableName;
213
  1. 日期增加/减少函数: date_add/date_sub

返回的是开始日期增加天数后的时间

hive> select date_add('2020-12-08',10) from tableName;
2020-12-18

hive> select date_sub('2020-12-08',10) from tableName;
2020-11-28

2.5 字符串函数

2.5.1 基本类型字符串

  1. 长度函数:length(string A)
hive> select length('abcedfg') from tableName;
7
  1. 字符串反转函数:reverse(string A)
hive> select reverse('abcedfg') from tableName;
gfdecba
  1. 字符串连接函数:concat(string A, string B…)/concat_ws(string SEP, string A, string B…)
hive> select concat_ws(',','abc','def','gh')from tableName;
abc,def,gh
  1. 字符串截取substr(string A, int start),substring(string A, int start)/ substr(string A, int start, int len),substring(string A, int start, int len)
hive> select substr('abcde',3) from tableName;
cde
hive> select substring('abcde',3) from tableName;
cde

hive> select substr('abcde',3,2) from tableName;
cd
hive> select substring('abcde',3,2) from tableName;
cd
  1. 正则表达式替换函数regexp_replace(string A, string B, string C)

将字符串A中的符合java正则表达式B的部分替换为C

hive> select regexp_replace('foobar', 'oo|ar', '') from tableName;
fb

regexp_extract(string subject, string pattern, int index)

将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符。

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from tableName;
the
  1. URL解析函数:parse_url(string urlString, string partToExtract [, string keyToExtract])

说明:返回URL中指定的部分。partToExtract的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO

hive> select parse_url
('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') 
from tableName;
www.tableName.com 
  1. json解析函数:get_json_object(string json_string, string path)
hive> select  get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},"owner":"amy"}','$.owner') from tableName;
  1. 重复字符串函数:repeat(string str, int n)

​ 说明:返回重复n次后的str字符串

hive> select repeat('abc',5) from tableName;
abcabcabcabcabc
  1. 首字符ascii函数:ascii/

  2. 分割字符串函数: split

语法: split(string str, string pat)

hive> select split('abtcdtef','t') from tableName;
["ab","cd","ef"]
  1. 集合查找函数: find_in_set

2.5.2 复合类型字符串

  1. Map构建:map (key1, value1, key2, value2, …)

    struct构建:struct(val1, val2, val3, …)

    array构建:array(val1, val2, …)

hive> create table arr_table2 as select array("tom","mary","tim") as t
 from tableName;
hive> select t[0],t[1] from arr_table2;
tom     mary    tim


hive> Create table map_table2 as select map('100','tom','200','mary') as t from tableName;
hive> select t['200'],t['100'] from map_table2;
mary    tom

hive> create table str_table2 as select struct('tom','mary','tim') as t from tableName;
hive> describe tableName;
t       struct<col1:string ,col2:string,col3:string>
hive> select t.col1,t.col3 from str_table2;
mary    tom
  1. 长度统计length:size(Map);size(Array);cast(expr as )

2.6 复杂函数

  1. lateral view

lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据

你可能感兴趣的:(hive,hadoop,hivesql)