hive初识

数据仓库:Data Warehouse 简写为DW

数据仓库 面向分析(大而全、准确性):

是面向主题、集成、不可修改、反映历史变化   用于数据分析,辅助管理决策

数据库 面向事务(一致性、时效性)

为什么分区?

可以让用户在做数据分析统计时缩小数据扫描的范围,因为可以在select时指定要统计的分区,提高查询效率。

分区字段是隐藏的,非建表字段,但是select会查出来

一张表可以有多个分区,分区下也可以有多个分区,无限套娃

PARTIONED BY(col_name data_type)

为什么分桶?

单个分区或者表中的数据量越来越大,当分区不能更细粒的划分数据时,所以会采用分桶技术将数据更细粒度的划分和管理。

基本数据类型:

Hive数据类型Java数据类型长度示例
TINYINTbyte1字节6
SMALLINTshort2字节6
INTint4字节6
BIGINTlong5字节6
BOOLEANboolean布尔类型
true / false
true/false
FLOATfloat单精度浮点数3.1415926
DOBULEdouble双精度浮点数3.1415926
STRINGString字符串
可以使用单引号或者双引号
“中华人民共和国”
TIMESTAMP时间类型
BINARY字节数组

集合类型:

数据类型描述语法示例
MAPMAP 是一组键-值对元组集合,使用数组表示法可以
访问数据。例如,如果某个列的数据类型是 MAP,其中
键->值对是’first’->’John’和’last’>’Doe’,那
么可以通过字段名[‘last’]获取最后一个元素
map()
例如:map<string, int>
ARRAY数组是一组具有相同类型名称的变量的集合。这些
变量称为数组的元素,每个数组元素都有一个编号,编号从
零开始。例如,数组值为[‘John’, ‘Doe’],那么第 2 个元素
可以通过数组名[1]进行引用。
Array()
例如:array
STRUCT和 c 语言中的 struct 类似,都可以通过“点”符号访
问元素内容。例如,如果某个列的数据类型
是 STRUCT{first STRING, last STRING}
,那么第 1 个元素可以通过字段.first来引用。
struct()
例如:struct<street:string, city:string>
格式为:字段名:字段类型

集合类型之array (1) 先创建一张表:

create table t_array(id int,name string,hobby array<string>)
row format delimited
fields terminated by ','
collection items terminated by '-'
stored as textfile;

load data inpath '/tmp/array.txt' into table t_array;

(2) 准备数据文件 t_array.txt

1,zhangsan,唱歌-跳舞-游泳
2,lisi,打游戏-篮球

(3) 查询数据

select id ,name,hobby[0],hobby[1] from t_array;
+-----+-----------+------+------+
| id  |   name    | _c2  | _c3  |
+-----+-----------+------+------+
| 1   | zhangsan  | 唱歌   | 跳舞   |
| 2   | lisi      | 打游戏  | 篮球   |
+-----+-----------+------+------+
select id ,name,hobby[0],hobby[1],hobby[2] from t_array;
+-----+-----------+------+------+-------+
| id  |   name    | _c2  | _c3  |  _c4  |
+-----+-----------+------+------+-------+
| 1   | zhangsan  | 唱歌   | 跳舞   | 游泳    |
| 2   | lisi      | 打游戏  | 篮球   | NULL  |
+-----+-----------+------+------+-------+
select * from t_array;
+-------------+---------------+-------------------+
| t_array.id  | t_array.name  |   t_array.hobby   |
+-------------+---------------+-------------------+
| 1           | zhangsan      | ["唱歌","跳舞","游泳"]  |
| 2           | lisi          | ["打游戏","篮球"]      |
+-------------+---------------+-------------------+
2 rows selected (0.114 seconds)

集合类型之map

(1) 先创建一张表

create table t_map(id int,name string,hobby map<string,string>)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
stored as textfile;

#加载数据
load data inpath '/tmp/t_map.txt' into table t_map;

(2) 准备数据文件 t_map.txt

1,zhangsan,唱歌:非常喜欢-跳舞:喜欢-游泳:一般般
2,lisi,打游戏:非常喜欢-篮球:不喜欢

查询数据:

 select * from _map;
+-----------+-------------+-------------------------------------+
| t_map.id  | t_map.name  |             t_map.hobby             |
+-----------+-------------+-------------------------------------+
| 1         | zhangsan    | {"唱歌":"非常喜欢","跳舞":"喜欢","游泳":"一般般"}  |
| 2         | lisi        | {"打游戏":"非常喜欢","篮球":"不喜欢"}           |
+-----------+-------------+-------------------------------------+
2 rows selected (0.103 seconds)
select id,name,hobby['唱歌'] from t_map;
+-----+-----------+-------+
| id  |   name    |  _c2  |
+-----+-----------+-------+
| 1   | zhangsan  | 非常喜欢  |
| 2   | lisi      | NULL  |
+-----+-----------+-------+
2 rows selected (0.115 seconds)
select id,name,hobby['唱歌'],hobby['跳舞'] from t_map;
+-----+-----------+-------+-------+
| id  |   name    |  _c2  |  _c3  |
+-----+-----------+-------+-------+
| 1   | zhangsan  | 非常喜欢  | 喜欢    |
| 2   | lisi      | NULL  | NULL  |
+-----+-----------+-------+-------+

hive基本操作:

        输入hive,进入hive

        查看所有数据库:show databases;

        建表语句:

create table test ( 
	name string, 
	friends array<string>,
	children map<string, int>, 
	address struct<street:string, city:string>
) 
row format delimited 
fields terminated by ',' 
collection items terminated by '_' 
map keys terminated by ':' 
lines terminated by '\n';

        选择数据库:

use tablename;

        查看数据库中所有表:

show tables;

        创建表,以及增删改查都与MySQL语法基本一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值