Lab - Working with Hive Tables
Deliverable: 6 Screenshot in one word document. I have mentioned where these screenshot are to be taken.
- In the cloudera Quickstart vm. Download the SalesData.csv from Blackboard.
- Open the Hue UI.
- Click Hue menu and select Files → Go to /user/hive/warehouse/
- Click Upload button and select Files. Select the SalesData.csv file that you downloaded from Downloads folder.
- Click Hue menu and select Tables to create a new table
- We will create a table from a file. Click on the + button on top right side of the screen.
- We’ll use a file to create our table. Ensure that File is selected as Type.
- Select the path by selecting the ellipse button. Click Upload a file, and select the /user/hive/warehouse/SalesData.csv
- Click Next. Review Field Names and Field Types listed and click submit.
- Hive will create table using the file you uploaded. Take a look at the newly created table in Hive. Take screenshot 1 here.
- When uploading CSV file to create table it is important to ensure that there is no commas in field data. If there are commas in the data, the table columns in Hive will have wrong data. Custom SerDe (Serializer/Deserializer) code can be used to fix this problem.
- In the cloudera Quickstart vm. Download the SalesData-WithCommas.csv and csv-serde-1.1.2-0.11.0-all.jar from Blackboard.
- Open Hive Menu → Files → Goto folder /user/hive/warehouse
- Click Upload button and select Files. Select the SalesData-WithCommas.csv file that you downloaded from Downloads folder.
- Click Hue menu and select Tables to create a new table
- We will create a table from a file. Click on the + button on top right side of the screen.
- We’ll use a file to create our table. Ensure that File is selected as Type.
- Select the path by selecting the ellipse button. Click Upload a file, and select the /user/hive/warehouse/SalesData-WithCommas.csv
- Click Next. Review Field Name and Field Type and click submit.
- Open the salesdatawithcommas table and check the company name you will notice “Netflix, inc.” has been separated into two columns. Sector and columns after Sector have wrong data. Take screenshot 2 here.
- We will fix this using custom serialization and deserialization. Open Hive Query editor and select all from the salesdatawithcommas table.
- Lets alter the table using a custom serde code. Type the following query, do not execute it yet.
ALTER TABLE salesdatawithcommas SET SERDE ‘com.bizo.hive.serde.csv.CSVSerde’
- Click the session button (the one with gears icon) → Select files → click on ellipses and select the jar file. Close the window and hit the play button to run the query. Take screenshot 3 here.
- Now you should be able to notice that we have fixed our issue with commas in data field.
- The Hive performance can be improved by partitioning the table into multiple tables. Partitions are based on one or more partition column. Let’s create a table that is partitioned by year. Download YearlySales.zip file and extract it.
- Open Hive Editior and use the following script to create the table definitions in Hive. Take screenshot 4 here.
create table sales_all_years (RowID smallint, OrderID int, OrderDate date, OrderMonthYear date, Quantity int, Quote float, DiscountPct float, Rate float, SaleAmount float, CustomerName string, CompanyName string, Sector string, Industry string, City string, ZipCode string, State string, Region string, ProjectCompleteDate date, DaystoComplete int, ProductKey string, ProductCategory string, ProductSubCategory string, Consultant string, Manager string, HourlyWage float, RowCount int, WageMargin float)
partitioned by (yr int)
row format serde ‘com.bizo.hive.serde.csv.CSVSerde’
stored as textfile;
- Click Hue Menu → select Files → go to /user/hive/warehouse/sale_all-years
- Create 4 directories 2009, 2010, 2011 and 2012. Then, upload appropriate csv file into each directory. For example, add SalesData-2009.csv to 2009 folder.
- Open Hive editor and run the following scripts: After it executes, take screenshot 5 here.
-- add the partitions
alter table sales_all_years
add partition (yr=2009)
location '2009/';
alter table sales_all_years
add partition (yr=2010)
location '2010/';
alter table sales_all_years
add partition (yr=2011)
location '2011/';
alter table sales_all_years
add partition (yr=2012)
location '2012/';
- Open the sales_all_year table. You should be able to see the data that you uploaded. Take ScreenShot 6 of the data.
- Submit the screenshots in a word file.
Comments
Post a Comment