Solutions - Problem 8ΒΆ

Get number of orders and revenue for each year, month and date using orders which are either COMPLETE or CLOSED. Make sure that daily metrics are rolled up at month and year as well.

  • Read data from orders and filter for COMPLETE or CLOSED.

  • Read data from order_items

  • Join orders and order_items using order_id

  • Group data by order_date and get number of orders as well as revenue for each day using rollup.

  • Sort the data using order_date.

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.

from pyspark.sql import SparkSession

import getpass
username = getpass.getuser()

spark = SparkSession. \
    builder. \
    config('spark.ui.port', '0'). \
    config("spark.sql.warehouse.dir", f"/user/{username}/warehouse"). \
    enableHiveSupport(). \
    appName(f'{username} | Python - Joining Data Sets'). \
    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.conf.set('spark.sql.shuffle.partitions', '2')
orders = spark.read.json('/public/retail_db_json/orders')
orders.count()
68883
orders_filtered = orders. \
    filter("order_status IN ('COMPLETE', 'CLOSED')")
orders_filtered.count()
30455
order_items = spark.read.json('/public/retail_db_json/order_items')
orders_join = orders_filtered. \
    join(order_items, orders_filtered.order_id == order_items.order_item_order_id)
from pyspark.sql.functions import col, countDistinct, sum, round, date_format, year
revenue = orders_join. \
    rollup(
        year('order_date').alias('order_year'),
        date_format(col('order_date'), 'yyyyMM').alias('order_month'),
        'order_date'
    ). \
    agg(
        countDistinct('order_id').alias('order_count'),
        round(sum('order_item_subtotal'), 2).alias('revenue')
    ). \
    orderBy('order_year', 'order_month', 'order_date')
revenue.show(50, truncate=False)
+----------+-----------+---------------------+-----------+-------------+
|order_year|order_month|order_date           |order_count|revenue      |
+----------+-----------+---------------------+-----------+-------------+
|null      |null       |null                 |25266      |1.501298248E7|
|2013      |null       |null                 |11266      |6686892.0    |
|2013      |201307     |null                 |564        |333465.45    |
|2013      |201307     |2013-07-25 00:00:00.0|51         |31547.23     |
|2013      |201307     |2013-07-26 00:00:00.0|99         |54713.23     |
|2013      |201307     |2013-07-27 00:00:00.0|80         |48411.48     |
|2013      |201307     |2013-07-28 00:00:00.0|67         |35672.03     |
|2013      |201307     |2013-07-29 00:00:00.0|90         |54579.7      |
|2013      |201307     |2013-07-30 00:00:00.0|90         |49329.29     |
|2013      |201307     |2013-07-31 00:00:00.0|87         |59212.49     |
|2013      |201308     |null                 |2073       |1221828.9    |
|2013      |201308     |2013-08-01 00:00:00.0|82         |49160.08     |
|2013      |201308     |2013-08-02 00:00:00.0|90         |50688.58     |
|2013      |201308     |2013-08-03 00:00:00.0|72         |43416.74     |
|2013      |201308     |2013-08-04 00:00:00.0|63         |35093.01     |
|2013      |201308     |2013-08-05 00:00:00.0|62         |34025.27     |
|2013      |201308     |2013-08-06 00:00:00.0|99         |57843.89     |
|2013      |201308     |2013-08-07 00:00:00.0|77         |45525.59     |
|2013      |201308     |2013-08-08 00:00:00.0|57         |33549.47     |
|2013      |201308     |2013-08-09 00:00:00.0|43         |29225.16     |
|2013      |201308     |2013-08-10 00:00:00.0|83         |46435.04     |
|2013      |201308     |2013-08-11 00:00:00.0|58         |31155.5      |
|2013      |201308     |2013-08-12 00:00:00.0|93         |59014.74     |
|2013      |201308     |2013-08-13 00:00:00.0|31         |17956.88     |
|2013      |201308     |2013-08-14 00:00:00.0|64         |42043.45     |
|2013      |201308     |2013-08-15 00:00:00.0|79         |49566.68     |
|2013      |201308     |2013-08-16 00:00:00.0|44         |30160.1      |
|2013      |201308     |2013-08-17 00:00:00.0|104        |63226.83     |
|2013      |201308     |2013-08-18 00:00:00.0|72         |46612.7      |
|2013      |201308     |2013-08-19 00:00:00.0|35         |21397.59     |
|2013      |201308     |2013-08-20 00:00:00.0|73         |43714.92     |
|2013      |201308     |2013-08-21 00:00:00.0|49         |28947.65     |
|2013      |201308     |2013-08-22 00:00:00.0|68         |38190.02     |
|2013      |201308     |2013-08-23 00:00:00.0|69         |36305.67     |
|2013      |201308     |2013-08-24 00:00:00.0|90         |52650.15     |
|2013      |201308     |2013-08-25 00:00:00.0|60         |32295.08     |
|2013      |201308     |2013-08-26 00:00:00.0|62         |38548.4      |
|2013      |201308     |2013-08-27 00:00:00.0|73         |41010.78     |
|2013      |201308     |2013-08-28 00:00:00.0|44         |22637.86     |
|2013      |201308     |2013-08-29 00:00:00.0|83         |43275.41     |
|2013      |201308     |2013-08-30 00:00:00.0|40         |24200.55     |
|2013      |201308     |2013-08-31 00:00:00.0|54         |33955.11     |
|2013      |201309     |null                 |2167       |1302255.8    |
|2013      |201309     |2013-09-01 00:00:00.0|56         |42237.77     |
|2013      |201309     |2013-09-02 00:00:00.0|78         |44463.56     |
|2013      |201309     |2013-09-03 00:00:00.0|69         |44379.1      |
|2013      |201309     |2013-09-04 00:00:00.0|37         |22946.52     |
|2013      |201309     |2013-09-05 00:00:00.0|104        |59942.43     |
|2013      |201309     |2013-09-06 00:00:00.0|102        |61976.1      |
|2013      |201309     |2013-09-07 00:00:00.0|76         |45235.53     |
+----------+-----------+---------------------+-----------+-------------+
only showing top 50 rows
revenue = orders_join. \
    rollup(
        year('order_date').alias('order_year'),
        date_format(col('order_date'), 'yyyyMM').alias('order_month'),
        'order_date'
    ). \
    agg(
        countDistinct('order_id').alias('order_count'),
        round(sum('order_item_subtotal'), 2).alias('revenue')
    ). \
    orderBy(
        col('order_year').asc_nulls_last(), 
        col('order_month').asc_nulls_last(), 
        col('order_date').asc_nulls_last()
    )
revenue.show(50, truncate=False)
+----------+-----------+---------------------+-----------+---------+
|order_year|order_month|order_date           |order_count|revenue  |
+----------+-----------+---------------------+-----------+---------+
|2013      |201307     |2013-07-25 00:00:00.0|51         |31547.23 |
|2013      |201307     |2013-07-26 00:00:00.0|99         |54713.23 |
|2013      |201307     |2013-07-27 00:00:00.0|80         |48411.48 |
|2013      |201307     |2013-07-28 00:00:00.0|67         |35672.03 |
|2013      |201307     |2013-07-29 00:00:00.0|90         |54579.7  |
|2013      |201307     |2013-07-30 00:00:00.0|90         |49329.29 |
|2013      |201307     |2013-07-31 00:00:00.0|87         |59212.49 |
|2013      |201307     |null                 |564        |333465.45|
|2013      |201308     |2013-08-01 00:00:00.0|82         |49160.08 |
|2013      |201308     |2013-08-02 00:00:00.0|90         |50688.58 |
|2013      |201308     |2013-08-03 00:00:00.0|72         |43416.74 |
|2013      |201308     |2013-08-04 00:00:00.0|63         |35093.01 |
|2013      |201308     |2013-08-05 00:00:00.0|62         |34025.27 |
|2013      |201308     |2013-08-06 00:00:00.0|99         |57843.89 |
|2013      |201308     |2013-08-07 00:00:00.0|77         |45525.59 |
|2013      |201308     |2013-08-08 00:00:00.0|57         |33549.47 |
|2013      |201308     |2013-08-09 00:00:00.0|43         |29225.16 |
|2013      |201308     |2013-08-10 00:00:00.0|83         |46435.04 |
|2013      |201308     |2013-08-11 00:00:00.0|58         |31155.5  |
|2013      |201308     |2013-08-12 00:00:00.0|93         |59014.74 |
|2013      |201308     |2013-08-13 00:00:00.0|31         |17956.88 |
|2013      |201308     |2013-08-14 00:00:00.0|64         |42043.45 |
|2013      |201308     |2013-08-15 00:00:00.0|79         |49566.68 |
|2013      |201308     |2013-08-16 00:00:00.0|44         |30160.1  |
|2013      |201308     |2013-08-17 00:00:00.0|104        |63226.83 |
|2013      |201308     |2013-08-18 00:00:00.0|72         |46612.7  |
|2013      |201308     |2013-08-19 00:00:00.0|35         |21397.59 |
|2013      |201308     |2013-08-20 00:00:00.0|73         |43714.92 |
|2013      |201308     |2013-08-21 00:00:00.0|49         |28947.65 |
|2013      |201308     |2013-08-22 00:00:00.0|68         |38190.02 |
|2013      |201308     |2013-08-23 00:00:00.0|69         |36305.67 |
|2013      |201308     |2013-08-24 00:00:00.0|90         |52650.15 |
|2013      |201308     |2013-08-25 00:00:00.0|60         |32295.08 |
|2013      |201308     |2013-08-26 00:00:00.0|62         |38548.4  |
|2013      |201308     |2013-08-27 00:00:00.0|73         |41010.78 |
|2013      |201308     |2013-08-28 00:00:00.0|44         |22637.86 |
|2013      |201308     |2013-08-29 00:00:00.0|83         |43275.41 |
|2013      |201308     |2013-08-30 00:00:00.0|40         |24200.55 |
|2013      |201308     |2013-08-31 00:00:00.0|54         |33955.11 |
|2013      |201308     |null                 |2073       |1221828.9|
|2013      |201309     |2013-09-01 00:00:00.0|56         |42237.77 |
|2013      |201309     |2013-09-02 00:00:00.0|78         |44463.56 |
|2013      |201309     |2013-09-03 00:00:00.0|69         |44379.1  |
|2013      |201309     |2013-09-04 00:00:00.0|37         |22946.52 |
|2013      |201309     |2013-09-05 00:00:00.0|104        |59942.43 |
|2013      |201309     |2013-09-06 00:00:00.0|102        |61976.1  |
|2013      |201309     |2013-09-07 00:00:00.0|76         |45235.53 |
|2013      |201309     |2013-09-08 00:00:00.0|70         |39660.48 |
|2013      |201309     |2013-09-09 00:00:00.0|86         |49372.42 |
|2013      |201309     |2013-09-10 00:00:00.0|88         |47745.72 |
+----------+-----------+---------------------+-----------+---------+
only showing top 50 rows
revenue.count()
380