学习Hive
对于Hive的String类型相当于数据库的varchar类型
Hive有三种复杂数据类型ARRAY、MAP 和 STRUCT。ARRAY和MAP与Java中的Array和Map类似,而STRUCT与C语言中的Struct类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。
注意:MAP,STRUCT和ARRAY里的元素间关系都可以用同一个字符表示,这里用“_”。
json格式的数据:
{
“name”: “songsong”,
“friends”: [“bingbing” , “lili”] , //列表Array,
“children”: { //键值Map,
“xiao song”: 18 ,
“xiaoxiao song”: 19
}
“address”: { //结构Struct,
“street”: “hui long guan” ,
“city”: “beijing”
}
}
一条数据:
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
新建一个本地文件test.txt
内容就是:
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
在Hive上创建测试表test
create table test(
name string,
friends array,
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’;
进入beeline客户端:
beeline -u jdbc:hive2://hadoop102:10000 -n atguigu
show databases;
use demo;
创建表:
create table test(
name string,
friends array,
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’;
row format delimited fields terminated by ‘,’ 这是说明列分隔符是逗号
collection items terminated by ‘_’ --MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)
map keys terminated by ‘:’ – MAP中的key与value的分隔符
lines terminated by ‘\n’; – 行分隔符,每行的数据按照换行符区分
show tables;
desc test;
加粗样式
将刚才的test.txt数据导入到测试表
退出beeline客户端
!quit
将windows的数据导入linux
rz
默认将你选中的windows的文件上传到了当前linux目录
重新试一下:
将E:\io\hivetest的test.txt文件上传到/opt/module/hive/datas目录下面
将linux系统的’/opt/module/datas/test.txt文件导入表test中:
先进入beeline
beeline -u jdbc:hive2://hadoop102:10000 -n atguigu
load data local inpath ‘/opt/module/hive/datas/test.txt’ into table test;
创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db。
创建一个数据库,指定数据库在HDFS上存放的位置
create database demo3 location ‘/demo3’;
查看具体表结构和数据库结构
desc test;
desc database demo;
显示数据库详细信息,extended
想更详细的显示数据库的信息。
用户可以使用ALTER DATABASE命令为某个数据库的DBPROPERTIES设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。
alter database demo set dbproperties(‘createtime’=‘20170830’);
删除一个空数据库
如果数据库不为空,可以采用cascade命令,强制删除
1)建表语法
(1)CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。
(2)EXTERNAL关键字可以让用户创建一个外部表,在建表的同时可以指定一个指向实际数据的路径(LOCATION),在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
(3)COMMENT:为表和列添加注释。
(4)PARTITIONED BY创建分区表
(5)CLUSTERED BY创建分桶表
SerDe是Serialize/Deserilize的简称, hive使用Serde进行行对象的序列与反序列化。
用户在建表的时候可以自定义SerDe或者使用自带的SerDe。如果没有指定ROW FORMAT 或者ROW FORMAT DELIMITED,将会使用自带的SerDe。在建表的时候,用户还需要为表指定列,用户在指定表的列的同时也会指定自定义的SerDe,Hive通过SerDe确定表的具体的列的数据。
(8)STORED AS指定存储文件类型
如果文件数据是纯文本,可以使用STORED AS TEXTFILE。
(9)LOCATION :指定表在HDFS上的存储位置。
(10)AS:后跟查询语句,根据查询结果创建表。
(11)LIKE允许用户复制现有的表结构,但是不复制数据。
默认创建的表都是所谓的管理表,有时也被称为内部表。当我们删除一个管理表时,Hive也会删除这个表中数据。
创建表:
原始数据
create table if not exists student(
id int, name string
)
row format delimited fields terminated by ‘\t’
stored as textfile
location ‘/user/hive/warehouse/student’;
load data local inpath ‘/opt/module/hive/datas/student.txt’ into table student;
根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists student2 as select id, name from student;
(3)根据已经存在的表结构创建表(只复制表结构)
create table if not exists student3 like student;
查询表的类型
desc student;
desc formatted student;两种结果对比
这是desc student;
这是desc formatted student;
外部表
因为表是外部表,所以Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。
分别创建部门和员工外部表,并向表中导入数据。
源数据:
(2)建表语句,创建外部表创建部门表
create external table if not exists default.dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by ‘\t’;
创建员工表
create external table if not exists default.emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by ‘\t’;
导入表中数据
load data local inpath ‘/opt/module/hive/datas/dept.txt’ into table dept;
load data local inpath ‘/opt/module/hive/datas/emp.txt’ into table emp;
(5)删除外部表
drop table dept;
外部表删除后,hdfs中的数据还在,但是metadata中dept的元数据已被删除
删除内部表的话,所有数据全部会被删除
create table if not exists student(
id int, name string
)
row format delimited fields terminated by ‘\t’;
load data local inpath ‘/opt/module/hive/datas/student.txt’ into table student;
管理表与外部表的互相转换
student表是内部表
修改内部表student为外部表
alter table student set tblproperties(‘EXTERNAL’=‘TRUE’);
修改外部表student为内部表
alter table student set tblproperties(‘EXTERNAL’=‘FALSE’);