1:CAST (expression AS data_type)
CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型。CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型。可以转换的类型是有限制的。
这个类型可以是以下值其中的一个:二进制,同带binary前缀的效果 : BINARY
字符型,可带参数 : CHAR()
日期 : DATE
时间: TIME
日期时间型 : DATETIME
浮点数 : DECIMAL
整数 : SIGNED
无符号整数 : UNSIGNED
2:binary
Mysql默认查询是不分大小写的,可以在SQL语句中加入 binary来区分大小写;BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写
例如 :select * from sys_user where binary loginno = `qqq`
3:explode
explode就是将sql或者hive一行中复杂的array或者map结构拆分成多行。lateral view(侧视图)的意义是配合explode(或者其他的UDTF),一个语句生成把单行数据拆解成多行后的数据结果集。形成的虚拟表tb1 相当于再和原表联结 形成了新表interests1直接使用explode 一次只能生成一个字段 要生成多列就是使用lateral view(侧视图)
drop table explode_lateral_view;
create table explode_lateral_view
(`area` string,
`goods_id` string,
`sale_info` string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS textfile;
导入数据:
INSERT INTO management_cc.explode_lateral_view VALUES('a:shandong,b:beijing,c:hebei','1,2,3,4,5,6,7,8,9','{"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jd","monthSales":2090,"userCount":78981,"score":"9.8"},{"source":"jdmart","monthSales":6987,"userCount":1600,"score":"9.0"}]');
表内数据如下
select explode(split(goods_id,',')) as goods_id from explode_lateral_view;
select explode(split(area,',')) as area from explode_lateral_view;
select explode(split(regexp_replace(regexp_replace(sale_info,'\\[\\{',''),'}]',''),'},\\{')) as sale_info from explode_lateral_view;
然后我们想用get_json_object来获取key为monthSales的数据:
select get_json_object(explode(split(regexp_replace(regexp_replace(sale_info,'\\[\\{',''),'}]',''),'},\\{')),'$.monthSales') as sale_info from explode_lateral_view;
然后挂了FAILED: SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions
UDTF explode不能写在别的函数内
如果你这么写,想查两个字段,select explode(split(area,',')) as area,good_id from explode_lateral_view;
会报错FAILED: SemanticException 1:40 Only a single expression in the SELECT clause is supported with UDTF's. Error encountered near token 'good_id'
使用UDTF的时候,只支持一个字段,这时候就需要LATERAL VIEW出场了
select goods_id2,sale_info from explode_lateral_view LATERAL VIEW explode(split(goods_id,','))goods as goods_id2;
select goods_id2,sale_info,area2
from explode_lateral_view
LATERAL VIEW explode(split(goods_id,','))goods as goods_id2
LATERAL VIEW explode(split(area,','))area as area2;
select
get_json_object(concat('{',sale_info_r,'}'),'$.source') as source,
get_json_object(concat('{',sale_info_r,'}'),'$.monthSales') as monthSales,
get_json_object(concat('{',sale_info_r,'}'),'$.userCount') as userCount,
get_json_object(concat('{',sale_info_r,'}'),'$.score') as score
from explode_lateral_view
LATERAL VIEW explode(split(
regexp_replace(
regexp_replace(sale_info,'\\{',''),
'}]',
''
),'},'))sale_info as sale_info_r;
网友评论