hive常用sql有哪些

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

成都创新互联公司是一家专业提供古田企业网站建设,专注与网站建设、网站设计、H5开发、小程序制作等业务。10年已为古田众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

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有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

网站标题:hive常用sql有哪些
当前URL:https://www.cdcxhl.com/article44/gighee.html

成都网站建设公司_创新互联,为您提供服务器托管网站维护企业网站制作云服务器关键词优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管