Article Directory
View and side view of Hive
Hive view
What is a view
1. Simplify the logical structure of the query by hiding subqueries, connections and functions
2. Virtual table, select data from real table
3. Only save the definition, not the data
4. If you delete or change the underlying table, the query view will fail
5. The view is read-only, you cannot insert or load data
Advantages of views
1. Provide specific columns to users to protect data privacy
2. Scenarios with complex query statements
View syntax
View operation commands: CREATE, SHOW, DROP, ALTER
CREATE VIEW view_name AS SELECT statement; -- 创建视图
-- 创建视图支持 CTE, ORDER BY, LIMIT, JOIN, etc.
SHOW TABLES; -- 查找视图 (SHOW VIEWS 在 hive v2.2.0之后)
SHOW CREATE TABLE view_name; -- 查看视图定义
DROP view_name; -- 删除视图
ALTER VIEW view_name SET TBLPROPERTIES ('comment' = 'This is a view');
--更改视图属性
ALTER VIEW view_name AS SELECT statement; -- 更改视图定义,
Side view of Hive
1. It is often used in combination with the table generation function to connect the input and output of the function
2. OUTER keyword: results will be generated even if output is empty
select name,work_place,loc from employee lateral view outer explode(split(null,',')) a as loc;
3. Support multiple levels
select name,wps,skill,score from employee
lateral view explode(work_place) work_place_single as wps
lateral view explode(skills_score) sks as skill,score;
4. Usually used to normalize rows or parse JSON
to sum up
view
The attempt in Hive is just a shortcut to the SQL statement
The view in hive is only a logical view, there is no materialized view
Hive view does not support addition, deletion, modification, only query
Hive view, only when the query is actually executed
Side view
Lateral View and UDTF function functions are used together. Each row in the table is connected with each row output by the UDTF function to generate a new virtual table. Field names can be set for the records generated by UDTF. The newly added fields can be used in In sort by, group by and other statements, there is no need to set another layer of sub-queries. The role of Lateral View is to expand the original table data.