hive创建表方式
方式一:create table
指定字段信息
方式二:CTAS
通过子查询创建表,将查询的数据存入另一张表。
create table as Select
方式三:create like
创建类似表
create table new_table like old_table/view
--数据数据库
create database db_lianjia;
--创建表
create table db_lianjia.tb_sh_price(
name STRING,
house_type STRING,
house_area STRING,
region STRING,
floor_str STRING,
direction STRING,
total_price STRING,
square_price STRING,
build_date STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
面积:统计各个房面积 所在房数
50㎡以下 50-70㎡ 70-90㎡ 90-110㎡ 110-140㎡
使用desc function WHEN
select
t.area_grop,count(house_area) as total
from
(
select
house_area,
case
when 0< house_area and house_area<=50 then "50平以下"
when 50< house_area and house_area<=70 then "50-70平"
when 70< house_area and house_area<=90 then "70-90平"
when 90< house_area and house_area<=110 then "90-110平"
when 110< house_area and house_area<=140 then "110-140平"
else "140平+"
end as area_grop
from
db_lianjia.tb_sh_price
) t
group by
t.area_grop

分析:
楼龄(1992年建)
5年以内 10年以内 15年以内 20年以内
截取函数substring
substring(str, pos[, len]) - returns the substring of str that starts
at pos and is of length len or substring(bin, pos[, len]) -
returns the slice of byte array that starts at pos and is of length len
去除2端空格trim()
获取字符串长度length
select
build_date,
case
when (2019-substring(build_date,0,4)) between 0 and 5 then "5年以内"
when (2019-substring(build_date,0,4)) between 5 and 10 then "10年以内"
when (2019-substring(build_date,0,4)) between 10 and 15 then "15年以内"
when (2019-substring(build_date,0,4)) between 15 and 20 then "20年以内"
else "20年以上"
end as year_grop
from
tb_house_price
where
length(trim(build_date))>0
limit 20;

hive 分区表
分区表是将大表的数据分成称为分区的许多小的子集,举例,以各地房价为例,每个地方的房价肯定不一样,也不能给不同地区的房价求平均值,可以将不同区域的房价分开存放,每个地方每月的数据也会不同,在不同区域的基础上再按照月份分类。
分区:partition
创建一个分区表
部分数据
梅园六街坊,2室0厅,47.72,浦东,低区/6层,朝南,500,104777,1992年建
碧云新天地(一期),3室2厅,108.93,浦东,低区/6层,朝南,735,67474,2002年建
博山小区,1室1厅,43.79,浦东,中区/6层,朝南,260,59374,1988年建
金桥新村四街坊(博兴路986弄),1室1厅,41.66,浦东,中区/6层,朝南北,280,67210,1997年建
博山小区,1室0厅,39.77,浦东,高区/6层,朝南,235,59089,1987年建
潍坊三村,1室0厅,34.84,浦东,中区/5层,,260,74626,1983年建
伟莱家园,2室2厅,100.15,浦东,中区/6层,朝南北,515,51422,2002年建
世茂滨江花园,3室2厅,260.39,浦东,中区/51层,朝西,2200,84488,
羽北小区,2室2厅,69.88,浦东,低区/6层,朝南,560,80137,1994年建
证大家园(公寓),3室2厅,122.75,浦东,低区/11层,朝南北,785,63951,2002年建
上南十村,1室1厅,40.17,浦东,低区/6层,朝南,240,59746,1992年建
鹏欣家园,1室1厅,59.42,浦东,中区/6层,朝南,410,69000,1998年建
香楠小区,2室2厅,80.34,浦东,高区/6层,,420,52277,
恒大华城新华苑,2室1厅,68.9,浦东,高区/6层,朝南北,460,66763,1997年建
创建:
按月份和城市:
CREATE TABLE db_house.tb_second_price(
name STRING,
house_type STRING,
house_area STRING,
region STRING,
floor_str STRING,
direction STRING,
total_price STRING,
square_price STRING,
build_date STRING
)
PARTITIONED by (month_str STRING comment 'first partition Month',
city_str STRING comment 'first partition City')
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
加载数据:
LOAD DATA LOCAL INPATH '/opt/datas/2nd_05_BJ_price.csv' INTO TABLE db_house.tb_second_price PARTITION(month_str='05',city_str='BJ');
LOAD DATA LOCAL INPATH '/opt/datas/2nd_05_SH_price.csv' INTO TABLE db_house.tb_second_price PARTITION(month_str='05',city_str='SH');
LOAD DATA LOCAL INPATH '/opt/datas/2nd_06_BJ_price.csv' INTO TABLE db_house.tb_second_price PARTITION(month_str='06',city_str='BJ');
LOAD DATA LOCAL INPATH '/opt/datas/2nd_06_SH_price.csv' INTO TABLE db_house.tb_second_price PARTITION(month_str='06',city_str='SH');
分区信息也是元数据的一部分


分区表是如何管理数据(数据文件)
普通表数据文件存储:
将表中所有数据放到对应HDFS目录下即可
db.lianjia.db/tb_sh_price/*.data
分区表:
按照分区字段创建对应分区目录,划分数据存储
db.lianjia.db/tb_sh_price
bj,06月份数据
db.lianjia.db/tb_sh_price/month_str=06/city_str=bj/
bj,03月份数据
db.lianjia.db/tb_sh_price/month_str=03/city_str=bj/
sh,03月份数据
db.lianjia.db/tb_sh_price/month_str=03/city_str=sh/
分区表是如何分析数据
分析:
sh,05月份数据
select
count(*)
from
db_house.tb_second_price
where
month_str='05'and city_str='SH';
分区表加载数据
方式一:
load
LOAD DATA LOCAL INPATH '/opt/datas/2nd_05_BJ_price.csv' INTO TABLE db_house.tb_second_price PARTITION(month_str='05',city_str='BJ');
方式二:直接使用put命令上传
创建文件目录:
dfs -mkdir -p /user/hive/warehouse/db_house.db/tb_second_price/month_str=05/city_str=XA
加载数据:
dfs -put /opt/datas/2nd_05_XA_price.csv /user/hive/warehouse/db_house.db/tb_second_price/month_str=05/city_str=XA
严格模式
介绍
在配置文件hive-site.xml中设置:
<property>
<name>hive.mapred.mode</name>
<value>nonstrict</value>
<description>
The mode in which the Hive operations are being performed.
In strict mode, some risky(危险) queries are not allowed to run. They include:
Cartesian Product.笛卡尔积,join 没有on
No partition being picked up for a query.没有分区信息的查询:分区表必须指定分区字段
Comparing bigints and strings.
Comparing bigints and doubles.
Orderby without limit. order by 没有limit
</description>
</property>
直接在hive客户端下输入:
set hive.mapred.mode = strict
外部表与分区表结合使用
create EXTERNAL table db_house.lianjia_2nd_house(
name STRING,
house_type STRING,
house_area STRING,
region STRING,
floor_str STRING,
direction STRING,
total_price STRING,
square_price STRING,
build_date STRING
)
PARTITIONED BY (month_str STRING COMMENT 'first partition Month',city_str STRING COMMENT 'first partition City')
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
LOCATION '/user/huadian/lianjia_2nd_house'
--(3)查看表详情
desc formatted db_house.lianjia_2nd_house;

HiveServer2
介绍
将hive作为一个服务启动(类比于MYSQL服务)
bin/hive 默认会自动一个服务,为Hive CLi提供服务
官方文档:
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients

启动hiveServer2服务
前端启动:
bin/hiveserver2
ctrl +C 或者窗口关闭就没了,
后台启动:将服务作为守护进程(deamon)启动
nohup bin/hiveserver2 > hiveServer2.log &
nohup和&啥意思
http://blog.songchunmin.com/698.html
nohup是永久执行
&是指在后台运行
Beeline 客户端
方式一:
bin/beeline
!connect jdbc:hive2://bigdata-hpsk01.huadian.com:10000 huadian huadian
方式二:
bin/beeline -u jdbc:hive2://bigdata-hpsk01.huadian.com:10000 -n huadian -p huadian
通过JDBC客户端去连接(了解)
代码实现

注意:运行,hive切换到apache版本,CDH版本,自己管理jar包
好处
(1)实现交互式查询
Java后台直接访问Hive,直接获取结果数据
(2)直接走文件扫描,不走MR程序
什么情况下,HiveQL不执行MapReduce而是直接返回结果
fecthTask
hive中如果是简单查询,没有函数,orderby等,语句会从hdfs直接查询不会转成mapreduce
将hive.fetch.task.conversion设置成more即可,默认为minimal。
-1. select * from
-2. select 某些字段 from
在hive-site.xml
<property>
<name>hive.fetch.task.conversion</name>
<value>more</value>
<description>
Expects one of [none, minimal, more].
Some select queries can be converted to single FETCH task minimizing latency.
Currently the query should be single sourced not having any subquery and should not have
any aggregations or distincts (which incurs RS), lateral views and joins.
0. none : disable hive.fetch.task.conversion
1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
2. more : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)
</description>
</property>
本文详细介绍了Hive分区表的概念、创建与管理,包括按月份和城市进行分区的数据组织方式。此外,还讲解了HiveServer2的服务启动、Beeline客户端的使用,以及通过JDBC进行连接的情况,强调了在某些条件下HiveQL可以不执行MapReduce任务。
1027

被折叠的 条评论
为什么被折叠?



