HIVE(2)-基础用法

   hive是一个应用性很强的工具,没有高深的东西,就是熟练与活用。而越是活用的技能,个人觉得基础越要清晰与精准,于是把基础的东西MARK一遍。其次,通常情况下,HIIVE与mysql同时存在的,写了些异同和常见错误。以供参考。
 

1.基本语法

   
select A from B where Chive用limit
sql 用top
 select user_name 
from user_info 
where city='beijing' and sex='female' limit 10    --查看前10个用户

 

 查看分区表 dt='2019-04-09'
select user_name,piece, pay_amount 
from user_trade 
where dt='2019-04-09' --查看分区表

and goods_category='food';

 

Group by分类汇总 count();sum();avg();max();min();
select goods_category, 
count(distinct user_name) as user_num, 
sum(pay_amount) as total_amount 
from user_trade 
where dt between '2019-01-01' and '2019-04-30'
group by goods_category;

 

   
Order byASC:升序 12345 ,默认值 DESC:降序 多个字段 ORDER BY A ASC,BDESC
select user_name,
 sum(pay_amount) as total_amount
 from user_trade
  where dt between '2019-01-01' and '2019-04-30'
  group by total_amount DESC limit 5;

 

执行顺序from->where->group by->having->select->order by 

2.常用函数

   
时间戳转化为日期 
select pay_time,from_unix_time(pay_time,'yyyy-mm-dd hh:mm:ss')
from user_trade
where dt='2019-04-01';

 

计算日期间隔

datediff(string enddate,string startdate) 天数=结束日期-开始日期
增加date_add(string startdate.int days)

减少date_sub(string startdate.int days)

select user_name,
 datediff('2019-04-01',to_date(firstactivetime)
from user_info<br/>limit 10;
条件函数case when
age<20
第二句写age>=18 也会从20以后查询的。
select case when age<20 then'20岁以下'
              when age>=20 and age<30 then '20-30岁'
              when age>=30 and age<40 then '30-40岁'
	              else '40岁以上' end ,
  count(distinct user_id) user_num
from user_info
group by case when age<20 then'20岁以下'
	              when age>=20 and age<30 then '20-30岁'
	              when age>=30 and age<40 then '30-40岁'
	              else '40岁以上' end ;

 

 if
select sex,
       if(level>5,'高','低'),-- 0 '高' 1'低'<br/>        
           count(distinct user_id) user_num
       from user_info
       group by sex,
       if(level>5,'高','低');

 

   
字符串函数

截取字符串

substr(string A,int start,int len)

select substr(fristtime,1,7) as month,
      count() user_name
      from user_info
      group by substr(fristtime,1,7);

 

 如果不指定长度,则一直截取到最后。 
 查询字符 string类型
get_json_object(extra1,'$.phonebrand')
select get_json_object(extra1,'$.phonebrand') as phone_brand,
       count(distinct user_id) user_num
       from user_info
       group by get_json_object(extra1,'$.phonebrand');

 

 查询字符 map<string,string>
extra2['phonebrand']
select extra2['phonebrand']as phone_brand,
       count(distinct user_id) user_num
 from user_info
 group by extra2['phonebrand'];

 

   
note:不允许avg(count(*)) 
 datediff(max(pay_time),min(pay_time)) 

 

3.联合查询

  HIVEsql

1.inner join

1.必须重命名
2.on后面是唯一键值
3.inner可不写,效果一样
select * from user_list_1 a join user_list_2 b on a.user_id=b.user_id;

 

select * from user_list_1 a join user_list_2 b on a.user_id=b.user_id;
2表查询1.左2017,右2018 2.先条件筛选,再连接
select a.user_name 
a.user_name=b.user_name from (select distinct user_name --distinct 去重 
from user_trade -- where year(dt)=2019) a -- 分片 
join (select distinct user_name  from user_trade where year(dt)=2019) b on a.user_name=b.user_name;

 

 
3表查询写法1 :
先join 再选择(表小的时候可以)
select distinct a.user_name from trade_2017 a join trade_2018 b on a.user_name=b.user_name join trade_2019 c on b.user_name=c.user_name ;

 

 
 

写法2:

先去重再join

select a.user_name from (select distinct user_name --distinct 去重 
from trade_2017) a join (select distinct user_name --distinct 去重 
from trade_2018) b on a.user_name=b.user_name; 
join (select distinct user_name                    --distinct 去重 
from trade_2019) c on b.user_name=c.user_name;

 

 
    

2,left join
左表为全集,返回能匹配上的右表2的匹配结果,没有匹配上 显示NULL  
 HIVE不支持在in里加子查询 比如:在user_list_1 中但不在user_list_2中的用户
select a.user_id,      
 a.user_name  from  user_list_1 a 
 LEFT JOIN user_list_2 b on a.user_id=b_user_id
where b_user_id is null;

 

select  a.user_name
from user_list_1 a  
where a.user_name not in (select user_name from user_list_2 );

 

 3表查询
select  a.user_name
from 
     (select distinct user_name from trade_2017) a     
join 
      (select distinct user_name from trade_2018) b on a.user_name=b.user_name 
left join 
      (select distinct user_name from trade_2019) c on b.user_name=c.user_name              
where c.user_name is null;

 

 
    
3.full join 两个表合并在一起 coalesce() user_list1,user_list2的所有用户
select coalesce(a.user_name,b.user_name)from user_list_1 a FULL JOIN user_list_2 b on a.user_id=b.user_id;

 

 
4.union all

1.字段名必须一致

2.字段顺序必须一致

3.没有连接条件

select user_id,user_name from user_list_1 UNION ALL select user_id,user_name from user_list_3

 

 
    
5.union   

6.union all ,

left join,

full join 区别

union all(追加) left join(加在右边) full join(全排列)  
7.union all,union区别union all不去重不排序 union 去重排序  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: apache-hive-3.1.2-bin.tar.gz 是Apache Hive的二进制安装包。Apache Hive是一个基于Hadoop的数据仓库工具,可以将结构化数据映射到Hadoop上,并提供SQL查询和数据分析功能。该二进制安装包包含了Hive的所有运行时文件和依赖库,可以方便地进行安装和部署。 ### 回答2: apache-hive-3.1.2-bin.tar.gz是Apache Hive的一个版本,它是一个基于Hadoop的数据仓库和查询工具。Hive是一个开源的数据仓库工具,它提供了类似于SQL的查询语言,使用户能够使用简单的SQL查询Hadoop集群中的数据。 apache-hive-3.1.2-bin.tar.gz是Hive的二进制发布文件,通过下载和解压该文件,可以在Hadoop集群上安装和运行Hive。它包含了运行Hive所需的所有二进制文件、配置文件和库文件。 Hive可以将结构化数据映射为表,并提供了类似于SQL的查询语言- HiveQL,使用户可以使用熟悉的SQL语法对数据进行查询和分析。HiveHiveQL查询转换为MapReduce或Tez任务,然后在Hadoop集群上执行这些任务。这样,用户无需编写复杂的MapReduce程序,就可以利用Hadoop的强大的并行处理能力进行数据分析。 Hive还支持用户自定义函数、用户自定义聚合函数和用户自定义运算符,使用户能够根据自己的需求扩展Hive的功能。此外,Hive还提供了用于数据导入和导出的命令和工具,支持各种数据格式,如文本、CSV、JSON等。 总之,apache-hive-3.1.2-bin.tar.gz是Apache Hive的一个发行版本,通过安装和配置它,用户可以在Hadoop集群上使用Hive来进行数据仓库和查询操作,让用户能够更方便地利用Hadoop进行大数据分析和处理。 ### 回答3: Apache Hive 是一个建立在 Hadoop 之上的数据仓库基础结构,它提供了一种以类似于 SQL 的查询语言来进行数据分析和数据查询的方式。而 apache-hive-3.1.2-bin.tar.gz 是 Apache Hive 的一个二进制发行版本。 在 apache-hive-3.1.2-bin.tar.gz 这个压缩文件中,包含了 Hive 的所有二进制文件和必要的依赖库。通过下载并解压这个压缩包,你就可以在你的系统上快速部署和使用 Hive。 解压后的文件夹结构通常如下: - `bin` 文件夹:包含了 Hive 所有可执行文件,比如用于启动 Hive Shell 的 `hive` 命令。 - `conf` 文件夹:存放了 Hive 的配置文件,包括 Hive 的元数据存储位置、Hadoop 集群的配置等。 - `lib` 文件夹:包含了 Hive 的依赖库文件,这些库文件是 Hive 运行所需的。 - `examples` 文件夹:提供了一些 Hive 的示例查询和数据样例,方便用户了解和学习 Hive 的使用方法。 apache-hive-3.1.2-bin.tar.gz 是 Hive 在 3.1.2 版本的二进制发行包。版本号中的 3.1.2 表示这个发行版是在 Hive 的主版本号 3 下的次要版本号为 1,次次要版本号为 2 的版本。这个版本通常包含了以往版本的修复 bug、增加新功能等改进。 因此,如果你想在你的系统上开始使用 Hive 进行数据仓库的工作,你可以下载 apache-hive-3.1.2-bin.tar.gz 这个发行版,并按照官方文档的指引来进行部署和配置,然后就可以开始编写和执行 Hive 查询了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值