View and Index in Hive

Tram Ho

This is the last post in the Hive series, I will release some more about HiveQL soon, everyone follow along.

View

This article describes how to create and manage views. Views are created based on user requests. You can save any result set data as a view. Using view in Hive is like in SQL. This is a standard RDBMS concept. We can perform all DML operations on one view.

Like in SQL, a VIEW view is a virtual table in a database whose content is defined through a certain SQL statement. A VIEW consists of rows and columns just like a real table. The fields in a view are fields from one or more actual tables in the Database.

The difference between a VIEW view and a table is that VIEW is not considered a data storage structure that exists in a database. In essence, the data observed in VIEW is retrieved from tables via data query statements and is used to restrict database access or to hide complex data.

Create view

You can create a view at the time of executing a SELECT statement. The syntax is as follows:

For example

Let’s get an example view. Assume the employee table is given below, with the fields Id, Name, Salary, Designation and Dept. Create a query to get details about employee salaries higher than Rs 30,000. We store the result in a view named emp_30000

The following query retrieves details of the employee using the above scenario:

Delete view

Use the following syntax to delete a view:

The following query will delete a view named emp_30000:

Index

Create the index

An index is nothing but a pointer on a specific column of the table. Creating an index means creating a cursor on a specific column of the table. Its syntax is as follows:

For example

Let’s take an example of an index. Use the same employee table as above with the fields Id, Name, Salary, Designation, Dept. Create an index named index_salary on the salary column of the employee table.

The following query creates an index:

It is a pointer on the salary column. If the column is changed, the change is stored using an index value.

Delete the index

The following syntax is used to delete the index named index_salary:

The following query deletes an index named index_salary:

Share the news now

Source : Viblo