On this page
Dependent Tables
Hive 支持分区和未分区的外部表。在这两种情况下,当添加新表/分区时,也会为新表/分区指定位置。让我们考虑一个具体的例子:
创建按(ds 字符串,hr 字符串)划分的表 T(键字符串,值字符串);
插入覆盖表 T 分区(ds ='1',hr ='1')...;
..
插入覆盖表 T 分区(ds ='1',hr = '24')...;
T 是按日期和小时划分的表,而 Tsignal 是一个外部表,从概念上讲它表示 signal 表的创建。
创建由(ds 字符串)分区的外部表 Tsignal(键字符串,值字符串);
当一天中创建所有小时分区(ds ='1')时,可以将相应的分区添加到 Tsignal
修改表 Tsignal 添加分区(ds ='1')的位置'T 的位置'/ ds = 1;
Tsignal @ ds = 1 和 T @ ds = 1/hr = 1,T @ ds = 1/hr = 2,...之间存在隐式依赖性。T @ ds = 1/hr = 24,但是这种依赖性是没有被捕获的任何地方
在 metastore 中。能够显式创建该依赖关系将很有用。此依赖项可用于各种审核目的。例如。当执行以下查询时:
从 Tsignal 中选择..,其中 ds ='1';
Importing 仅包含 Tsignal @ ds = 1,但也应包含 T @ ds = 1/hr = 1,T @ ds = 1/hr = 2,.... T @ ds = 1/hr = 24
此依赖项应由元存储库捕获。为简单起见,让我们假设我们创建了一个新的依赖表概念(而不是重载外部表)。
创建由(ds 字符串)划分的依赖表 Tdependent(键字符串,值字符串);
这就像一个外部表,但也捕获了依赖性(我们也可以增强外部表的依赖性)。
更改表 T 依赖的添加分区(ds ='1')位置'/ T/ds = 1'依赖的分区表 T 分区(ds ='1');
指定从属分区的部分分区规范。
请注意,每个表可以指向不同的位置-配置单元需要确保所有从属分区都在“ T/ds = 1”位置下
- 指定位置
元存储可以完全或部分存储依赖项。
-
- 双向实现依赖
Tdependent @ ds = 1 取决于 T @ ds = 1/hr = 1 到 T @ ds = 1/hr = 24
T @ ds = 1/hr = 1 取决于 T @ ds = 1
优点:如果丢弃了 T @ ds = 1/hr = 1,则可以通知 T @ ds = 1,或者可以选择禁用
Tdependent 上的任何属性都可以传播到 T
- 双向实现依赖
-
依赖项用于查询吗?如果添加 T @ ds = 1/hr = 25 会怎样?查询“从 Tdependent 那里 ds = 1 的 select ..”包括 T @ ds = 1/hr = 25,但这未在 Importing 中显示。
不要使用位置进行查询-那么为什么要使用该位置?
-
- 存储部分依赖
Tdependent @ ds = 1 取决于 T @ ds = 1(规范)。
在描述时,对规范进行评估,并动态计算所有从属分区。在添加分区时,请验证该位置是否捕获了所有相关分区。
部分规范不用于查询,而是用于位置。在查询时,请验证该位置是否捕获了所有相关分区。
- 存储部分依赖
从属表没有位置。
分区列表是在查询时计算的-将其视为一个视图,其中每个分区都有自己的定义,仅限于'select * from T from 局部/完全分区规范'。查询层需要更改。可能吗 ?与视图不同,它不会在语义分析时重写。分区修剪完成后(在从属表上),重写
包含基础表 T 的树-列保持不变,因此应该是可能的。
这样,分区可能指向不同的表。
For eg:
alter table Tdependent 添加分区(ds ='1')取决于表 T1 分区(ds ='1');
alter table Tdependent 添加分区(ds ='2')取决于表 T2 分区(ds ='2');
当前可以通过外部表实现的功能。从属分区是动态计算的-T1 @ ds = 1/hr = 1 不知道它由 Tdependent @ ds = 1 所依赖的事实。
T1 @ ds = 1/hr = 1 可以随时删除,从那时起,Tdependent @ ds = 1 会自动停止运行。
我倾向于这一点-用户无需同时指定位置和相关分区。
可以增强外部表来支持此功能吗?由于外部表今天的处理方式不同,这将为查询层造成问题。
-
- 相关分区列表已具体化并存储在 metastore 中,并用于查询。
像'select T.select from from Tdependent where ds = 1'的查询被转换为'select .. from(select * from T where(ds = 1 and hr = 1)或(ds = 1 and hr = 2).. ..或(ds = 1 和 hr = 24))'
会给查询层带来很多负担。
- 相关分区列表已具体化并存储在 metastore 中,并用于查询。
=最终提案=
无需增强外部表来解决复杂的用例,而是创建一种新的表类型-依赖表。存在外部表的原因是指向一些现有数据。
==从属表==
创建一个表,该表显式依赖于另一个表。对于提到的第一个用例,请考虑以下情形:
create table T (key string, value string) partitioned by (ds string, hr string);
-- create a dependent table which specifies the dependencies explicitly
-- Tdep inherits the schema of T
-- Tdep is partitioned by a prefix of T (either ds or ds,hr)
create dependent table Tdep partitioned by (ds string) depends on table T;
为了表示 T 的每日分区的结束,在 Tdep 中添加了相应的分区
-- create partition T@ds=1/hr=1 to T@ds=1/hr=24
alter table Tdep add partition (ds=1);
元存储存储以下依赖项
Tdep 取决于 T,而 T 取决于 Tdep
Tdep @ ds = 1 取决于 T @ ds = 1
在某些情况下,Tdep 的某些分区可能取决于 T,而 Tdep 的较新分区则取决于 T2.
create table T (key string, value string) partitioned by (ds string, hr string);
-- create a dependent table which specifies the dependencies explicitly
-- Tdep inherits the schema of T
-- Tdep is partitioned by a prefix of T (either ds or ds,hr)
create dependent table Tdep partitioned by (ds string) depends on table T;
-- create partition T@ds=1/hr=1 to T@ds=1/hr=24
alter table Tdep add partition (ds=1);
-- repeat this for T@ds=1 to T@ds=10
-- T is being deprecated, and T2 is the new table (with the same schema, possibly partitioned on different keys)
create table T2 (key string, value string) partitioned by (ds string, hr string, min string);
alter table Tdep depends on table T2;
-- create partition T2@ds=11/hr=1/min=1 to T2@ds=11/hr=24/min=60
alter table Tdep add partition (ds=1);
元存储存储以下依赖项
Tdep 取决于(T,T2),而(T,T2)取决于 Tdep
T 和 T2 具有相同的架构
Tdep @ ds = 1..Tdep @ ds = 10 分别取决于 T @ ds = 1..T @ ds = 10
Tdep @ ds = 11 取决于 T2 @ ds = 11
Tdep 上的查询被重写以访问基础表。例如。对于查询'来自 ds = 1 的 Tdep 的 select count(1)',一旦分区修剪完成(在 Tdep 上),操作员树(TableScan(Tdep):ds = 1-> Select)将被重写为(TableScan(T ):ds = 1/hr = 1 到 ds = 1/hr = 24->选择)。与外部表相比,此方法的优点是使用了基础表的所有表属性(存储/排序/列表存储)。这可以轻松扩展到多个表。例如。对于查询'从 ds = 1 或 ds = 11 的 Tdep 中选择 count(1)',一旦完成分区修剪(在 Tdep 上),操作员树(TableScan(Tdep):ds = 1,ds = 11->选择)被重写为(TableScan(T):ds = 1/hr = 1 至 ds = 1/hr = 24->选择->联合)和(TableScan(T2):ds = 1/hr = 1 至 ds = 1/hr = 24->选择->联合)。
如果 T 的模式发生更改,则可以禁止它,也可以将其传播到 Tdep。
desc extended 将得到增强,以显示所有相关分区。
可以更改现有分区的依赖性:alter table Tdep 分区(ds = 10)取决于表 T2