Microsoft Azure Data Engineering Certificatio ...
- 14k Enrolled Learners
- Weekend/Weekday
- Live Class
Being a Data Warehousing package built on top of Hadoop, Apache Hive is increasingly getting used for data analysis, data mining and predictive modeling. Organizations are looking for professionals with a firm hold on Hive & Hadoop skills. In this post, let’s look at how to run Hive Scripts. In general, we use the scripts to execute a set of statements at once. Hive Scripts are used pretty much in the same way. It will reduce the time and effort we put on to writing and executing each command manually.
Hive Scripts are supported in the Hive 0.10.0 and above versions. As Hive 0.90 version is installed in CDH3, we cannot run Hive Scripts in CDH3. You can try the below steps in CDH4 as it has Hive 0.10.0 version installed in them. Are you aware of how to create a Hive script? If no, click here to gain more clarification.
Now, let us see how to write the scripts in Hive and run them in CDH4:
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
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. Creating 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;
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.
Command: sudo gedit input.txt
Edit the contents in the file as shown in the figure.
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.
The following is the command to run the Hive script:
Command: hive –f /home/cloudera/sample.sql
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.
This is how Hive scripts are run and executed in CDH4.
Hive is a critical component of Hadoop and your expertise in Hive can land you top-paying Hadoop jobs! Edureka has a specially curated Hadoop course that helps you master concepts such as MapReduce, Yarn, Pig, Hive, HBase, Oozie, Flume and Sqoop. Click on the button below to get started.
Learn more about Big Data and its applications from the Data Engineer Course.
Got a question for us? Please mention them in the comments section and we will get back to you.
Related Posts:
edureka.co
Lets say, i have multiple databases and out of which i have to execute the above statement into one database. For that how and where we need to mentioned in the script.
As i have 3 databases ( default, books, assignment ) and i want to execute all the commands in the assignment database.
I have created two scripts ( 1. nyse_scripts.sh and 2. nyse_scripts.hql ) which includes the commands as shown in the screen shot attached. While i am executing the script in the command mode in the path of the script : sh -x nyse_scripts.sh
then the script is failing while loggin to the assignment database https://uploads.disquscdn.com/images/2b3de8503e8655585f7ca56d0a196ffc267910ce0eba981ec288b3d8d6ec026f.png https://uploads.disquscdn.com/images/a8018715aa52904c4ffb644ea0ea8cfd02e4b6718ff7eab6f644adac623bb4ee.png https://uploads.disquscdn.com/images/1335a4c7be7195623709d9b57b3f27ec89e7b0d136400ad5e8cd93180b2dbe48.png
Please suggest…
FAILED: SemanticException The current builtin authorization in Hive is incomplete and disabled.
I would suggest using the extension .hql instead of .sql, since HiveQL or HQL is not exactly the same as any standardization of SQL, AFAIK. Identifying the script as HQL would give the reader a hint “not your daddy’s SQL”.