Hive 分区表和内外表

2022年3月5日19:50:02 发表评论 651 views

外部表

1)理论

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

2)管理表和外部表的使用场景:

每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表。

3)案例实操

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

(1)原始数据

(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';

(3)查看创建的表

hive (default)> show tables;

OK

tab_name

dept

emp

(4)向外部表中导入数据

导入数据

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept;

hive (default)> load data local inpath '/opt/module/datas/emp.txt' into table default.emp;

查询结果

hive (default)> select * from emp;

hive (default)> select * from dept;

(5)查看表格式化数据

hive (default)> desc formatted dept;

Table Type:             EXTERNAL_TABLE

4.6.1 分区表基本操作

1)引入分区表(需要根据日期对日志进行管理)

/user/hive/warehouse/log_partition/20180702/20180702.log

/user/hive/warehouse/log_partition/20180703/20180703.log

/user/hive/warehouse/log_partition/20180704/20180704.log

2)创建分区表语法

hive (default)> create table dept_partition(

deptno int, dname string, loc string

)

partitioned by (month string)

row format delimited fields terminated by '\t';

3)加载数据到分区表中

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201809');

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201808');

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201807');

4)查询分区表中数据

单分区查询

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

多分区联合查询(查出三份)

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

union

select * from dept_partition where month='201808'

union

select * from dept_partition where month='201807';

 

_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

5)增加分区

创建单个分区

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

同时创建多个分区

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

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

6)删除分区

删除单个分区

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

同时删除多个分区

hive (default)> alter table dept_partition drop partition (month='201805'), partition (month='201806');

7)查看分区表有多少分区

hive>show partitions dept_partition;

8)查看分区表结构

hive>desc formatted dept_partition;

 

# Partition Information

# col_name              data_type               comment

month                   string

4.6.2 分区表注意事项

1)创建二级分区表

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

2)正常的加载数据

(1)加载数据到二级分区表中

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition2 partition(month='201809', day='13');

(2)查询分区数据

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

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

(1)方式一:上传数据后修复

上传数据

hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=12;

hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=12;

查询数据(查询不到刚上传的数据)

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

执行修复命令

hive>msck repair table dept_partition2;

再次查询数据

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

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

上传数据

hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=11;

hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=11;

执行添加分区

hive (default)> alter table dept_partition2 add partition(month='201809', day='11');

查询数据

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

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

创建目录

hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=10;

上传数据(会出现多份数据,需要添加overwrite关键字, overwrite table tablename

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201809',day='10');

查询数据

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

4.7 修改表

4.7.1 重命名表

(1)语法

ALTER TABLE table_name RENAME TO new_table_name

(2)实操案例

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

4.7.2 增加和删除表分区

详见4.6.1分区表基本操作。同上

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

1)语法

更新列

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则是表示替换表中所有字段。

2)实操案例

(1)查询表结构

hive>desc dept_partition;

(2)添加列

hive (default)> alter table dept_partition add columns(deptdesc string);

(3)查询表结构

hive>desc dept_partition;

(4)更新列

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

(5)查询表结构

hive>desc dept_partition;

(6)替换列

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

(7)查询表结构

hive>desc dept_partition;

4.8 删除表

hive (default)> drop table dept_partition;

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: