温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

hive常用sql有哪些

发布时间:2021-07-15 09:38:27 来源:亿速云 阅读:140 作者:chen 栏目:大数据

本篇内容介绍了“hive常用sql有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.hive分组去重函数使用。

select  *,row_num() over(partition by id order by modifytime desc) rn from lyjtest  where rn=1;
 row_num() over函数对id做分区根据修改时间做降序,然后筛选出时间最新的一条(rn=1)的数据,达到去重的效果。

2.hive 写入数据
insert into table table2 select * from table1; --查询table1中的数据写入table2;
insert overwrite table table2 select * from table1;--覆盖写入

3.where和having的区别

//where是先限定性条件再分组(对原始数据过滤,where不能过滤聚合函数)
hive> select count(*),age from table1 where id>18 group by age;

//having是先分组在限定条件(对每个组进行过滤,having后只能跟select中已有的列)
hive> select age,count(*) c from table1 group by age having c>2;

//where和having一起使用
select id,count(*) from table1 where id>18 group by id having count(*)>2;

4.hive只支持union all,不支持union
  union all 不去重
  select name,age from table1 where id<80
  union all
  select name,age from table2 where age>18;

5.查询前五条数据
select * from table1 order by age desc limit 5; --查询年龄最大的五条数据
select * from student limit 5;--随机查询五条数据

6.五种子句的严格顺序

where → group by → having → order by → limit

7.distinct

//distinct关键字返回唯一不同的值(返回age和id均不相同的记录)
hive> select distinct age,id from test;

8.复制表
create table test1_temp  like test1; --只复制表不包含数据
create  table test1 as select * from test2; --复制表复制数据到新表9.创建表

9.创建表

CREATE TABLE `lyjtest1`(     `id` double,   `name` string, `sex` string) 
COMMENT 'create table from sql'
ROW FORMAT SERDE  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'  
WITH SERDEPROPERTIES (  'field.delim'='\t',  'line.delim'='\n', 'serialization.format'='\t')   
STORED AS INPUTFORMAT   'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT   'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION   'hdfs://ambari1:8020/warehouse/tablespace/managed/hive/ods_lyjtest.db/lyjtest1' ;

“hive常用sql有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI