Read JSON Top Clicked Products

Medium
File I/O
JSON
Filtering
Aggregation

Read JSON Top Clicked Products

The file datasets/json/web_logs.json holds one object per web event. The events with event_type == 'click_product' are product clicks - which products get the most attention?

What to do

  1. Read the JSON file from disk. Its local path is handed to you as paths['web_logs'].
  2. Keep the rows with event_type == 'click_product' and a product_id that is not null (some clicks have no product).
  3. Count the rows per product_id.
  4. Return the ten products with the most clicks.
  5. Sort by click_count descending, then by product_id ascending.
  6. Return exactly these columns: product_id, click_count.

Signature

File questions receive the path as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    logs = spark.read.json(paths['web_logs'])
    # TODO: filter, aggregate and sort
    return logs

The same data is also preloaded as dfs['web_logs'] if you prefer to skip the file read.

This question runs in your browser - no sign-in needed. Press Run for the sample data and Submit for the hidden data.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input files

1 File
Loaded automatically

These files are loaded into this browser when you run. Their paths are handed to your code as the third argument: solve(spark, dfs, paths). Read one with spark.read.csv(paths['users'], header=True), or skip the read and use dfs['users'].

paths[web_logs]
json
3000 rows
datasets/json/web_logs.json
log_id(bigint)
session_id(string)
user_id(int)
event_type(string)
product_id(int)
referrer_url(string)
device_type(string)
timestamp(timestamp)

Return these columns

product_id(bigint)
click_count(bigint)

Column names and order must match. Sort order is specified in the question.

Browser engine
Local files
Loading Python editor…
Click Run to try the sample cases, or Submit to check the hidden cases. First run caches the browser Python engine (~8–15s once); all code stays on your device.