You are on page 1of 7

Installation of Hive in Hadoop-1.

X cluster

1. Downloading the stable version of hive

https://archive.apache.org/dist/hive/hive-0.12.0/hive-0.12.0.tar.gz

2. Unzip hive-0.12.0

mca@mca-VirtualBox:~$tar -zxvf hive-0.12.0.tar.gz

3. Setting - up the path of Hadoop and Hive

mca@mca-VirtualBox:~$ sudo gedit .bashrc

export HIVE_HOME= /home/mca/hive-0.12.0

mca@mca-VirtualBox:~$ source .bashrc


3. Start your Hive

mca@mca-VirtualBox:~$ cd hive-0.12.0
mca@mca-VirtualBox:~/hive-0.12.0$
mca@mca-VirtualBox:~$ cd hive-0.12.0

mca@mca-VirtualBox:~/hive-0.12.0$ bin/hive
Logging initialized using configuration in jar:file:/home/mca/hive-0.12.0/lib/hive-
common-0.12.0.jar!/hive-log4j.properties
Hive>

Working with HIVE Queries


1.Create Database Statement
Create Database is a statement used to create a database in Hive. A database
in Hive is a namespace or a collection of tables.
Hive> CREATE DATABASE [IF NOT EXISTS] userdb;
Ok
The following query is used to verify a databases list:
Hive> SHOW DATABASES;
default
userdb
Hive> SHOW DATABASES LIKE 'u.*';
userdb
2.Drop Database Statement
Drop Database is a statement that drops all the tables and deletes the
database
Hive> DROP DATABASE IF EXISTS userdb;
ok
3. Create a Table statement
The following query creates a table named employee.
hive> create table if not exists employee (eid int,name String,designation
String, salary int)
COMMENT 'Employee detail'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
OK
Time taken: 0.234 seconds
The following query is used to verify a table in a list:
Hive> show tables;
Employee1
OK
Time taken: 0.176 seconds, Fetched: 1 row(s)
4. Displaying structure table ‘employee’
Hive> desc employee;
OK
eid int None
ename string None
designation string None
salary int None

Time taken: 0.048 seconds, Fetched: 4 row(s)

5. Inserting data in a table


To insert the following data into the table. It is a text file named sample.txt in
/home/mca directory.
The following query loads the given text into the table
Hive> LOAD DATA LOCAL INPATH '/home/mca/sample.txt'
> OVERWRITE INTO TABLE employee;
OK
Time taken: 0.337 seconds
6. Displaying data in a table
The following query is executed to retrieve the employee details using the employee
table
Hive> select * from employee;
OK
1201 Raja Asst-Manager 18000
1202 Raul Asst-Manager 18500
1203 Kiran Op-Admin 12500
1204 Robin Programmer 12000
1205 Haja Gen-Admin 8000
Time taken: 0.06 seconds, Fetched: 5 row(s)

6. Altering Structure in a table


The following query renames the table from employee1 to employee.
Hive> ALTER TABLE employee RENAME TO employee1;
OK
Time taken: 3.638 seconds
The following query is used to verify a table in a list:

Hive> show tables;


Employee1
OK
Time taken: 0.089 seconds, Fetched: 1 row(s)
The following query rename the column name
Hive> ALTER TABLE employee1 CHANGE name ename String;
OK
Time taken: 0.295 seconds

To show the structure of the employee table

Hive> desc employee1;


OK
eid int None
ename string None
designation string None
salary int None
Time taken: 0.048 seconds, Fetched: 4 row(s)
The following query is executed to retrieve the employee details using the
employee table
Hive> select * from employee1;
OK
1201 Raja Asst-Manager 18000
1202 Raul Asst-Manager 18500
1203 Kiran Op-Admin 12500
1204 Robin Programmer 12000
1205 Haja Gen-Admin 8000
Time taken: 0.102 seconds, Fetched: 5 row(s)

The following query adds a column named dept to the employee table
Hive> ALTER TABLE employee1 ADD COLUMNS (
> dept STRING COMMENT 'Department name');
OK
Time taken: 0.114 seconds

To show the structure of the employee table


Hive> desc employee1;
OK
eid int None
ename string None
designation string None
salary int None
dept string Department name
Time taken: 0.028 seconds, Fetched: 5 row(s)
7. Insert Record
To insert the record for the above structure, from a text file named
sample.txt in /home/mca directory

The following query loads the given text into the table

Hive> LOAD DATA LOCAL INPATH '/home/mca/sample.txt'


> OVERWRITE INTO TABLE employee1;
OK
Time taken: 0.328 seconds
The following query is executed to retrieve the employee details using the
employee table
8. Select Clause
Hive> select * from employee1;
OK
1201 Raja Asst-Manager 18000 HRM
1202 Raul Asst-Manager 18500 TP
1203 Kiran Op-Admin 12500 ADMIN
1204 Robin Programmer 12000 ADMIN
1205 Haja Gen-Admin 8000 ADMIN

Time taken: 0.138 seconds, Fetched: 5 row(s)

The following query is executed to retrieve the employee details whose employee
id=1201.
Hive> select * from employee1 where eid=1201;
1201 Raja Asst-Manager 18000 HRM

Time taken: 25.728 seconds, Fetched: 1 row(s)

The following query is executed to retrieve the employee details whose salary is
more than or equal to Rs 10000.
9. Select and Where Clause
Hive> SELECT * FROM employee1 WHERE Salary>=10000;
1201 Raja Asst-Manager 18000 HRM
1202 Raul Asst-Manager 18500 TP
1203 Kiran Op-Admin 12500 ADMIN
1204 Robin Programmer 12000 ADMIN

Time taken: 9.305 seconds, Fetched: 4 row(s)


The following query is used to retrieve employee details whose Department is
ADMIN and Salary is more than Rs 12000.

Hive> select * from employee1 where salary>12000 and dept='ADMIN';

1203 Kiran Op-Admin 12500 ADMIN

Time taken: 7.378 seconds, Fetched: 1 row(s)


10. Order By Clause

Generate a query to retrieve the employee details in order by using salary

Hive> SELECT * from employee1 order by salary;

1205 Haja Gen-Admin 8000 ADMIN


1204 Robin Programmer 12000 ADMIN
1203 Kiran Op-Admin 12500 ADMIN
1201 Raja Asst-Manager 18000 HRM
1202 Raul Asst-Manager 18500 TP
Time taken: 39.569 seconds, Fetched: 5 row(s)

You might also like