This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
sellers_table = spark.read.parquet("./data/sellers_parquet") | |
''' | |
SELECT * | |
FROM sales_table | |
WHERE seller_id NOT IN (SELECT seller_id FROM sellers_table) | |
''' | |
# Left Anti joins are a way to express the NOT IN operation in SQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
sellers_table = spark.read.parquet("./data/sellers_parquet") | |
''' | |
SELECT * | |
FROM sales_table | |
WHERE seller_id IN (SELECT seller_id FROM sellers_table) | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
sellers_table = spark.read.parquet("./data/sellers_parquet") | |
''' | |
SELECT a.*, | |
b.* | |
FROM sales_table a | |
LEFT JOIN sellers_table b | |
ON a.seller_id = b.seller_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
''' | |
CREATE TABLE part_1 AS | |
SELECT * | |
FROM sales_table | |
WHERE num_pieces_sold > 50; | |
CREATE TABLE part_2 AS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
''' | |
SELECT seller_id, | |
CASE WHEN num_pieces_sold < 30 THEN 'Lower than 30', | |
WHEN num_pieces_sold < 60 THEN 'Between 31 and 60' | |
WHEN num_pieces_sold < 90 THEN 'Between 61 and 90' | |
ELSE 'More than 91' AS sales_bucket | |
FROM sales_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
''' | |
SELECT DISTINCT seller_id, | |
date | |
FROM sales_table | |
''' | |
sales_table_execution_plan = sales_table.select( | |
col("seller_id"), col("date") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
''' | |
SELECT order_id, | |
product_id, | |
seller_id, | |
date, | |
num_pieces_sold, | |
bill_raw_text, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read the source tables in Parquet format | |
sales_table = spark.read.parquet("./data/sales_parquet") | |
''' | |
SELECT order_id, | |
product_id, | |
seller_id, | |
date, | |
num_pieces_sold AS pieces, | |
bill_raw_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Print Schema | |
sales_table_execution_plan.printSchema() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Explain the Execution Plan of the previous script | |
sales_table_execution_plan.explain() |