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 指定存储位置
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
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
hive> select from_unixtime(1616906976,'yyyyMMdd') from tableName;
20210328
说明: 返回日期时间字段中的日期部分。
hive> select to_date('2021-03-28 14:03:01') from tableName;
2021-03-28
hive> select weekofyear('2020-12-08 10:03:01') from tableName;
49
返回的是相差的天数
hive> select datediff('2020-12-08','2012-05-09') from tableName;
213
返回的是开始日期增加天数后的时间
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
hive> select length('abcedfg') from tableName;
7
hive> select reverse('abcedfg') from tableName;
gfdecba
hive> select concat_ws(',','abc','def','gh')from tableName;
abc,def,gh
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
将字符串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
说明:返回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
hive> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},"owner":"amy"}','$.owner') from tableName;
说明:返回重复n次后的str字符串
hive> select repeat('abc',5) from tableName;
abcabcabcabcabc
首字符ascii函数:ascii/
分割字符串函数: split
语法: split(string str, string pat)
hive> select split('abtcdtef','t') from tableName;
["ab","cd","ef"]
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
lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据