0
点赞
收藏
分享

微信扫一扫

学习Hive

九月的栩 2022-11-21 阅读 192


学习Hive

学习Hive_hive


学习Hive_数据_02


对于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

学习Hive_数据_03

学习Hive_数据_04

内容就是:
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

学习Hive_hive_05


show databases;

学习Hive_数据库_06


use demo;

学习Hive_数据库_07


创建表:

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’; – 行分隔符,每行的数据按照换行符区分

学习Hive_数据_08


学习Hive_hive_09


show tables;

学习Hive_数据_10


desc test;

学习Hive_数据_11


加粗样式

将刚才的test.txt数据导入到测试表

退出beeline客户端

!quit

学习Hive_hive_12


将windows的数据导入linux

rz

学习Hive_数据库_13


学习Hive_hive_14


学习Hive_数据库_15


默认将你选中的windows的文件上传到了当前linux目录

学习Hive_hive_16


重新试一下:

将E:\io\hivetest的test.txt文件上传到/opt/module/hive/datas目录下面

学习Hive_数据库_17


学习Hive_数据库_18


学习Hive_数据_19


学习Hive_hive_20


将linux系统的’/opt/module/datas/test.txt文件导入表test中:

先进入beeline

beeline -u jdbc:hive2://hadoop102:10000 -n atguigu

学习Hive_hive_21


学习Hive_数据库_22


load data local inpath ‘/opt/module/hive/datas/test.txt’ into table test;

学习Hive_数据_23


学习Hive_hive_24


创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db。

学习Hive_数据库_25


学习Hive_数据库_26


学习Hive_hive_27


学习Hive_hive_28

创建一个数据库,指定数据库在HDFS上存放的位置

create database demo3 location ‘/demo3’;

学习Hive_数据库_29


学习Hive_hive_30


学习Hive_数据库_31


查看具体表结构和数据库结构

desc test;

学习Hive_数据库_32


desc database demo;

学习Hive_hive_33


显示数据库详细信息,extended

想更详细的显示数据库的信息。

学习Hive_hive_34


用户可以使用ALTER DATABASE命令为某个数据库的DBPROPERTIES设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。

alter database demo set dbproperties(‘createtime’=‘20170830’);

学习Hive_数据库_35


学习Hive_hive_36


删除一个空数据库

学习Hive_数据库_37


学习Hive_数据_38


学习Hive_数据_39


如果数据库不为空,可以采用cascade命令,强制删除

学习Hive_数据库_40


学习Hive_数据库_41


学习Hive_数据库_42


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也会删除这个表中数据。

创建表:

原始数据

学习Hive_数据库_43


学习Hive_hive_44

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’;

学习Hive_hive_45


学习Hive_数据_46


load data local inpath ‘/opt/module/hive/datas/student.txt’ into table student;

学习Hive_数据库_47


学习Hive_数据库_48


根据查询结果创建表(查询的结果会添加到新创建的表中)

create table if not exists student2 as select id, name from student;

学习Hive_数据_49


学习Hive_数据库_50


学习Hive_数据库_51


(3)根据已经存在的表结构创建表(只复制表结构)

create table if not exists student3 like student;

学习Hive_数据_52


学习Hive_数据_53


学习Hive_数据_54


查询表的类型

desc student;

desc formatted student;两种结果对比

这是desc student;

学习Hive_数据库_55


学习Hive_hive_56


这是desc formatted student;

学习Hive_hive_57


学习Hive_数据_58

外部表
因为表是外部表,所以Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。

分别创建部门和员工外部表,并向表中导入数据。

源数据:

学习Hive_数据_59


学习Hive_数据_60


学习Hive_数据库_61


(2)建表语句,创建外部表创建部门表

create external table if not exists default.dept(

deptno int,

dname string,

loc int

)

row format delimited fields terminated by ‘\t’;

学习Hive_数据_62


学习Hive_数据库_63


创建员工表

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’;

学习Hive_hive_64


学习Hive_数据库_65


学习Hive_数据_66


导入表中数据

load data local inpath ‘/opt/module/hive/datas/dept.txt’ into table dept;

学习Hive_数据库_67


学习Hive_hive_68


load data local inpath ‘/opt/module/hive/datas/emp.txt’ into table emp;

学习Hive_数据库_69


学习Hive_数据_70


学习Hive_hive_71

(5)删除外部表

drop table dept;

外部表删除后,hdfs中的数据还在,但是metadata中dept的元数据已被删除

学习Hive_数据库_72


学习Hive_数据_73


学习Hive_数据库_74


学习Hive_数据_75

删除内部表的话,所有数据全部会被删除

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;

学习Hive_数据_76


学习Hive_数据库_77


学习Hive_数据库_78


学习Hive_数据库_79


学习Hive_数据库_80

管理表与外部表的互相转换

student表是内部表

学习Hive_数据库_81

修改内部表student为外部表

alter table student set tblproperties(‘EXTERNAL’=‘TRUE’);

学习Hive_数据库_82

修改外部表student为内部表

alter table student set tblproperties(‘EXTERNAL’=‘FALSE’);

学习Hive_数据库_83


学习Hive_数据库_84


举报

相关推荐

0 条评论