Monday, January 27, 2014

Executing Hive Scripts

Step 1: Writing a Hive script.

To write the Hive Script the file should be saved with .sql extension. Open a terminal in your Cloudera CDH4 distribution and give the following command to create a Hive Script.
Command: sudo gedit sample.sql
how to run hive scripts

On executing the above command, it will open the file with the list of all the Hive commands that need to be executed.
In this script,  a table will be created, described and data will be loaded and retrieved from the table.

1. To create the table in Hive:

Command: create table product ( productid: int, productname: string, price: float, category: string) rows format delimited fields terminated by ‘,’ ;
Here, product is the table name and { productid, productname, price, category} are the columns of this table.
Fields terminated by ‘,’ indicate that the columns in the input file are separated by the symbol ‘,’.
By default the records in the input file are separated by a new line.

2. Describing the table:

Command: describe product;

3. Loading the data into the table.

To load the data into the table first we need to create an input file which contains the records that need to be inserted in the table.
Let us create an input file. The command is:
Command: sudo gedit input.txt
how to run hive scripts
Edit the contents in the file as shown in the figure.
how to run hive scripts

4. Retrieving the data:

To retrieve the data, the select command is used.
Command: Select * from product;
The above command is used to retrieve the value of all the columns present in the table. The script should be like as it is shown in the below image.
Now we are done with writing the Hive script.  The file sample.sql  can now be saved.
how to run hive scripts

Step 2: Running the Hive Script

The following is the command to run the Hive script:
Command: hive –f /home/cloudera/sample.sql
how to run hive scripts
While executing the script, make sure that  the entire path of the location of the Script file is present.
We can see that all the commands are executed successfully.
how to run hive scripts
This is how Hive scipts are run and executed in CDH4.

0 comments:

Post a Comment