Introductory

Hive is a data warehouse infrastructure built on top of Hadoop for providing data summarization, query, and analysis.

Hive Components

  • Two Main Components

    1. High-level language (HiveQL)
    2. Two execution modes
      • Local: reads/writes to local file system
      • Mapreduce: connects to Hadoop cluster and reads/writes to HDFS
  • Two modes:

    1. Interactive mode: using console
    2. Batch mode: submit a script
  • Physical Layout:

    • Warehouse directory in HDFS
      • E.g., /user/hive/warehouse
    • 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

Introductory Example

Task: Relational join on the table of word counts from Shakespeare collection and table of word counts from Homer

1
2
3
SELECT 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
  • 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

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. CREATE TABLE, DROP TABLE commands 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

  1. Create table

    1
    CREATE TABLE sample (foo INT, bar STRING) PARTITIONED BY (ds STRING);

  2. Print a table and its details information

    1
    2
    SHOW TABLES '.*s';
    DESCRIBE sample;
  3. Others

    1
    2
    ALTER 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
  • User Defined Aggregation Function (UDAF)

    • Perform Operations on Columns, e.g. Sum, Average, Max, Min, …
  • User Defined Table-Generating Functions (UDTF)

    • Output a new table
    • e.g. the Explode function which is similar to FLATTEN() in Pig
  • Custom MapReduce scripts

    • The MR scripts must read rows from standard output
    • Write rows to standard input

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- Create the UrlCounts which count the number of visits for each url
INSERT TABLE UrlCounts
(SELECT url, count(*) AS count
FROM Visits
GROUP BY url)

-- Join the UrlCounts and UrlInfo to UrlCategoryCount based on their url attribute
INSERT TABLE UrlCategoryCount
(SELECT url, count, category
FROM UrlCounts JOIN UrlInfo ON (UrlCounts.url = UrlInfo.url))

-- Select the top 10 categories
SELECT category, topTen(*)
FROM UrlCategoryCount
GROUP BY category
  • Execution flow:

Query Examples

Select & Filter

1
2
3
4
5
6
7
8
SELECT foo FROM sample WHERE ds='2012-02-24';

-- Create HDFS dir for the output
INSERT OVERWRITE DIRECTORY '/tmp/hdfs_out' SELECT *
FROM sample WHERE ds='2012-02-24';

-- Create local dir for the output
INSERT OVERWRITE LOCAL DIRECTORY '/tmp/hive-sample-out' SELECT * FROM sample;

Aggregation & Grouping

1
2
3
4
5
6
7
8
9
SELECT MAX(foo) FROM sample;
SELECT ds, COUNT(*), SUM(foo) FROM sample GROUP BY ds;

-- Store the results into a table (Hive allows the From clause to come first !!!)
FROM sample s
INSERT OVERWRITE TABLE bar
SELECT s.bar, count(*)
WHERE s.foo > 0
GROUP BY s.bar;

Multi-Insertion

1
2
3
4
5
6
7
FROM page_view_stg pvs
INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country='US')
SELECT pvs.viewTime, ... WHERE pvs.country = 'US'
INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country='CA')
SELECT pvs.viewTime, ... WHERE pvs.country = 'CA'
INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country='UK')
SELECT pvs.viewTime, ... WHERE pvs.country = 'UK';

Joins

1
2
3
4
5
6
SELECT * FROM customer c JOIN order_cust o ON (c.id=o.cus_id);

SELECT c.id, c.name, c.address, ce.exp
FROM customer c JOIN (SELECT cus_id,sum(price) AS exp
FROM order_cust
GROUP BY cus_id) ce ON (c.id=ce.cus_id);