Apache Hive
Introductory
Hive is a data warehouse infrastructure built on top of Hadoop for providing data summarization, query, and analysis.
Hive Components
Two Main Components
- High-level language (HiveQL)
- Two execution modes
- Local: reads/writes to local file system
- Mapreduce: connects to Hadoop cluster and reads/writes to HDFS
Two modes:
- Interactive mode: using console
- Batch mode: submit a script
Physical Layout:
- Warehouse directory in HDFS
- E.g.,
/user/hive/warehouse
- E.g.,
- Tables stored in subdirectories of warehouse
- Partitions form subdirectories of tables
- Actual data stored in flat files
- Control char-delimited text, or SequenceFiles
- With custom SerDe, can use arbitrary format
- Warehouse directory in HDFS
Introductory Example
Task: Relational join on the table of word counts from Shakespeare collection and table of word counts from Homer1
2
3SELECT s.word, s.freq, k.freq FROM shakespeare s
JOIN homer k ON (s.word = k.word) WHERE s.freq >= 1 AND k.freq >= 1
ORDER BY s.freq DESC LIMIT 10;
Data Model
Hive deals with Structured Data, of different types: Tables → Partitions → Buckets
- Table:
- Typed columns (int, float, string, boolean)
- Similar to Tables in RDBMS
- Each Table is a Unique directory in HDFS
- Also, list: map (for JSON-like data)
- Partitions:
- To determine the distribution of data within a Table
- For example, range-partition tables by date
- Each Partition is a sub-directory of the main directory in HDFS
- To determine the distribution of data within a Table
- Buckets:
- Partitions can be further divided into Buckets
- For example, Hash partitions within ranges (useful for sampling, join optimization)
- Each Bucket is stored as a file in the directory
- Partitions can be further divided into Buckets
HiveQL
- Data Definition Language (DDL)
- Used to define the structure of your data. It includes commands for creating, altering, and dropping database objects such as tables, views, indexes, etc.
- e.g.
CREATETABLE,DROP TABLEcommands with extensions to define file formats, partitioning and bucketing information
- Data Manipulation Language (DML)
- Used to manipulate the data stored within the tables. It includes commands for querying, inserting, updating, and deleting data.
- Query Statements:
SELECT,JOIN,UNION, etc
Data Definition Language
Create table
1
CREATE TABLE sample (foo INT, bar STRING) PARTITIONED BY (ds STRING);

Print a table and its details information
1
2SHOW TABLES '.*s';
DESCRIBE sample;Others
1
2ALTER TABLE sample ADD COLUMNS (new_col INT);
DROP TABLE sample;
Data Manipulation Language
User Defined Functions (UDF)
- Perform tasks such as
Substr,Trim, etc on data elements
- Perform tasks such as
User Defined Aggregation Function (UDAF)
- Perform Operations on Columns, e.g.
Sum,Average,Max,Min, …
- Perform Operations on Columns, e.g.
User Defined Table-Generating Functions (UDTF)
- Output a new table
- e.g. the
Explodefunction which is similar toFLATTEN()in Pig
Custom MapReduce scripts
- The MR scripts must read rows from standard output
- Write rows to standard input

Example
1 | -- Create the UrlCounts which count the number of visits for each url |
- Execution flow:

Query Examples
Select & Filter
1 | SELECT foo FROM sample WHERE ds='2012-02-24'; |
Aggregation & Grouping
1 | SELECT MAX(foo) FROM sample; |
Multi-Insertion
1 | FROM page_view_stg pvs |
Joins
1 | SELECT * FROM customer c JOIN order_cust o ON (c.id=o.cus_id); |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

