HIVE in Hadoop
By Abhishek Sengupta (B14003)
Hive is a component of Hortonworks Data Platform(HDP). Hive provides a SQL-like interface to data stored in HDP. Hive is considered to be more user friendly and is designed for users who are used to using SQL for querying data.
To start, we first download the zip file containing the data sets from the following link.
http://seanlahman.com/files/database/lahman591-csv.zip
Once the zip file is downloaded, next step will be to unzip it. We will be using the Master.csv and Batting.csv files for this example.
The first step would be to upload the files. Since there was an access issue with ambari, we have used the HUE interface to upload the files. On clicking the file browser icon in hue we get the following landing page.
In the user directory, we create another directory by selecting the new directory option from the drop-down. The new directory created will be named admin. Both the Master.csv and Batting.csv files will be uploaded to in the admin directory.
Once we have uploaded the files via the hue interface, we log onto ambari. The landing page of ambari will look like this.
We can view the uploaded files in ambari by selecting the HDFS files option in the tool bar as shown in the following screenshot.
We migrate to the /user/admin directory and we can see that the two csv files are uploaded there.
Now as seen in the screenshot above, we open the hive view by clicking on the hive button. The Hive view provides a user interface to the Hive data warehouse system for Hadoop.
On right is a query editor. A query may span multiple lines. At the bottom there are buttons to Execute the query, Explain the query, Save the query with a name and to open a new Worksheet window for another query.
The first task we will do is create a table to hold the data. We will type the query into the composition area on the right handside. Once you have typed in the query hit the Execute button at the bottom. We use the following query.
create table temp_batting (col_value STRING);
The query does not return any results because at this point we just created an empty table. Once the query has executed we can see the Database Explorer at the left of the composition area and when folding down the default database we will see we have a new table called temp_batting.
The next step would be to load the data file Batting.csv into the table temp_batting. The query for this is
LOAD DATA INPATH '/user/admin/Batting.csv' OVERWRITE INTO TABLE temp_batting;
The above query is execute in the hue as a pig script.
After the loading is complete, we can use the select query to view the data in the table temp_batting.
Now that we have read the data in we can start working with it. The next thing is to extract the data. So first we will type in a query to create a new table called batting to hold the data. That table will have three columns for player_id, year and the number of runs.
create table batting (player_id STRING, year INT, runs INT);
Then we have to extract the data we want from temp_batting and copy it into batting. We will do this with a regexp pattern. A multi line query is required to do this job. The first line of the query creates the table batting. The three regexp_extract calls are going to extract the player_id, year and run fields from the table temp_batting. There should be no spaces in the regular expression pattern.
insert overwrite table batting
SELECT
regexp_extract(col_value, '^(?:([^,]*)\,?){1}', 1) player_id,
regexp_extract(col_value, '^(?:([^,]*)\,?){2}', 1) year,
regexp_extract(col_value, '^(?:([^,]*)\,?){9}', 1) run
from temp_batting;
The following screenshot demonstrates this in the query editor.
Now we can execute a select query and look at the Batting data.
SELECT year, max(runs) FROM batting GROUP BY year;
This query groups the data by year so we can find the highest score for each year. The query first groups all the records by year and then selects the player with the highest runs from each year. The output is shown below.
Now we need to go back and get the player_id(s) so we know who the player(s) was. We know that for a given year we can use the runs to find the player(s) for that year. So we can take the previous query and join it with the batting records to get the final table.
SELECT a.year, a.player_id, a.runs from batting a
JOIN (SELECT year, max(runs) runs FROM batting GROUP BY year ) b
ON (a.year = b.year AND a.runs = b.runs) ;
So this was how we obtained the results using hive.
By Abhishek Sengupta (B14003)
Hive is a component of Hortonworks Data Platform(HDP). Hive provides a SQL-like interface to data stored in HDP. Hive is considered to be more user friendly and is designed for users who are used to using SQL for querying data.
To start, we first download the zip file containing the data sets from the following link.
http://seanlahman.com/files/database/lahman591-csv.zip
Once the zip file is downloaded, next step will be to unzip it. We will be using the Master.csv and Batting.csv files for this example.
The first step would be to upload the files. Since there was an access issue with ambari, we have used the HUE interface to upload the files. On clicking the file browser icon in hue we get the following landing page.
In the user directory, we create another directory by selecting the new directory option from the drop-down. The new directory created will be named admin. Both the Master.csv and Batting.csv files will be uploaded to in the admin directory.
Once we have uploaded the files via the hue interface, we log onto ambari. The landing page of ambari will look like this.
We can view the uploaded files in ambari by selecting the HDFS files option in the tool bar as shown in the following screenshot.
We migrate to the /user/admin directory and we can see that the two csv files are uploaded there.
Now as seen in the screenshot above, we open the hive view by clicking on the hive button. The Hive view provides a user interface to the Hive data warehouse system for Hadoop.
On right is a query editor. A query may span multiple lines. At the bottom there are buttons to Execute the query, Explain the query, Save the query with a name and to open a new Worksheet window for another query.
The first task we will do is create a table to hold the data. We will type the query into the composition area on the right handside. Once you have typed in the query hit the Execute button at the bottom. We use the following query.
create table temp_batting (col_value STRING);
The query does not return any results because at this point we just created an empty table. Once the query has executed we can see the Database Explorer at the left of the composition area and when folding down the default database we will see we have a new table called temp_batting.
LOAD DATA INPATH '/user/admin/Batting.csv' OVERWRITE INTO TABLE temp_batting;
The above query is execute in the hue as a pig script.
After the loading is complete, we can use the select query to view the data in the table temp_batting.
Now that we have read the data in we can start working with it. The next thing is to extract the data. So first we will type in a query to create a new table called batting to hold the data. That table will have three columns for player_id, year and the number of runs.
create table batting (player_id STRING, year INT, runs INT);
Then we have to extract the data we want from temp_batting and copy it into batting. We will do this with a regexp pattern. A multi line query is required to do this job. The first line of the query creates the table batting. The three regexp_extract calls are going to extract the player_id, year and run fields from the table temp_batting. There should be no spaces in the regular expression pattern.
insert overwrite table batting
SELECT
regexp_extract(col_value, '^(?:([^,]*)\,?){1}', 1) player_id,
regexp_extract(col_value, '^(?:([^,]*)\,?){2}', 1) year,
regexp_extract(col_value, '^(?:([^,]*)\,?){9}', 1) run
from temp_batting;
The following screenshot demonstrates this in the query editor.
Now we can execute a select query and look at the Batting data.
SELECT year, max(runs) FROM batting GROUP BY year;
This query groups the data by year so we can find the highest score for each year. The query first groups all the records by year and then selects the player with the highest runs from each year. The output is shown below.
Now we need to go back and get the player_id(s) so we know who the player(s) was. We know that for a given year we can use the runs to find the player(s) for that year. So we can take the previous query and join it with the batting records to get the final table.
SELECT a.year, a.player_id, a.runs from batting a
JOIN (SELECT year, max(runs) runs FROM batting GROUP BY year ) b
ON (a.year = b.year AND a.runs = b.runs) ;
So this was how we obtained the results using hive.














seen
ReplyDelete