-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a44bafe
Showing
52 changed files
with
856,796 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Apache Spark/Amount Spent by Customer SQL Activity/customer-orders-amount-sorted.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Import the necessary modules and libraries | ||
from pyspark.sql import SparkSession, functions as func | ||
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, FloatType | ||
|
||
# Create a SparkSession | ||
spark = SparkSession.builder.appName("CustomerOrders").getOrCreate() | ||
|
||
# Create schema when reading customer-orders.csv | ||
schema = StructType([ \ | ||
StructField("cust_id", IntegerType(), True), \ | ||
StructField("item_id", IntegerType(), True), \ | ||
StructField("amount_spent", FloatType(), True)]) | ||
|
||
# Load up the data into a DataFrame | ||
df = spark.read.schema(schema).csv("customer-orders.csv") | ||
|
||
# Group by customer ID and sum up the amount spent | ||
df.groupBy("cust_id").agg(func.round(func.sum("amount_spent"), 2).alias("total_spent")).sort("total_spent").show() | ||
|
||
# Group by total amount spent by customer | ||
df.groupBy("cust_id").agg(func.round(func.sum("amount_spent"), 2).alias("total_spent")).sort(func.desc("total_spent")).show() | ||
|
||
# Stop the session | ||
spark.stop() | ||
|
||
|
||
|
Oops, something went wrong.