Chapter 7. HiveQL: Views
A view allows a query to be saved and treated like a table. It is a logical construct, as it does not store data like a table. In other words, materialized views are not currently supported by Hive.
When a query references a view, the information in its definition is combined with the rest of the query by Hive’s query planner. Logically, you can imagine that Hive executes the view and then uses the results in the rest of the query.
Views to Reduce Query Complexity
When a query becomes long or complicated, a view may be used to hide the complexity by dividing the query into smaller, more manageable pieces; similar to writing a function in a programming language or the concept of layered design in software. Encapsulating the complexity makes it easier for end users to construct complex queries from reusable parts. For example, consider the following query with a nested subquery:
FROM(SELECT*FROMpeopleJOINcartON(cart.people_id=people.id)WHEREfirstname='john')aSELECTa.lastnameWHEREa.id=3;
It is common for Hive queries to have many levels of nesting. In the following example, the nested portion of the query is turned into a view:
CREATEVIEWshorter_joinASSELECT*FROMpeopleJOINcartON(cart.people_id=people.id)WHEREfirstname='john';
Now the view is used like any other table. In this query we added a
WHERE clause to the SELECT statement. This exactly emulates the
original query:
SELECTlastnameFROMshorter_joinWHEREid=3;
Views that Restrict ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access