2017年3月21日星期二

hive

not in
取出在2014.4月第1周出现,但没有在2014.3月出现过的usr相关数据
select t1.time, t1.usr, t1.data
from some_table t1
left outer join
(select distinct usr from some_table where
time>='2014-03-01 00:00:00' and time<'2014-04-01 00:00:00')
t2
on t1.usr=t2.usr
where
t1.time >='2014-04-01 00:00:00' and t1.mr_date<'2014-04-08 00:00:00'
and t2.usr is null
order by 和 sort by 的差别
见:programming hive chapter 6
order by 跟传统 sql 一样,最终数据只有一个reducer且全排序
sort by 可以有多个reducer,最终数据只在单个reducer内部排序
数据量较大时,用sort by可能比order by快很多
distribute by
见:programming hive chapter 6
hive> SELECT s.ymd, s.symbol, s.price_close
    > FROM stocks s
    > DISTRIBUTE BY s.symbol
    > SORT BY  s.symbol ASC, s.ymd ASC;
distribute by 将指定条件取值相同的原始数据划分到相同的reducer里面,与group by有些类似,不过group by是针对计算结果而言
cluster by
见: programming hive chapter 6
hive> SELECT s.ymd, s.symbol, s.price_close
    > FROM stocks s
    > CLUSTER BY s.symbol;
cluster by s.symbol 相归于 "distribute by s.symbol,且sort by 所有column升序"
用 cast 转换数据类型
见:programming hive chapter 6
原始数据读入时为string,计算时转换为数值 float
hive > SELECT name, salary FROM employees
WHERE cast(salary AS FLOAT) < 100000.0;
group_concat 聚合多个值到一行
SELECT name, GROUP_CONCAT(friendname SEPARATOR ',')

没有评论:

发表评论