网站首页可以做竖版吗,wordpress 3.4.2,大朗做网站在,企业培训师【大数据进阶第三阶段之Hive学习笔记】Hive安装-CSDN博客
【大数据进阶第三阶段之Hive学习笔记】Hive常用命令和属性配置-CSDN博客
【大数据进阶第三阶段之Hive学习笔记】Hive基础入门-CSDN博客
【大数据进阶第三阶段之Hive学习笔记】Hive查询、函数、性能优化-CSDN博客
…【大数据进阶第三阶段之Hive学习笔记】Hive安装-CSDN博客
【大数据进阶第三阶段之Hive学习笔记】Hive常用命令和属性配置-CSDN博客
【大数据进阶第三阶段之Hive学习笔记】Hive基础入门-CSDN博客
【大数据进阶第三阶段之Hive学习笔记】Hive查询、函数、性能优化-CSDN博客
1、Hive数据类型
1.1、基本数据类型 红标为常用的数据类型
对于Hive的String类型相当于数据库的varchar类型该类型是一个可变的字符串不过它不能声明其中最多能存储多少个字符。
1.2、集合数据类型 1.3、类型转化 可以使用CAST操作显示进行数据类型转换 例如CAST(‘1’ AS INT)将把字符串’1’ 转换成整数1如果强制类型转换失败如执行CAST(‘X’ AS INT)表达式返回空值 NULL。 2、DDL数据定义
2.1、创建数据库 创建一个数据库数据库在HDFS上的默认存储路径是/opt/hive/warehouse/*.db create database hivetest; 避免要创建的数据库已经存在错误增加if not exists判断。标准写法 create database if not exists hivetest; 创建一个数据库指定数据库在HDFS上存放的位置 create database if not exists hivetest location hdfs路径; 2.2、查询数据库 显示数据库 show databases; 过滤显示查询的数据库 show databases like hivetest*; 查看数据库详情 desc database hivetest; 切换当前数据库 use 目标数据库名称; 2.3删除数据库 删除空数据库 drop database 库名; 如果删除的数据库不存在最好采用 if exists判断数据库是否存在 drop database if exists 库名; 如果数据库不为空可以采用cascade命令强制删除 drop database 库名 cascade; 2.4、创建表 建表语法 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] 字段解释说明 1CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在则抛出异常用户可以用 IF NOT EXISTS 选项来忽略这个异常。
2EXTERNAL关键字可以让用户创建一个外部表在建表的同时指定一个指向实际数据的路径LOCATIONHive创建内部表时会将数据移动到数据仓库指向的路径若创建外部表仅记录数据所在的路径不对数据的位置做任何改变。在删除表的时候内部表的元数据和数据会被一起删除而外部表只删除元数据不删除数据。
3COMMENT为表和列添加注释。
4PARTITIONED BY创建分区表
5CLUSTERED BY创建分桶表
6SORTED BY不常用
7ROW 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_nameproperty_value, property_nameproperty_value, …)]
用户在建表的时候可以自定义SerDe或者使用自带的SerDe。如果没有指定ROW FORMAT 或者ROW FORMAT DELIMITED将会使用自带的SerDe。在建表的时候用户还需要为表指定列用户在指定表的列的同时也会指定自定义的SerDeHive通过SerDe确定表的具体的列的数据。
SerDe是Serialize/Deserilize的简称目的是用于序列化和反序列化。
8STORED AS指定存储文件类型
常用的存储文件类型SEQUENCEFILE二进制序列文件、TEXTFILE文本、RCFILE列式存储格式文件
如果文件数据是纯文本可以使用STORED AS TEXTFILE。如果数据需要压缩使用 STORED AS SEQUENCEFILE。
9LOCATION 指定表在HDFS上的存储位置。
10LIKE允许用户复制现有的表结构但是不复制数据。
2.4.1、内部表 默认创建的表都是所谓的管理表有时也被称为内部表。因为这种表Hive会或多或少地控制着数据的生命周期。Hive默认情况下会将这些表的数据存储在由配置项hive.metastore.warehouse.dir(例如/opt/hive/warehouse)所定义的目录的子目录下。 当我们删除一个管理表时Hive也会删除这个表中数据。管理表不适合和其他工具共享数据。 普通创建表 create table if not exists student2( id int, name string ) row format delimited fields terminated by \t; 根据查询结果创建表查询的结果会添加到新创建的表中 create table if not exists student3 as select id, name from student; 根据已经存在的表结构创建表 create table if not exists student4 like student; 查询表的类型 desc formatted student2; 2.4.2、外部表 因为表是外部表所以Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据不过描述表的元数据信息会被删除掉。
管理表和外部表的使用场景 每天将收集到的网站日志定期流入HDFS文本文件。在外部表原始日志表的基础上做大量的统计分析用到的中间表、结果表使用内部表存储数据通过SELECTINSERT进入内部表。 案例详解 分别创建employee外部表并向表中导入数据。 Michael|Montreal,Toronto|Male,30|DB:80|Product:DeveloperLead Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect Lucy|Vancouver|Female,57|Sales:89|Sales:Lead 建表语句 创建员工表 create external table if not exists employee( name string, address arraystring, personalInfo arraystring, technol mapstring,int, jobs mapstring,string) row format delimited fields terminated by | collection items terminated by , map keys terminated by : lines terminated by \n; 向外部表中导入数据 load data local inpath /root/employee.txt into table employee; 查询结果 select * from employee; 2.4.3管理表与外部表的互相转换 修改内部表student2为外部表 alter table student2 set tblproperties(EXTERNALTRUE); 修改外部表student2为内部表 alter table student2 set tblproperties(EXTERNALFALSE); 注意(EXTERNALTRUE)和(EXTERNALFALSE)为固定写法区分大小写 2.5、分区表partition 分区表实际上就是对应一个HDFS文件系统上的独立的文件夹该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE子句中的表达式选择查询所需要的指定的分区这样的查询效率会提高很多。
2.5.1、分区表基本操作 数据 10,ACCOUNTING,NEW YORK 10,ACCOUNTING,NEW YORK 10,ACCOUNTING,NEW YORK 20,RESEARCH,DALLAS 20,RESEARCH,DALLAS 20,RESEARCH,DALLAS 30,SALES,CHICAGO 30,SALES,CHICAGO 1引入分区表需要根据日期对日志进行管理 /opt/hive/warehouse/log_partition/20170702/20170702.log /opt/hive/warehouse/log_partition/20170703/20170703.log /opt/hive/warehouse/log_partition/20170704/20170704.log 2创建分区表语法 create table dept_partition( deptno int, dname string, loc string ) partitioned by (month string) row format delimited fields terminated by ,; 3加载数据到分区表中 load data local inpath /opt/dept.txt into table default.dept_partition partition(month201707’); load data local inpath /opt/dept.txt into table default.dept_partition partition(month201708’); load data local inpath /opt/dept.txt into table default.dept_partition partition(month201709’); 4查询分区表中数据 单分区查询 select * from dept_partition where month201709; 多分区联合查询 select * from dept_partition where month201709 union select * from dept_partition where month201708 union select * from dept_partition where month201707; 注意
Hive 1.2.0之前的版本仅支持UNION ALL其中重复的行不会被删除。
Hive 1.2.0和更高版本中UNION的默认行为是从结果中删除重复的行。
5增加分区 alter table dept_partition add partition(month201706) ; alter table dept_partition add partition(month201705) partition(month201704); 6删除分区 alter table dept_partition drop partition (month201704); alter table dept_partition drop partition (month201705), partition (month201706) 7查看分区表有多少分区 show partitions dept_partition; 8查看分区表结构 desc formatted dept_partition; 2.6、修改表 2.6.1重命名表 语法 ALTER TABLE table_name RENAME TO new_table_name 实例 alter table dept_partition2 rename to dept_partition3; 2.6.2增加/修改/替换列信息 语法 更新列 ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name] 增加和替换列 ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...) 注ADD是代表新增一字段字段位置在所有列后面(partition列前)REPLACE则是表示替换表中所有字段。
案例 添加列 alter table dept_partition add columns(deptdesc string); 更新列 alter table dept_partition change column deptdesc desc int; 替换列 alter table dept_partition replace columns(deptno string, dname string, loc string); 2.6.3删除表
drop table dept_partition; 注意外部表不能简单的通过这个命令删除这个命令只能删除外部表的元数据没有办法删除hdfs上面的数据如果需要将外部表彻底删除有以下方法 方案一转换为内部表再删除 ALTER TABLE xxx SET TBLPROPERTIES(EXTERNALFalse); drop table xxx; 方案二删除元数据然后使用hdfs删除数据 3、DML数据操作 3.1 数据导入 3.1.1 向表中装载数据Load 语法 hive load data [local] inpath 路径 [overwrite] into table 表名 [partition (partcol1val1,…)]; 1 1load data:表示加载数据
2local:表示从本地加载数据到hive表否则从HDFS加载数据到hive表
3inpath:表示加载数据的路径
4overwrite:表示覆盖表中已有数据否则表示追加
5into table:表示加载到哪张表
6表名:表示具体的表
7partition:表示上传到指定分区
3.1.2 通过查询语句向表中插入数据Insert
案例 基本插入 insert into table student partition(month201709) values(1,wangwu); insert overwrite table student partition(month201708) select id, name from student where month201709; 多插入 from dept_partition insert overwrite table dept_partition partition(month201707) select deptno,dname,loc where month201709 insert overwrite table dept_partition partition(month201706) select deptno,dname,loc where month201709; 3.1.3 查询语句中创建表并加载数据As Select 根据查询结果创建表查询的结果会添加到新创建的表中
create table if not exists student3 as select id, name from student; 3.1.4 创建表时通过Location指定加载数据路径 创建表并指定在hdfs上的位置
create table if not exists student5( id int, name string) row format delimited fields terminated by \t location /user/hive/warehouse/student5; 上传数据到hdfs上
dfs -put /opt/datas/student.txt /opt/hive/warehouse/student5; 3.1.5 Import数据到指定Hive表中 注意先用export导出后再将数据导入。
import table student2 partition(month201709) from /opt/hive/warehouse/export/student; 3.2 数据导出 3.2.1 Insert导出 1将查询的结果导出到本地
insert overwrite local directory /opt/datas select * from dept_partition; 2将查询的结果格式化导出到本地
insert overwrite local directory /opt/datas/dept1 row format delimited fields terminated by | select * from dept_partition; 3将查询的结果导出到HDFS上(没有local)
insert overwrite directory /opt/datas/dept row format delimited fields terminated by | select * from dept_partition; 3.2.2 Hadoop命令导出到本地 dfs -get /opt/hive/warehouse/employee/employee.txt /opt/datas/dept2/dept.txt; 3.2.3 Hive Shell 命令导出 基本语法hive -f/-e 执行语句或者脚本 file
hive -e select * from hivetest.dept_partition; /opt/datas/dept3/dept.txt; 注意需要在shell窗口执行需要库名.表名需要本地文件夹存在。 3.2.4 Export导出到HDFS上 export table hivetest.dept_partition to /opt/datas/dept2; 3.3 清除表中数据Truncate 注意Truncate只能删除管理表不能删除外部表中数据
truncate table student;