基本查询操作
select [all | distinct] select 列名1,列名2,,,,列名n
from 表名
where 条件
group by 列名 [having 条件]
cluster by 列名 (排序)
distribute by 列名 (排序)sort by 列名 order by 列名
Limit 数量
mysql转换到hive sql应该注意的点:
1.hive不支持 where 中的等值连接 比如不支持 where 表名1.列名1=表名2.列名2
连接可以用on 表名1.列名1=表名2.列名2
2.分号字符(识别不够智能)
在hiveSQL中,concat(";",key) from 表1;
他对应了第一个;结束的话,就无法把后面的;给对应起来。
所以解决方法是用 ; 的八进制ASCII码进行转义 ; 改为 \073
3.判断null,在hive中空的String类型字符串会被判断为非空
4.hive不支持数据插入,若要改变数据,需要重写整张表(毫无疑问,要尽量减少这样的操作)
5.hive的嵌套查询必须给表起别名
6.当有两个分区时,在使用where语句查询的时候,必须将两个分区都写出来
时间处理函数
思路:时间戳和日期相互转换,最好归向一边,比如所有的时间戳通通转化为日期。然后使用日期处理函数。如果转换为时间戳则单纯比较大小
时间戳转化为日期
例子:
select distinct from_unixtime(1441565203,'yyyy/MM/dd HH:mm:ss') from test_date;
2015/09/07 02:46:43
日期时间>>>>>>时间戳 默认格式为“yyyy-MM-dd HH:mm:ss“
例子:
select distinct unix_timestamp('2015-09-07 02:46:43') from test_date;
1441565203
注:当括号中不指定日期时,会以当前的时间日期为参数产生结果
指定日期时间>>>>>>时间戳:
select distinct unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from test_date;
132323406
日期时间>>>>>>日期:
select distinct to_date('2011-12-08 10:03:01') from test_date;
结果:2011-12-08
日期时间>>>>>>年/月/日/时/分/秒: year(string date),month(),day(),hour(),minute(),second()。
select distinct year('2011-12-08 10:03:01') from test_date;
2011
select distinct second('2011-12-08 10:03:01') from test_date;
当前日期时间获取
current_timestamp
from_unixtime(unix_timestamp())
当前日期获取
current_date
有关日期的函数
1.日期差值:datediff(结束日期,开始日期),返回值为结束日期减去开始日期的天数
2.日期加减:date_add(日期,增加的天数),返回增加天数后的日期
sub 减少 减少
字符串处理函数
instr(字符串A,字符串B) 返回类型:int
返回B在 A中的位置,存在则返回下标(1开始),没有则返回0
length(string A) 返回类型:int
返回A的长度
reverse(string A) 返回类型:string
返回把A字符串反转后的字符串结果
concat(string A,string B,,,,,string N) 返回类型:string
返回把A,B,,,,N字符串连接后的字符串结果
(也有带分隔符的concat函数,具体使用时可以查查语法)
substr(string A,int start) / substring(string A,int start) 返回类型:string
返回字符串A从start个字符开始到结尾的字符串结果
substr(string A,int start,int len) / substring(string A,int start,int len) 返回类型:string
返回字符串A从start个字符开始截取len个字符的字符串结果
upper(string A) / ucase(string A) 返回类型:string
返回字符串A的大写字符串
lower(string A) / lcase(string A) 返回类型:string
返回字符串A的小写字符串
trim(string A) 返回类型:string
去除字符串A两边的空格
ltrim(string A) 返回类型:string
去除字符串A左边的空格
rtrim(string A) 返回类型:string
去除字符串A右边的空格
space(int n) 返回类型:string
返回长度为n的空格字符串
repeat(string A,int n) 返回类型:string
返回n次重复A后的字符串结果
ascii(string A) 返回类型: int
返回字符串A的首字符的ascii码
lpad(string A,int n,string pad) 返回类型:string
返回pad左边不停重复A字符串补足,直到长度达到n的字符串
rpad(string A,int n,string pad) 返回类型:string
返回pad右边不停重复A字符串补足,直到长度达到n的字符串
split(string A , string pat) 返回类型:array
在字符串A中寻找pat子串,去除pat子串后分割A,返回数组
find_in_set(string A,string strList) 返回类型:int
说明:返回A在strList第一次出现的位置。如果没有找到,则返回0。轻重strList是逗号分隔的字符串