HW5 - Work with Hive

Updated 12 Nov 2026

Overview

  • HW5: Work with Hive on Amazon EMR (Homework Assignment 5, 5 marks)
  • Goal: learn how to use Hive to process data
  • Your assignment tasks:
    1. Follow the tutorial steps and capture screen along the way
    2. Complete an exercise
  • Make sure each screenshot includes both the command (SQL) and the result
  • Steps:
    • Prepare EMR with Hive Environment
    • Upload data to Hadoop (EMR)
    • Connect to Hive CLI with ssh
    • Create table and import data
    • Calculate statistics for data quality check
    • Ready for analysis
    • Exercise: Preprocessing and redo statistics again
  • What I did (summary): cloned an EMR cluster (emr-7.9.0, Hadoop 3.4.1, Hive 3.1.3, Hue 4.11.0; 1 primary + 2 core nodes), uploaded 2 days of Bangkok taxi GPS probe data (20180801.csv.out, 20180802.csv.out, ~470 MB) with scp, built a partitioned Hive table, ran data quality statistics, cleaned the data into a new ORC table, re-ran the statistics, and used Hue to plot unique vehicles per hour

Assignment Checklist (What to Submit)

  • 1. Screen Capture of Tutorial Part
    • 1.1 AWS EMR Screen
    • 1.2 Create a table and import data
    • 1.3 Calculate statistics for data quality check
      • 1.3.1 How many data in the system?
      • 1.3.2 How many days of data?
      • 1.3.3 How many data for each day?
      • 1.3.4 How many unique taxis for each day?
      • 1.3.5 Find min, max, avg speed by hour
    • 1.4 Check Hive data files on HDFS
    • 1.5 Check Hive process on YARN
  • 2. Exercise
    • Preprocessing: create taxi_data_orc and insert the cleaned data
    • 2.1 How many data in the system?
    • 2.2 How many days of data?
    • 2.3 How many data for each day?
    • 2.4 How many unique taxis for each day?
    • 2.5 Find min, max, avg speed by hour
    • 2.6 Use Hue to calculate and make graph

0. Hadoop on Cloud: AWS EMR

  • Access the AWS Learner Lab (50 USD credits)
  • Select the recent EMR cluster and click "Clone"
  • EMR will stop after some time or after you stop the lab
  • Make sure to select components:
    • Hadoop, Tez
    • Hive, Hue, Pig
    • Spark, Livy
    • JupyterHub, Zeppelin

Cloning is like "Save As" on last week's cluster — same settings, new machine, so you don't have to tick all the boxes again.


1. Prepare EMR with Hive Environment

  • Wait until status shows Waiting → EMR is ready to use (CAPTURE SCREEN)
  • Use the Primary node public DNS for remote connection
  • EMR also provides a Web GUI for each service on Hadoop — replace the URL with your EMR URL (master-public-dns-name)
  • We normally check:
    • Hadoop HDFS NameNode
    • YARN ResourceManager
    • Hue for Hive access
InterfaceURI
Hadoop HDFS NameNode (EMR 6.x+)http://master-public-dns-name:9870/
Hadoop HDFS DataNode (EMR 6.x+)http://coretask-public-dns-name:9865/
HBasehttp://master-public-dns-name:16010/
Huehttp://master-public-dns-name:8888/
JupyterHubhttps://master-public-dns-name:9443/
Livyhttp://master-public-dns-name:8998/
Spark HistoryServerhttp://master-public-dns-name:18080/
Tezhttp://master-public-dns-name:8080/tez-ui
YARN NodeManagerhttp://coretask-public-dns-name:8042/
YARN ResourceManagerhttp://master-public-dns-name:8088/
Zeppelinhttp://master-public-dns-name:8890/
Flink history server (EMR 5.33+)http://master-public-dns-name:8082/
Gangliahttp://master-public-dns-name/ganglia/
  • Older EMR (pre-6.x) used NameNode :50070 / :50470 and DataNode :50075 / :50475

My Answer — 1.1 AWS EMR Screen

  • The cluster was cloned from the previous EMR cluster and reached the Waiting state (ready to use)
  • emr-7.9.0 with Hadoop, Hive, Hue, JupyterHub, Livy, Pig, Spark, Tez and Zeppelin installed; capacity 1 Primary / 2 Core
  • The primary node public DNS is used for SSH, scp and the web interfaces


2. Upload Data to Hadoop (EMR)

5yc/f14Tqe9XCRu5qCGxv5qo+ug,1,13.83610,100.47647,2017-04-11 09:10:26,0,288,0,1
EO8d8iuEH3vjgNQURcvOM/PxgQo,1,13.82045,100.46629,2018-08-01 00:00:10,0,169,1,0
7D8GUsE79mc8lTjsrfnVPrxRgU0,0,13.72489,100.54439,2018-08-01 00:00:11,10,8,0,1
jO66susXra8+ggJKVz0rfAh0tyA,0,13.72504,100.54420,2018-07-31 23:59:43,12,5,0,1
S94VbUQxHCfO0HyPSOFvY/bK3L0,1,13.81945,100.41950,2018-07-31 23:59:14,0,30,1,0
lFXyziHxuJO+hcyZ5fzWLixxaK0,1,13.72528,100.54395,2018-08-01 00:09:33,0,84,1,0
  • Notice: the very first sample row is dated 2017-04-11 and some are 2018-07-31 even though the file is "20180801" → a hint of the data quality problem found later

Taxi Header Info

  • Vehicle ID: unique vehicle ID
  • GPS Valid: 1 = enough satellites for a GPS fix
  • lat / lon: GPS location up to 5 decimal places
  • timestamp: GPS timestamp (GMT+7)
  • speed: km/h
  • heading: vehicle heading direction [0,360)[0, 360) degrees from North =0= 0
  • for_hire_light: [for taxis] whether the "For Hire" light is ON
    • 1 = light on ⇒ possibly no passenger
    • 0 = light off ⇒ possibly carrying passengers
  • engine_acc: car key status active, can be:
    • 1 (active, data collected every minute)
    • 0 (inactive, data collected every 3 minutes)
  • In the Hive table these are named: vehicle_id, gps_valid, lat, lon, gps_time, speed, heading, meter, engine (so meter = for_hire_light, engine = engine_acc)

Uploading

  • Connect to EMR using FileZilla Client (Windows) or scp (Mac)
  • Specify IP address (Primary node public DNS), port: 22
  • Specify User: hadoop and Private Key (PEM from the AWS Details of the lab learner)
  • Create directory data under /home/hadoop

  • Mac (SCP):
scp -i pem_file file_to_upload hadoop@<primary node public DNS>:/home/hadoop/<path>/
 
# EX:
scp -i pem_file 20180801.csv.out hadoop@<primary node public DNS>:/home/hadoop/data/
scp -i pem_file 20180802.csv.out hadoop@<primary node public DNS>:/home/hadoop/data/
  • Note: the target folder must exist first, e.g. ssh -i pem_file hadoop@<primary node public DNS> "mkdir -p ~/data"

3. Connect to Hive CLI with ssh

  • Connect to EMR with ssh with user: hadoop (for Mac, use the ssh CLI)
  • Run command hive to access the Hive shell
  • Run initial commands:
    • Show header
    • Enable dynamic partition
ssh -i pem_file hadoop@<primary node public DNS>
$ hive
hive> set hive.cli.print.header=true;
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> set hive.exec.dynamic.partition=true;
  • hive.cli.print.header=true → print column names with results
  • hive.exec.dynamic.partition* → needed later for INSERT ... PARTITION (PD) where Hive figures out the partition value from the data

4. Create Table and Import Data

  • Create database and table for storing raw taxi data
  • Database: taxidb
  • Table: taxi_raw_data with partition: date (PDATE)
create database taxidb;
Use taxidb;
 
CREATE TABLE taxi_raw_data (vehicle_id string, gps_valid int, lat double, lon double, gps_time string, speed double, heading int, meter int, engine int)
PARTITIONED BY (PDATE string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
  • Import data
load data local inpath '/home/hadoop/data/20180801.csv.out' overwrite into table taxi_raw_data
partition (pdate='2018-08-01');
 
load data local inpath '/home/hadoop/data/20180802.csv.out' overwrite into table taxi_raw_data
partition (pdate='2018-08-02');
  • Check data (CAPTURE SCREEN)
SELECT PDATE, count(*) as total, count(distinct vehicle_id) as totalvehicle
FROM taxi_raw_data
GROUP BY PDATE
ORDER BY PDATE;

My Answer — 1.2 Create a Table and Import Data

  • My SQL (same as the tutorial, compacted):
CREATE TABLE taxi_raw_data (vehicle_id string, gps_valid int, lat double, lon double,
  gps_time string, speed double, heading int, meter int, engine int)
PARTITIONED BY (PDATE string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
 
load data local inpath '/home/hadoop/data/20180801.csv.out'
  overwrite into table taxi_raw_data partition (pdate='2018-08-01');
load data local inpath '/home/hadoop/data/20180802.csv.out'
  overwrite into table taxi_raw_data partition (pdate='2018-08-02');
pdatetotaltotalvehicle
2018-08-013,181,4595,931
2018-08-022,781,0955,942
  • Observation: matches the expected result in the tutorial

PARTITIONED BY (PDATE) + partition (pdate='2018-08-01') = putting each day's file into its own labelled folder. Note: the label comes from which file it was, not from the actual GPS timestamps inside — that's why the next checks use to_date(gps_time).


5. Calculate Statistics for Data Quality Check (Tutorial 1.3)

  • CAPTURE SCREEN for each result

1.3.1 How many data in the system?

Select count(*) as total from taxi_raw_data;
  • Answer: 5,962,554 records in total (= 3,181,459 + 2,781,095, the sum of both partitions)

1.3.2 How many days of data?

Select count(distinct to_date(gps_time)) as total_day from taxi_raw_data;
  • Answer: 52 different dates — even though only two daily files were imported!

1.3.3 How many data for each day?

Select to_date(gps_time) as dateonly, count(*) as total from taxi_raw_data group by
to_date(gps_time) order by dateonly;
  • Answer (52 rows):
dateonlytotal
1970-01-01118
2015-08-11205
2015-08-12579
2015-08-13271
2016-12-29469
2016-12-30596
2016-12-31142
2017-03-121
2017-04-112
2017-11-172
2017-12-104
2017-12-252
2017-12-312
2018-01-312
2018-02-052
2018-04-052
2018-04-102
2018-04-201
2018-04-218
2018-05-134
2018-05-157
2018-05-187
2018-06-081
2018-06-118
2018-06-15320
2018-06-169
2018-06-181
2018-07-022
2018-07-051
2018-07-072
2018-07-1113
2018-07-161
2018-07-222
2018-07-231
2018-07-245
2018-07-251
2018-07-262
2018-07-275
2018-07-2810
2018-07-295
2018-07-30155
2018-07-313,729
2018-08-013,179,230
2018-08-022,775,951
2018-08-03643
2018-08-226
2018-08-231
2018-09-1918
2019-06-111
2019-12-041
2020-04-011
2027-01-201
  • Observation:
    • Almost all records belong to 2018-08-01 (3,179,230) and 2018-08-02 (2,775,951)
    • 3,729 records on 2018-07-31 and 643 on 2018-08-03 come from the hours around midnight
    • A small number have clearly wrong dates: 1970-01-01 (118 records — the Unix epoch, i.e. an unset GPS clock), 2015–2017, and even 2027-01-20 (in the future)
    • These are GPS timestamp errors and should be removed before analysis

1970-01-01 is the "zero" of computer time. Seeing it is like a receipt printed with date 00/00/0000 — the clock was never set, not that the taxi drove in 1970.

1.3.4 How many unique taxis for each day?

Select to_date(gps_time) as dateonly, count(distinct vehicle_id) as total from taxi_raw_data group
by to_date(gps_time);
  • Answer: 5,913 taxis on 2018-08-01, 5,920 on 2018-08-02, and 2,843 taxis have records on 2018-07-31
  • Observation:
    • Most of the wrong dates come from only 1–2 vehicles each → errors from a few GPS devices
    • This query has no ORDER BY, so dates are printed in the order the reducers produced them

1.3.5 Find min, max, avg speed by hour

select hour(gps_time) as hour24,
       min(speed) as min_speed, max(speed) as max_speed, avg(speed) as avg_speed
from taxi_raw_data
group by hour(gps_time)
order by hour24;
  • Answer (24 rows; min_speed = 0.0 for every hour):
hour24max_speedavg_speed
0149.09.8775
1139.07.9923
2135.07.0067
3156.06.5050
4161.08.3651
5153.011.7817
6159.013.9312
7149.013.2038
8215.014.3988
9147.015.5218
10161.016.2517
11167.016.5442
12283.016.4377
13148.016.3885
14136.016.2072
15155.016.0206
16139.015.5861
17169.014.1990
18189.013.7128
19199.013.7759
20257.014.0375
21148.013.7095
22160.012.8088
23141.010.9770
  • Observation:
    • Average speed is lowest between 01:00 and 04:00 (about 6.5–8.4 km/h) and highest around midday (about 16.5 km/h), because many taxis are parked or waiting with speed 0 at night
    • Some maximum speeds are unrealistic for city taxis, e.g. 283 km/h at 12:00, 257 km/h at 20:00, 215 km/h at 08:00 → noise in the speed values
  • Average for an hour hh:

avg_speed(h)=1nh∑i : hour(ti)=hspeedi\boxed{\text{avg\_speed}(h) = \frac{1}{n_h}\sum_{i\,:\,\text{hour}(t_i) = h} \text{speed}_i}

The average is low at night not because taxis drive slowly, but because lots of parked taxis (speed 0) still send GPS pings, pulling the average down — like averaging the running speed of a team where half are sitting on the bench.


Check Hive Data Files on HDFS (Tutorial 1.4)

  • Access the HDFS GUI (NameNode web UI, port 9870) (CAPTURE SCREEN)
  • Check files in /user/hive/warehouse/taxidb.db/taxi_raw_data
  • Make sure to include the URL in the screenshot

My Answer — 1.4

  • Hive stores each partition of the table as a sub-directory: pdate=2018-08-01 and pdate=2018-08-02, owned by hadoop
  • The loaded .csv.out file is inside each directory
/user/hive/warehouse/taxidb.db/taxi_raw_data
├── pdate=2018-08-01/
│   └── 20180801.csv.out
└── pdate=2018-08-02/
    └── 20180802.csv.out

  • This is exactly the Hive data model from the lecture: database → taxidb.db, table → taxi_raw_data, partition → sub-directory

Check Hive Process on YARN (Tutorial 1.5)

  • Access the YARN GUI (ResourceManager, port 8088) (CAPTURE SCREEN)
  • There should be several tasks

My Answer — 1.5

  • The YARN ResourceManager lists the Hive applications
  • Hive runs queries on the Tez engine and keeps one Tez session (= one YARN application) open per client
    • All queries from the Hive CLI ran inside one application (FINISHED / SUCCEEDED after the session closed)
    • The Hue queries ran in a second application (RUNNING)

Why only 1–2 apps instead of one per query? Tez keeps a "session" warm like a taxi waiting at the curb — each new query hops into the same running container instead of calling a new taxi every time.


Exercise: Apply Preprocessing Step

  • Create a new table with clean data (remove error and noise)

    • From the result of the previous statistics, even though we imported 2 days of data, the files contain data of other days
      • Filter data for 2018-07-31 to 2018-08-02
    • Filter include only GPS Valid = 1
    • Filter include only speed less than 200
  • Create new table (stored as ORC, partitioned by PD)

CREATE TABLE taxi_data_orc (vehicle_id string, gps_valid int, lat double, lon double, gps_time string, speed double, heading int, meter int, engine int)
PARTITIONED BY (PD string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS ORC;
  • Insert data into the new clean table (tutorial template: WHERE … <put criteria here>)
INSERT OVERWRITE TABLE taxi_data_orc PARTITION (PD)
SELECT vehicle_id, gps_valid, lat, lon, gps_time, speed, heading, meter, engine,
to_date(gps_time) as PD
FROM taxi_raw_data
WHERE to_date(gps_time) between '2018-07-31' and '2018-08-02' and gps_valid =1 and speed <200

My Answer — 2.0 Create taxi_data_orc and Insert Cleaned Data

CREATE TABLE taxi_data_orc (vehicle_id string, gps_valid int, lat double, lon double,
  gps_time string, speed double, heading int, meter int, engine int)
PARTITIONED BY (PD string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
STORED AS ORC;
 
INSERT OVERWRITE TABLE taxi_data_orc PARTITION (PD)
SELECT vehicle_id, gps_valid, lat, lon, gps_time, speed, heading, meter, engine,
       to_date(gps_time) as PD
FROM taxi_raw_data
WHERE to_date(gps_time) between '2018-07-31' and '2018-08-02'
  and gps_valid = 1 and speed < 200;
  • Keeps only clean records: GPS time between 2018-07-31 and 2018-08-02, gps_valid = 1, speed < 200 km/h
  • Dynamic partitioning creates one partition per date automatically → 3/3 partitions loaded (2018-07-31, 2018-08-01, 2018-08-02)
  • Key ideas:
    • PARTITION (PD) with no value = dynamic partition → the last column of the SELECT (to_date(gps_time) as PD) decides the partition
    • Now the partition is based on the real GPS date, not the file name
    • This is the Hive way to "update/clean" data: create a new table + INSERT ... SELECT (no UPDATE/DELETE)
    • STORED AS ORC → columnar, compressed, faster

Instead of erasing mistakes in the original notebook (Hive can't), you copy only the good lines into a fresh, neatly organized notebook (ORC), with a tab for each real date.


Rerun: Calculate Statistics for Data Quality Check (Exercise 2.1–2.5)

  • CAPTURE SCREEN for each result

2.1 How many data in the system?

Select count(*) from taxi_data_orc;
  • Answer: 5,927,794 records
  • Removed: 5,962,554−5,927,794=34,7605{,}962{,}554 - 5{,}927{,}794 = 34{,}760 records

Removed %=Nraw−NcleanNraw×100%\boxed{\text{Removed \%} = \frac{N_{\text{raw}} - N_{\text{clean}}}{N_{\text{raw}}} \times 100\%}

  • 34,7605,962,554×100%≈0.58%\frac{34{,}760}{5{,}962{,}554} \times 100\% \approx 0.58\% (about 0.6% removed)
  • Hive answered in 0.15 seconds without launching a Tez job, because ORC files store the row count in their metadata

2.2 How many days of data?

Select count(distinct to_date(gps_time)) from taxi_data_orc;
  • Answer: 3 dates (only those inside the filter range; compared with 52 before cleaning)

2.3 How many data for each day?

Select to_date(gps_time) as dateonly, count(*) from taxi_data_orc group by to_date(gps_time)
order by dateonly;
dateonlycount(before cleaning)
2018-07-313,6953,729
2018-08-013,162,0413,179,230
2018-08-022,762,0582,775,951
  • Each date lost some records because of the gps_valid and speed filters

2.4 How many unique taxis for each day?

Select to_date(gps_time) as dateonly, count(distinct vehicle_id) from taxi_data_orc group by
to_date(gps_time);
dateonlyunique taxis(before cleaning)
2018-07-312,8102,843
2018-08-015,9085,913
2018-08-025,9155,920
  • Only a few taxis disappeared → the removed records were scattered across many vehicles rather than coming from a few broken ones

2.5 Find min, max, avg speed by hour

select hour(gps_time) as hour24,
       min(speed) as min_speed, max(speed) as max_speed, avg(speed) as avg_speed
from taxi_data_orc
group by hour(gps_time)
order by hour24;
  • Answer (24 rows; min_speed = 0.0 for every hour):
hour24max_speedavg_speedmax before cleaning
0149.09.9348149.0
1139.08.0336139.0
2135.07.0407135.0
3156.06.5346156.0
4161.08.4082161.0
5153.011.8556153.0
6159.013.9891159.0
7149.013.2536149.0
8169.014.4517215.0
9147.015.5739147.0
10153.016.2994161.0
11167.016.5971167.0
12137.016.4905283.0
13148.016.4553148.0
14136.016.2629136.0
15155.016.0869155.0
16139.015.6562139.0
17169.014.2643169.0
18189.013.7773189.0
19199.013.8589199.0
20187.014.1264257.0
21148.013.8013148.0
22160.012.8901160.0
23141.011.0548141.0
  • Observation:
    • Every maximum speed is now below 200 km/h (highest is 199 km/h at 19:00)
    • Outliers like 283 km/h at 12:00 are gone (new max for 12:00 is 137 km/h)
    • Hourly averages increased slightly (e.g. 16.44 → 16.49 km/h at 12:00) because invalid-GPS records were removed

2.6 Use Hue to Calculate and Make Graph

  • Find total unique vehicles for each hour and plot on a bar chart (CAPTURE SCREEN)
  • Capture the whole screen with SQL

My Answer — 2.6

  • Open Hue (port 8888) in the browser → Hive editor → count unique vehicles for each hour from the clean table
  • Display as a bar chart with hour24 on the X-axis and total_vehicle on the Y-axis
SELECT hour(gps_time) AS hour24,
       count(DISTINCT vehicle_id) AS total_vehicle
FROM taxidb.taxi_data_orc
GROUP BY hour(gps_time)
ORDER BY hour24

  • Observation:
    • Between 4,497 and 4,555 taxis report data in every hour from 00:00–08:00 and 16:00–23:00
    • The number jumps to 5,878–5,892 (peak 5,892 at 11:00) between 09:00 and 15:00
    • Because the counts are almost flat within each period instead of following a smooth daily curve, this suggests a group of about 1,350 vehicles only sends data during that time window, rather than a real change in the number of taxis on the road

A real "rush hour" would rise and fall gradually like a hill. A flat step up and a flat step down is more like a second shift of devices clocking in at 9 and out at 3 — a data collection pattern, not traffic.


Key Takeaways

  • Schema on read: CREATE TABLE + LOAD DATA just puts the raw CSV into the table's HDFS folder; nothing is checked at load time → you must run data quality checks
  • Partition by file ≠ partition by real date: pdate came from the file name, but to_date(gps_time) revealed 52 dates (1970 epoch, 2015–2017, 2027…)
  • Cleaning in Hive = new table + INSERT OVERWRITE ... SELECT ... WHERE (no UPDATE/DELETE), with dynamic partitioning to split by the real date
  • ORC makes count(*) instant (row count stored in metadata)
  • Hue gives a web SQL editor + quick charts; HDFS UI (9870) shows partitions as directories; YARN UI (8088) shows the Tez applications running the queries