Hive 官网地址
hive官网
文档查看地址
文档地址
二、Hive 常用交互命令
(1)“-e”不进入 hive 的交互窗口执行 sql 语句
bin/hive -e "select id from student;"
(2)-f”执行脚本中 sql 语句
bin/hive -f /opt/module/hive/datas/hivef.sql
(3)退出 hive 窗口
hive(default)>exit; hive(default)>quit;
(4)在 hive cli 命令窗口中如何查看 hdfs 文件系统
hive(default)>dfs -ls /;
三、Hive 数据类型
(1)基本数据类型
| Hive 数据类型 | Java 数据类型 | 长度 | 例子 |
| TINYINT | byte | 1byte 有符号整数 | 20 |
| SMALINT | short | 2byte 有符号整数 | 20 |
| INT | int | 4byte 有符号整数 | 20 |
| BIGINT | long | 8byte 有符号整数 | 20 |
| BOOLEAN | boolean | 布尔类型,true 或者false | TRUE FALSE |
| FLOAT | float | 单精度浮点数 | 3.14159 |
| DOUBLE | double | 双精度浮点数 | 3.14159 |
| STRING | string | 字符系列。可以指定字 符集。可以使用单引号或者双 引号。 | ‘ now is the time ’ “for all good men” |
| TIMESTAMP | 时间类型 | ||
| BINARY | 字节数组 |
(2)集合数据类型
| 数据类型 | 描述 | 语法示例 |
| STRUCT | 和 c 语言中的 struct 类似,都可以通过“点”符号访 问元素内容。例如,如果某个列的数据类型是 STRUCT{first STRING, last STRING},那么第 1 个元素可以通过字段.first 来 引用。 | struct() 例 如 struct |
| MAP | MAP 是一组键-值对元组集合,使用数组表示法可以 访问数据。例如,如果某个列的数据类型是 MAP,其中键 ->值对是’first’->’John’和’last’->’Doe’,那么可以 通过字段名[‘last’]获取最后一个元素 | map() 例如 map |
| ARRAY | 数组是一组具有相同类型和名称的变量的集合。这些 变量称为数组的元素,每个数组元素都有一个编号,编号从 零开始。例如,数组值为[‘John’, ‘Doe’],那么第 2 个 元素可以通过数组名[1]进行引用。 | Array() 例如 array |
(1)创建数据库
// 数据库在 HDFS 上的默认存储路径是/user/hive/warehouse/*.db CREATE DATAbase [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_name=property_value, ...)]; eg: 创建一个数据库,指定数据库在 HDFS 上存放的位置, create database db_hive2 location '/db_hive2.db';
(2)查询数据库
hive> show databases; // 过滤显示查询的数据库 hive> show databases like 'db_hive*'; // 查看数据库详情 hive> desc database db_hive; // 显示数据库详细信息,extended desc database extended db_hive;
(3)修改数据库
// 用户可以使用 ALTER DATAbase 命令为某个数据库的 DBPROPERTIES 设置键-值对属性值, 来描述这个数据库的属性信息。
hive (default)> alter database db_hive
set dbproperties('createtime'='20220130');
(4)删除数据库
// 删除空数据库 hive>drop database db_hive2; // 如果删除的数据库不存在,最好采用 if exists 判断数据库是否存在 hive> drop database if exists db_hive2; // 如果数据库不为空,可以采用 cascade 命令,强制删除 hive> drop database db_hive cascade;
(5)创建表
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [PARTITIonED BY (col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] [ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path] [TBLPROPERTIES (property_name=property_value, ...)] [AS select_statement] eg: 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';
字段解释说明(6)修改表
(1)CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常; 用户可以用 IF NOT EXISTS 选项来忽略这个异常。
(2)EXTERNAL 关键字可以让用户创建一个外部表,在建表的同时可以指定一个指向实 际数据的路径(LOCATION),在删除表的时候,内部表的元数据和数据会被一起删除,而外 部表只删除元数据,不删除数据。
(3)COMMENT:为表和列添加注释。
(4)PARTITIonED BY 创建分区表
(5)CLUSTERED BY 创建分桶表
(6)SORTED BY 不常用,对桶中的一个或多个列另外排序
(7)ROW FORMAT
DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]|SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
用户在建表的时候可以自定义 SerDe 或者使用自带的 SerDe。如果没有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,将会使用自带的 SerDe。在建表的时候,用户还需 要为表指定列,用户在指定表的列的同时也会指定自定义的 SerDe,Hive 通过 SerDe 确定表 的具体的列的数据。
SerDe 是 Serialize/Deserilize 的简称, hive 使用 Serde 进行行对象的序列与反序列化。
(8)STORED AS 指定存储文件类型 常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)
如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在 HDFS 上的存储位置。
(10)AS:后跟查询语句,根据查询结果创建表。
(11)LIKE 允许用户复制现有的表结构,但是不复制数据。
// 修改表名 ALTER TABLE table_name RENAME TO new_table_name; // 增加单个表分区 hive (default)> alter table dept_partition add partition(day='20200404'); // 增加多个表分区 hive (default)> alter table dept_partition add partition(day='20200405') partition(day='20200406'); // 删除单个分区 hive (default)> alter table dept_partition drop partition (day='20200406'); // 同时删除多个分区 hive (default)> alter table dept_partition drop partition (day='20200404'), partition(day='20200405');
(7)删除表
hive (default)> drop table dept;五、DML 数据操作
(1)数据导入 load data
hive> load data [local] inpath '数据的 path' [overwrite] into table student [partition (partcol1=val1,…)]; (1)load data:表示加载数据 (2)local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表 (3)inpath:表示加载数据的路径 (4)overwrite:表示覆盖表中已有数据,否则表示追加 (5)into table:表示加载到哪张表 (6)student:表示具体的表
(2)插入数据
// 基本模式插入(根据单张表查询结果) hive (default)> insert overwrite table student_par select id, name from student where month='201709'; // 多表(多分区)插入模式(根据多张表查询结果),student 为具体的源表 hive (default)> from student insert overwrite table student partition(month='201707') select id, name where month='201709' insert overwrite table student partition(month='201706') select id, name where month='201709';
(3)创建表时通过 Location 指定加载数据路径
1.上传数据到 hdfs 上 hive (default)> dfs -mkdir /student; hive (default)> dfs -put /opt/module/datas/student.txt /student; 2.创建表,并指定在 hdfs 上的位置 hive (default)> create external table if not exists student5( id int, name string) row format delimited fields terminated by 't' location '/student; 3.查询数据 hive (default)> select * from student5;
(4)import 数据到指定 Hive 表中
hive (default)> import table student2 from '/user/hive/warehouse/export/student';
(5)数据导出
1.Insert 导出,将查询的结果导出到本地 hive (default)> insert overwrite local directory '/opt/module/hive/data/export/student' select * from student; 2.将查询的结果格式化导出到本地 hive(default)>insert overwrite local directory '/opt/module/hive/data/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' select * from student; 3.将查询的结果导出到 HDFS 上(没有 local) hive (default)> insert overwrite directory '/user/atguigu/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' select * from student; 4.Hadoop 命令导出到本地 hive (default)> dfs -get /user/hive/warehouse/student/student.txt /opt/module/data/export/student3.txt; 5. Hive Shell 命令导出 bin/hive -e 'select * from default.student;' > /opt/module/hive/data/export/student4.txt; 6. Export 导出到 HDFS 上 hive (default)> export table default.student to
(6)清除表中数据(Truncate)
// 注意:Truncate 只能删除管理表,不能删除外部表中数据 hive (default)> truncate table student;六、查询
官网查询解释
SELECt [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERe where_condition]
[GROUP BY col_list]
[ORDER BY col_list]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY col_list]
]
[LIMIT [offset,] rows]
七、分区表和分桶表
(1)分区表
1. 创建分区表语法 hive (default)> create table dept_partition( deptno int, dname string, loc string) partitioned by (day string) row format delimited fields terminated by 't'; 注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。 2.加载数据到分区表中,注意:分区表加载数据时,必须指定分区 hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition partition(day='20200401'); hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200403.log' into table dept_partition partition(day='20200403'); 3.查询分区表中数据 单分区查询 hive (default)> select * from dept_partition where day='20200401'; 4.增加分区 hive (default)> alter table dept_partition add partition(day='20200404'); hive (default)> alter table dept_partition add partition(day='20200405') partition(day='20200406'); 5.删除分区 hive (default)> alter table dept_partition drop partition (day='20200406'); hive (default)> alter table dept_partition drop partition (day='20200404'), partition(day='20200405'); 6.查看分区表有多少分区 hive> show partitions dept_partition; 7.查看分区表结构 hive> desc formatted dept_partition;
(2)二级分区
// 创建二级分区表 hive (default)> create table dept_partition2( deptno int, dname string, loc string) partitioned by (day string, hour string) row format delimited fields terminated by 't'; // 加载数据到二级分区表中 hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401', hour='12'); // 查询分区数据 hive (default)> select * from dept_partition2 where day='20200401' and hour='12';
(3)把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
方式一:上传数据后修复 上传 hive (default)> dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13; hive (default)> dfs -put /opt/module/datas/dept_20200401.log /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13; 查询数据(查询不到刚上传的数据) hive (default)> select * from dept_partition2 where day='20200401' and hour='13'; 执行修复命令 hive> msck repair table dept_partition2; 方式二:上传数据后添加分区 上传数据 hive (default)> dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14; hive (default)> dfs -put /opt/module/hive/datas/dept_20200401.log /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14; 执行添加分区 hive (default)> alter table dept_partition2 add partition(day='201709',hour='14'); 查询数据 hive (default)> select * from dept_partition2 where day='20200401' and hour='14'; 方式三:创建文件夹后 load 数据到分区 创建目录 hive (default)> dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=15; 上传数据 hive (default)> load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401',hour='15'); 查询数据 hive (default)> select * from dept_partition2 where day='20200401' and hour='15';
(4)分桶表
// 创建分桶表 create table stu_buck(id int, name string) clustered by(id) into 4 buckets row format delimited fields terminated by 't'; // 查看表结构 hive (default)> desc formatted stu_buck; Num Buckets: 4 // 导入数据到分桶表中,load 的方式 hive (default)> load data inpath '/student.txt' into table stu_buck; 分桶表操作需要注意的事项: (1)reduce 的个数设置为-1,让 Job 自行决定需要用多少个 reduce 或者将 reduce 的个 数设置为大于等于分桶表的桶数 (2)从 hdfs 中 load 数据到分桶表中,避免本地文件找不到问题 (3)不要使用本地模式八、函数
(1)查看系统自带的函数
hive> show functions;
(2)显示自带的函数的用法
hive> desc function upper;
(3)详细显示自带的函数的用法
hive> desc function extended upper;



