Hive 3 物化视图学习总结

1 Hive中的事务表

参考网站:hive完整介绍文档 — Apache Hive 3 tables

在Hive中,内表(managed table)就是具有ACID特性的表,可以不限制其transactions,也可以限制为insert-only。外表就是非事务表。

下表的表头分别是:Hive可支持的表类型,是否支持ACID特性,存储格式要求,是否支持UPDATE/DELETE操作。尽管某些表类型不能使用UPDATE/DELETE操作,但所有类型的表都能使用drop partition删除数据。
在这里插入图片描述

  • 原文
    You can create ACID (atomic, consistent, isolated, and durable) tables for unlimited transactions or for insertonly transactions. These tables are Hive managed tables. Alternatively, you can create an external table for nontransactional use. Because Hive control of the external table is weak, the table is not ACID compliant.
    在这里插入图片描述

2 物化视图的更新:增量更新、全量更新、定期重建。

参考网站:
[1] Materialized_views:创建、查询重写、刷新
[2] ALTER MATERIALIZED VIEW REBUILD:包含刷新
[3] Materialized view commands:包含创建、刷新、删除

2.1 增量更新(默认情况)

当物化视图满足一定条件时,默认会执行增加刷新,即只刷新原始源表中的变动会影响到的数据,增量刷新会减少重建步骤的执行时间。
要执行增量刷新,物化视图的创建语句和更新源表的方式都须满足一定条件:

  1. 物化视图须满足下列条件:
    (1)物化视图只使用了事务表,ACID或micromanaged都可以。
    (2)如果物化视图中包含Group By,则该物化视图必须存储在ACID表中,因为它需要支持MERGE操作。对于由Scan-Project-Filter-Join组成的物化视图,不存在该限制。
  2. 更新源表时:
    若只用insert更新源表数据,可以对物化视图进行增量更新。若使用了update、insert更新了源表数据,那么只能进行重建,即全量更新(full rebuild)。
  • 原文
    Materialized view maintenance
    When data in the source tables used by a materialized view changes, e.g., new data is inserted or existing data is modified, we will need to refresh the contents of the materialized view to keep it up-to-date with those changes. Currently, the rebuild operation for a materialized view needs to be triggered by the user. In particular, the user should execute the following statement:
    .
    ALTER MATERIALIZED VIEW [db_name.]materialized_view_name REBUILD;
    Hive supports incremental view maintenance, i.e., only refresh data that was affected by the changes in the original source tables. Incremental view maintenance will decrease the rebuild step execution time. In addition, it will preserve LLAP cache for existing data in the materialized view.
    .
    By default, Hive will attempt to rebuild a materialized view incrementally, falling back to full rebuild if it is not possible. Current implementation only supports incremental rebuild when there were INSERT operations over the source tables, while UPDATE and DELETE operations will force a full rebuild of the materialized view.
    .
    To execute incremental maintenance, following conditions should be met:
    (1) The materialized view should only use transactional tables, either micromanaged or ACID.
    (2) If the materialized view definition contains a Group By clause, the materialized view should be stored in an ACID table, since it needs to support MERGE operation. For materialized view definitions consisting of Scan-Project-Filter-Join, this restriction does not exist.
    .
    A rebuild operation acquires an exclusive write lock over the materialized view, i.e., for a given materialized view, only one rebuild operation can be executed at a given time.
  • 译文
    物化视图维护
    当实例化视图使用的源表中的数据发生更改(例如,插入新数据或修改现有数据)时,我们将需要刷新实例化视图的内容,以使其与这些更改保持最新。当前,物化视图的重建操作需要由用户触发。用户尤其应执行以下语句:
    ALTER MATERIALIZED VIEW [db_name.]materialized_view_name REBUILD;
    Hive支持增量视图维护,即仅刷新受原始源表中的更改影响的数据。增量视图维护将减少重建步骤的执行时间。此外,它将为实例化视图中的现有数据保留LLAP缓存。
    默认情况下,Hive将尝试以增量方式重建物化视图,如果不可能,则回落到完全重建。当前实现仅INSERT 在对源表进行操作时才支持增量重建 ,而 UPDATE 和 DELETE 操作将强制对物化视图进行完全重建。
    要执行增量维护,应满足以下条件:
    (1)物化视图应仅使用微管理表或ACID事务表。
    (2)如果实例化视图定义包含Group By子句,则该实例化视图应存储在ACID表中,因为它需要支持MERGE操作。对于由Scan-Project-Filter-Join组成的实例化视图定义,此限制不存在。

重建操作将在物化视图上获取排他写锁定,即,对于给定的物化视图,在给定的时间只能执行一个重建操作。

2.2 定期更新

若物化视图包含旧旧数据,没有更新,那么不会启动自动重写。
如果物化视图使用的是非事务表,那么无法判断数据是否过时,然而我们还希望,优化器会对查询进行自动重写。
这时可以通过SET hive.materializedview.rewriting.time.window=10min;设置定期刷新。
这个参数也可以作为建表语句的一个属性,在建表时设置。

  • 原文
    Materialized view lifecycle
    By default, once a materialized view contents are stale, the materialized view will not be used for automatic query rewriting.
    .
    However, in some occasions it may be fine to accept stale data, e.g., if the materialized view uses non-transactional tables and hence we cannot verify whether its contents are outdated, however we still want to use the automatic rewriting. For those occasions, we can combine a rebuild operation run periodically, e.g., every 5minutes, and define the required freshness of the materialized view data using the hive.materializedview.rewriting.time.window configuration parameter, for instance:
    SET hive.materializedview.rewriting.time.window=10min;
    The parameter value can be also overridden by a concrete materialized view just by setting it as a table property when the materialization is created.
  • 译文
    物化视图生命周期
    默认情况下,一旦物化视图的内容过期,该物化视图将不用于自动查询重写。
    但是,在某些情况下,可以接受陈旧的数据,例如,如果物化视图使用非事务表,因此我们无法验证其内容是否已过时,但是我们仍想使用自动重写。在这种情况下,我们可以结合定期运行(例如每5分钟一次)的重建操作,并使用hive.materializedview.rewriting.time.window 配置参数定义实例化视图数据的所需新鲜度 ,例如:
    SET hive.materializedview.rewriting.time.window=10min;
    只要在创建实例化时将参数值设置为表格属性,也可以通过具体的实例化视图覆盖该参数值。

3 查询重写

参考网址:Materialized views — Materialized view-based query rewriting
创建物化视图后,优化器将能够利用其定义语义来使用物化视图自动重写传入的查询,从而加快查询的执行速度。
可以使用hive.materializedview.rewriting配置属性全局启用和禁用重写算法 (默认值为 true)。此外,用户可以有选择地启用/禁用实例化视图以进行重写。回想一下,默认情况下,实例化视图已启用,可在创建时进行重写。若要更改该行为,可以使用以下语句:
ALTER MATERIALIZED VIEW [db_name.]materialized_view_name ENABLE|DISABLE REWRITE;

示例:
例子1
考虑以下DDL语句创建的数据库模式:

CREATE TABLE emps (
  empid INT,
  deptno INT,
  name VARCHAR(256),
  salary FLOAT,
  hire_date TIMESTAMP)
STORED AS ORC
TBLPROPERTIES ('transactional'='true');

CREATE TABLE depts (
  deptno INT,
  deptname VARCHAR(256),
  locationid INT)
STORED AS ORC
TBLPROPERTIES ('transactional'='true');

假设我们要经常获取有关2016年之后按不同时期粒度聘用的员工及其部门的信息。我们可以创建以下物化视图:

CREATE MATERIALIZED VIEW mv1
AS
SELECT empid, deptname, hire_date
FROM emps JOIN depts
  ON (emps.deptno = depts.deptno)
WHERE hire_date >= '2016-01-01';
然后,以下查询提取有关Hive 2018年第

一季度雇用的员工的信息:

SELECT empid, deptname
FROM emps
JOIN depts
  ON (emps.deptno = depts.deptno)
WHERE hire_date >= '2018-01-01'
    AND hire_date <= '2018-03-31';

Hive将能够使用实例化视图重写传入的查询,包括在实例化扫描之上的补偿谓词。尽管重写发生在代数级别,但为了说明此示例,我们包含与mv Hive用来回答传入查询的重写等效的SQL语句 :

SELECT empid, deptname
FROM mv1
WHERE hire_date >= '2018-01-01'
    AND hire_date <= '2018-03-31';
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张之海

若有帮助,客官打赏一分吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值