当前位置: 首页>数据库>正文

Hive(四)—— DDL数据定义


文章目录

  • 1. 创建数据库
  • 2. 修改数据库信息
  • 3. 查询数据库
  • 3.1 显示数据库
  • 3.2 查看数据库详情
  • 3.3 切换库
  • 4. 删除库
  • 5. 创建表
  • 5.1 普通创建
  • 5.2 根据查询结果创建表(查询的结果会添加到新创建的表中)
  • 5.3 根据已经存在的表结构创建表(不复制数据)
  • 6. 分区表
  • 6.1 定义
  • 6.2 创建分区表语法
  • 6.3 加载数据到分区表中
  • 6.4 分区查询
  • 6.5 增加分区
  • 6.6 删除分区
  • 6.7 查看分区表有多少分区
  • 6.8 查看分区表结构
  • 6.9 二级分区表
  • 6.10 把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
  • 7. 修改表
  • 7.1 重命名表
  • 7.2 增加/修改/替换列信息
  • 8. 删除表


1. 创建数据库

create database db_hive;
create database if not exists db_hive;
create database db_hive2 location '/db_hive2.db'; # hdfs根目录下

2. 修改数据库信息

数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置

hive(default)> alter database db_hive set dbproperties('createtime'='20190506');

3. 查询数据库

3.1 显示数据库

hive(default)> show databases;
hive(default)> show databases like 'db_hive*';

3.2 查看数据库详情

hive(default)> desc database db_hive;
hive(default)> desc database extended db_hive;

3.3 切换库

hive(default)> use db_hive;

4. 删除库

hive(default)> drop database db_hive2;
hive(default)> drop database if exists db_hive2;
hive(default)> drop database db_hive cascade; # 如果数据库不为空,可以采用cascade命令,强制删除

5. 创建表

(记住顺序) :字段 - comment - partitioned - clustered - terminated - stored - location
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, …)
[songRTED BY (col_name [ASC|DESC], …)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]

STORED AS指定存储文件类型
SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)
如果文件数据是纯文本,可以使用STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。

外部表:
因为表是外部表,所以Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。在建表的同时指定一个指向实际数据的路径(LOCATION)

内部表(管理表):
默认创建的表都是所谓的管理表,有时也被称为内部表。因为这种表,Hive会(或多或少地)控制着数据的生命周期。Hive默认情况下会将这些表的数据存储在由配置项hive.metastore.warehouse.dir(例如,/user/hive/warehouse)所定义的目录的子目录下。当我们删除一个管理表时,Hive也会删除这个表中数据。管理表不适合和其他工具共享数据。

内部表和外部表的使用场景:

  • 外部表
    比如公司的原始日志,或每天收集到的网站日志,都会存在HDFS的某个目录,多个部门都会对原始数据进行分析,这时建外部表更合适,因为不会被删除。
  • 内部表
    统计分析时用到的中间表或结果表,使用内部表更为合适,因为这些数据不需要共享。
    数据通过SELECT+INSERT进入内部表。

5.1 普通创建

hive(default)> create table if not exists student2(
		id int, name string
	)
	row format delimited fields terminated by '\t'
	stored as textfile
	location '/user/hive/warehouse/student2';

注意:多个表的路径可以一样。

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

这种方式不能创建外部表,只能创建内部表。

hive(default)> create table if not exists student3 as select id, name from student;

5.3 根据已经存在的表结构创建表(不复制数据)

hive(default)> create table if not exists student4 like student;

描述表

hive(default)> desc formatted student2;

6. 分区表

6.1 定义

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。

6.2 创建分区表语法

hive(default)> create table dept_partition_dump(deptno int, dname string, loc string) partitioned by (month string)
row format delimited fields terminated by '\t';

6.3 加载数据到分区表中

hive(default)> load data local inpath '/usr/local/hive-3.1.2/test_files/dept.txt' into table dept_partition partition(month='201907');
hive(default)> load data local inpath '/usr/local/hive-3.1.2/test_files/dept.txt' into table dept_partition partition(month='201908');
hive(default)> load data local inpath '/usr/local/hive-3.1.2/test_files/dept.txt' into table dept_partition partition(month='201909');

6.4 分区查询

单分区查询

hive(default)> select * from dept_partition where month='201907';

多分区联合查询

hive(default)> select * from dept_partition where month='201907'
union
select * from dept_partition where month='201908'
union
select * from dept_partition where month='201909';

底层走的是MapReduce,并不是像MySQL一样的表的关联

_u3.deptno _u3.dname _u3.loc _u3.month
10 ACCOUNTING NEW YORK 201807
10 ACCOUNTING NEW YORK 201808
10 ACCOUNTING NEW YORK 201809
20 RESEARCH DALLAS 201807
20 RESEARCH DALLAS 201808
20 RESEARCH DALLAS 201809
30 SALES CHICAGO 201807
30 SALES CHICAGO 201808
30 SALES CHICAGO 201809
40 OPERATIONS BOSTON 201807
40 OPERATIONS BOSTON 201808
40 OPERATIONS BOSTON 201809

dept.txt:
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700

emp.txt:
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10

6.5 增加分区

创建单个分区

hive(default)> alter table dept_partition add partition(month='201906');

同时创建多个分区

hive(default)>  alter table dept_partition add partition(month='201905') partition(month='201904');

注:增加多个分区之间用空格" “隔开,删除多个分区用”,"隔开

6.6 删除分区

删除单个分区

hive(default)> alter table dept_partition drop partition(month='201904');

同时删除多个分区

hive(default)> alter table dept_partition drop partition(month='201905'), partition(month='201906');

6.7 查看分区表有多少分区

hive(default)> show partitions dept_partition;

6.8 查看分区表结构

hive(default)> desc formatted dept_partition;

6.9 二级分区表

创建

hive(default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';

加载

hive(default)> load data local inpath '/usr/local/hive-3.1.2/test_files/dept.txt' into table dept_partition2 partition(month='201909', day='08');

查询分区数据

hive(default)> select * from dept_partition2 where month='201909' and day='08';

6.10 把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式

方式一:上传数据后修复

# 上传数据
hive(default)> dfs -mkdir -p /user/hive/warehouse/during.db/dept_partition2/month=201909/day=07;
hive(default)> dfs -put /usr/local/hive-3.1.2/test_files/dept.txt /user/hive/warehouse/during.db/dept_partition2/month=201909/day=07;

#查询数据(查询不到刚上传的数据)
hive(default)> select * from dept_partition2 where month='201909' and day='07';

#执行修复命令,再次查询数据可查询到
hivehive(default)> msck repair table dept_partition2;

方式二:上传数据后添加分区

# 上传数据
hive(default)> dfs -mkdir -p /user/hive/warehouse/during.db/dept_partition2/month=201909/day=06;
hive(default)> dfs -put /usr/local/hive-3.1.2/test_files/dept.txt /user/hive/warehouse/during.db/dept_partition2/month=201909/day=06;

# 执行添加分区
hive(default)> alter table dept_partition2 add partition(month='201909', day='06');

# 查询数据
hive(default)> select * from dept_partition2 where month='201909' and day='06';

方式三:上传数据后load数据到分区

# 创建目录
hive(default)> dfs -mkdir -p /user/hive/warehouse/during.db/dept_partition2/month=201909/day=09;

# 上传数据
hive(default)> load data local inpath '/usr/local/hive-3.1.2/test_files/dept.txt' into table dept_partition2 partition(month='201909',day='09');

# 查询数据
hive(default)> select * from dept_partition2 where month='201909' and day='09';

7. 修改表

7.1 重命名表

hive(default)> alter table dept_partition2 rename to dept_partition3;

7.2 增加/修改/替换列信息

更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]

hive(default)> alter table dept_partition change column deptdesc deptno int;

注:在hive的低版本,将字段的类型改变后,如name string -> int,该字段的值都会变成NULL;

但在高版本的hive,当列的类型改变时,会报:
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Unable to alter table. The following columns have types incompatible with the existing columns in their respective positions :name

增加和替换列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], …)

hive(default)> alter table dept_partition add columns(deptdesc string);
hive(default)> alter table dept_partition replace columns(deptno string, dname string, loc string);

注:ADD是代表新增一字段,字段位置在所有列后面(partition列前),REPLACE则是表示替换表中所有字段。

8. 删除表

hive(default)> drop table student2;



https://www.xamrdz.com/database/6kq1926430.html

相关文章: