Beruflich Dokumente
Kultur Dokumente
Ahmad Alkilani
www.pluralsight.com
Outline
Bucketing
Sampling Data
Bucket sampling
Block sampling
Joins
Types of joins
Joins in depth
Join optimizations
Distributed Cache
Advanced Hive Functions
Table valued functions (UDTFs)
Lateral view
Extending Hive
Creating our own UDF
Transformation script using Streaming
Windowing and Analytical/Ranking Functions
Bucketing
Tables or Partitions can be bucketed
Bucketing is an approach to distribute or cluster table data
More efficient sampling
Better performance with Map-side joins
Used with partitioning or w/o when partitioning doesn’t work for your data
set
Buckets can also be sorted
Sort-Merge-Bucket (SMB) joins
SELECT * FROM source TABLESAMPLE (10 ROWS); 10 rows per input split
Joins
Join Types
JOIN (Inner Join)
LEFT, RIGHT, FULL [OUTER] JOIN
LEFT SEMI JOIN
CROSS JOIN
SELECT a.val, b.val FROM a LEFT OUTER JOIN b ON (a.key = b.key) JOIN c ON (c.key =
a.key);
CROSS JOIN
SELECT a.*, b.* FROM a CROSS JOIN
b;
Joins - In Depth
STREAM
S
&
S
9
R
E 9 x y
D 9 a b
U
C
E
Joins - Merging MR Jobs
A C B AB
ABC
Map-side Joins
All tables involved in a join are small enough to fit into memory
except 1 which is streamed through the mapper
set hive.auto.convert.join=true
SELECT a.*, b.* FROM a
JOIN b ON (a.key = b.key);
Map-side Joins for Bucketed Tables
Extending Hive
Creating a UDF
Import necessary packages
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.exec.Description;
Anything you need as part of your UDF
import org.apache.hadoop.io.Text
import java.util.*;
Add annotations
Description, Deterministic, Stateful, DistinctLike
Add annotations
Description, Deterministic, Stateful, DistinctLike
Tell Hive about the JAR file. Use ADD JAR /path/to/jar/myudf.jar
Adds JAR to distributed cache & classpath
Use the -i option when launching hive from the command line
Provide an initialization file
TRANSFORM
MAP, REDUCE
Don’t confuse with actual Map and Reduce, these are just syntactical sugar
Primarily introduced to minimize the work required to create Reduce code by
eliminating boilerplate code
*Example used here was inspired by the O’REILLY book “Programming Hive” and www.grymoire.com/Unix/Sed.html
Windowing and Analytics Functions
LEAD/LAG ID Basket Contents Quantity
1 Susan Apple 6
FIRST_VALUE 2 Susan Banana 12
3 Mike Pear 5
LAST_VALUE 4 Mike Milk 2
5 Mike Eggs 12
PARTITION BY 6 John Cereal 1
7 John Apple 7
OVER clause 8 John Milk 3
9 John Cheese 1
10 John Broccoli 2
WINDOW clause to provide window specification