Data Engineering using Spark SQL - Basic Transformations - Conclusion - Final Solution

Description Paragraph:
This article provides a step-by-step guide on how to use Spark SQL for retail data analysis. It covers setting up Spark SQL, creating tables, loading data, performing joins, filtering data, and aggregating results to analyze product revenue. The guide is designed for beginners and includes hands-on tasks for practical application.

Explanation for the video:
The accompanying video provides a visual demonstration of the concepts discussed in this article. It serves as a complementary resource to the text, offering a guided tutorial on executing Spark SQL commands for retail data analysis.

Put a placeholder for the video here with text so that I can replace as part of the automation

Key Concepts Explanation

Spark SQL Setup

To start using Spark SQL, you need to configure Spark session settings using either Spark SQL command line interface, Scala, or Pyspark.

Data Preparation

Tasks include creating tables for retail data, loading data into created tables, and projecting fields of interest such as order date, product ID, and product revenue.

Hands-On Tasks

  1. Create tables for orders and order items.
  2. Load data from local files into the created tables.
  3. Perform joins, filtering for COMPLETE or CLOSED orders.
  4. Group data by order date and product ID, aggregate subtotal to calculate product revenue.


In conclusion, this guide has equipped you with the essential knowledge of using Spark SQL for retail data analysis. Practice the provided tasks and explore further to enhance your skills. Feel free to engage with the community for support and learning.

Conclusion - Final Solution

Let us review the Final Solution for our problem statement daily_product_revenue.
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(“”)
import org.apache.spark.sql.SparkSession

val username = System.getProperty(“”)

val spark = SparkSession.


config("spark.ui.port", "0").

config("spark.sql.warehouse.dir", s"/user/${username}/warehouse").


appName(s"${username} | Spark SQL - Basic Transformations").



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
  • Prepare tables
    • Create tables
    • Load the data into tables
  • Project fields of interest (order_date, order_item_product_id, product_revenue)
  • Perform joins and filter for COMPLETE or CLOSED orders
  • Group data by order_date and order_item_product_id for aggregation on order_item_subtotal to get product revenue.

Watch the video tutorial here