Hive SQL函数 |
您所在的位置:网站首页 › vb取余函数 › Hive SQL函数 |
Hive SQL函数
一、关系运算 1.等值比较: =
2.不等值比较: 3.小于比较: =7.空值判断: IS NULL 8.非空判断: IS NOTNULL 举例:统计表中字段 label 非空数量: select count(*) from wsc_recommend_pos_neg_for_ctr_new where label is not null; 9.LIKE 比较: LIKE (注意:否定比较时候用 NOT ALIKE B) 举例:统计表中字段 label 为’expo’开头的数量: select count(*) from wsc_recommend_pos_neg_for_ctr_new where label like ‘expo%’; JAVA 的 LIKE 操作: RLIKEA RLIKE B,如果字符串 A 符合 JAVA 正则表达式 B 的正则语法,则为 TRUE;否则为 FALSE。 二、数学运算加法操作: + 减法操作: - 乘法操作: * 除法操作: / 取余操作: % 位与操作: & 位或操作: | 位异或操作: ^ 位取反操作: ~ 三、逻辑运算逻辑与操作: AND 逻辑或操作: OR 逻辑非操作: NOT 四、数值计算取整函数: round (遵循四舍五入) 指定精度取整函数: round(double a, int d) 向下取整函数: floor 向上取整函数: ceil 取随机数函数: rand 返回一个 0 到 1 范围内的随机数 自然指数函数: exp 以 10 为底对数函数: log10(double a) 以 2 为底对数函数: log2(double a) 对数函数: log(double base, double a) 幂运算函数: pow(double a, double p) 开平方函数: sqrt(double a) 二进制函数: bin(BIGINT a) 十六进制函数: hex(BIGINT a) 反转十六进制函数: unhex(string a) 进制转换函数: conv(BIGINT num, int from_base, int to_base) 绝对值函数: abs 正取余函数: pmod 正弦函数: sin 反正弦函数: asin 余弦函数: cos 反余弦函数: acos positive 函数: positive negative 函数: negative 五、日期函数UNIX 时间戳转日期函数:from_unixtime from_unixtime(1323308943,‘yyyyMMdd’) 获取当前 UNIX 时间戳函数:unix_timestamp 日期转 UNIX 时间戳函数:unix_timestamp unix_timestamp(‘2011-12-07 13:01:03’) 日期时间转日期函数:to_date 返回日期时间字段中的日期部分 日期转年函数: year 返回日期中的年 日期转月函数: month 返回日期中的月份 日期转天函数: day 返回日期中的天 日期转小时函数: hour 日期转分钟函数: minute 日期转秒函数: second 日期转周函数:weekofyear 日期比较函数: datediff datediff(‘2012-12-08’,‘2012-05-09’) 日期增加函数: date_add date_add(‘2012-12-08’,10) 日期减少函数: date_sub date_sub(‘2012-12-08’,10) 六、条件函数 If 函数: ifselect if(label=‘click’,get_json_object(goodsid, ‘$.[0]’), goodsid) as goodsid from data_model.wsc_recommend_pos_neg_for_ctr_new limit 10; 非空查找函数: COALESCE(T v1, T v2,…)返回参数中的第一个非空值;如果所有值都为 NULL,那么返回 NULL 条件判断函数:CASECASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END 如果 a 等于 b,那么返回 c;如果 a 等于 d,那么返回 e;否则返回 f select case label when ‘click’ then get_json_object(goodsid, ‘$.[0]’) when ‘expose’ then goodsid else ‘null’ end as goodsid from data_model.wsc_recommend_pos_neg_for_ctr_new limit 10; CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END 如果 a 等于 b,那么返回 c;如果 a 等于 d,那么返回 e;否则返回 f select case when label=‘click’ then get_json_object(goodsid, ‘$.[0]’) when label=‘expose’ then goodsid else ‘null’ end as goodsid from data_model.wsc_recommend_pos_neg_for_ctr_new limit 10; 七、字符串函数字符串长度函数:length(string A) 字符串反转函数:reverse(string A) 字符串连接函数:concat(string A, string B…) 带分隔符字符串连接函数:concat_ws(string SEP, string A, string B…) 字符串截取函数:substr(string A, int start, int len),substring(string A, intstart, int len) 字符串转大写函数:upper(string A) ucase(string A) 字符串转小写函数:lower(string A) lcase(string A) 去空格函数:trim 去除字符串两边的空格 左边去空格函数:ltrim 右边去空格函数:rtrim 正则表达式替换函数:regexp_replace 将字符串 A 中的符合 java 正则表达式 B 的部分替换为 C regexp_replace(‘foobar’, ‘oo|ar’, ‘’) 正则表达式解析函数:regexp_extract(string subject, string pattern, int index)将字符串 subject 按照 pattern 正则表达式的规则拆分,返回 index 指定的字符。 regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 1) URL 解析函数:parse_url(string urlString, string partToExtract [, stringkeyToExtract])返回 URL 中指定的部分。partToExtract 的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO. json 解析函数:get_json_object()get_json_object(recgslt,‘$.[0:].id’)as goodsid 空格字符串函数:space(int n) 重复字符串函数:repeat(string str, int n) 首字符 ascii 函数:ascii(string str) 左补足函数:lpad(string str, int len, string pad) 将 str 进行用 pad 进行左补足到 len 位 右补足函数:rpad(string str, int len, string pad) 分割字符串函数: split(string str, stringpat) 返回 array 集合查找函数:find_in_set(string str, string strList) find_in_set(‘ab’,‘ef,ab,de’) 八、集合统计函数 个数统计函数: count(*), count(expr), count(DISTINCT expr[, expr_.])总和统计函数: sum(col), sum(DISTINCT col) 平均值统计函数: avg(col), avg(DISTINCT col) 最小值统计函数: min(col) 最大值统计函数: max(col) 非空集合总体变量函数:var_pop(col) 方差 非空集合样本变量函数:var_samp(col) (SUM(expr*expr)-SUM(expr)*SUM(expr)/COUNT(expr))/(COUNT(expr)-1) 总体标准偏离函数:stddev_pop(col) 标准差 样本标准偏离函数:stddev_samp(col) var_samp(col)开方 10.中位数函数:percentile(BIGINT col, p) percentile(BIGINT col, array(p1 [, p2]…)) 近似中位数函数:percentile_approx(DOUBLE col, p [, B])percentile_approx(DOUBLE col, array(p1 [, p2]…) [, B]) 直方图:histogram_numeric(col, b) 九、复合类型构建操作Map 类型构建: map (key1, value1, key2, value2,…) Struct 类型构建: struct(val1, val2, val3,…) array 类型构建: array(val1, val2,…) select m[‘0’] from (select map(recommendType, goodsid)as m from data_model.wsc_recommend_pos_neg_for_ctr_new limit 10) as t; 十、复杂类型访问操作array 类型访问: A[n] map 类型访问: M[key] struct 类型访问: S.x 返回结构体 S 中的 x 字段 十一、复杂类型长度统计函数1.Map 类型长度函数: size(Map) 2.array 类型长度函数: size(Array) 举例:统计 goodsid 中元素个数 select size(split(goodsid, ‘,’)) from wsc_recommend_pos_neg_for_ctr_new limit 10; 3.类型转换函数 select case(recommendType as bigint) from wsc_recommend_pos_neg_for_ctr_new limit 10; 关注林哥,持续更新哦!!!★,°:.☆( ̄▽ ̄)/$:.°★ 。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |