Joining Tables - Inner¶
Let us understand how to join data from multiple tables.
Let us start spark context for this Notebook so that we can execute the code provided. You can sign up for our 10 node state of the art cluster/labs to learn Spark SQL using our unique integrated LMS.
val username = System.getProperty("user.name")
import org.apache.spark.sql.SparkSession
val username = System.getProperty("user.name")
val spark = SparkSession.
builder.
config("spark.ui.port", "0").
config("spark.sql.warehouse.dir", s"/user/${username}/warehouse").
enableHiveSupport.
appName(s"${username} | Spark SQL - Basic Transformations").
master("yarn").
getOrCreate
If you are going to use CLIs, you can use Spark SQL using one of the 3 approaches.
Using Spark SQL
spark2-sql \
--master yarn \
--conf spark.ui.port=0 \
--conf spark.sql.warehouse.dir=/user/${USER}/warehouse
Using Scala
spark2-shell \
--master yarn \
--conf spark.ui.port=0 \
--conf spark.sql.warehouse.dir=/user/${USER}/warehouse
Using Pyspark
pyspark2 \
--master yarn \
--conf spark.ui.port=0 \
--conf spark.sql.warehouse.dir=/user/${USER}/warehouse
Spark SQL supports ASCII style join (JOIN with ON).
There are different types of joins.
INNER JOIN - Get all the records from both the datasets which satisfies JOIN condition.
OUTER JOIN - We will get into the details as part of the next topic
Example for INNER JOIN
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
LIMIT 10
We can join more than 2 tables in one query. Here is how it will look like.
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
JOIN products p
ON p.product_id = oi.order_item_product_id
LIMIT 10
If we have to apply additional filters, it is recommended to use WHERE clause. ON clause should only have join conditions.
We can have non equal join conditions as well, but they are not used that often.
Here are some of the examples for INNER JOIN:
Get order id, date, status and item revenue for all order items.
Get order id, date, status and item revenue for all order items for all orders where order status is either COMPLETE or CLOSED.
Get order id, date, status and item revenue for all order items for all orders where order status is either COMPLETE or CLOSED for the orders that are placed in the month of 2014 January.
%%sql
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
LIMIT 10
%%sql
SELECT count(1)
FROM orders
%%sql
SELECT count(1)
FROM order_items
%%sql
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
LIMIT 10
%%sql
SELECT count(1)
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
%%sql
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
LIMIT 10
%%sql
SELECT count(1)
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
LIMIT 10
%%sql
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
AND date_format(order_date, 'yyyy-MM') = '2014-01'
LIMIT 10
%%sql
SELECT count(1)
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
AND date_format(order_date, 'yyyy-MM') = '2014-01'
LIMIT 10
Using Spark SQL with Python or Scala
spark("""
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
""").show()
spark("""
SELECT count(1)
FROM orders
""").show()
spark("""
SELECT count(1)
FROM order_items
""").show()
spark("""
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
""").show()
spark("""
SELECT count(1)
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
""").show()
spark("""
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
""").show()
spark("""
SELECT count(1)
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
""").show()
spark("""
SELECT o.order_id,
o.order_date,
o.order_status,
oi.order_item_subtotal
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
AND date_format(order_date, 'yyyy-MM') = '2014-01'
""").show()
spark("""
SELECT count(1)
FROM orders o JOIN order_items oi
ON o.order_id = oi.order_item_order_id
WHERE o.order_status IN ('COMPLETE', 'CLOSED')
AND date_format(order_date, 'yyyy-MM') = '2014-01'
""").show()