-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaywiseSales.py
53 lines (37 loc) · 2.27 KB
/
DaywiseSales.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from pyspark.sql import SparkSession
import pandas as pd
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
from pyspark.sql.functions import *
spark = SparkSession.builder.appName("SparkSQL").getOrCreate()
# ------------------ Schema Definition ----------------------------------------- #
mySchema = StructType([\
StructField("event_time", StringType(), True),
StructField("event_type", StringType(), True),
StructField("product_id",IntegerType(), True),
StructField("category_id",StringType(), True),
StructField("category_code", StringType(), True),
StructField("brand", StringType(), True),
StructField("price", StringType(), True),
StructField("user_id", IntegerType(), True),
StructField("user_session", StringType(), True),
])
# ------------------------ Loading Data --------------------------------------- #
ecommerceData = spark.read.format("csv")\
.schema(mySchema)\
.option("mode", "DROPMALFORMED")\
.option("path", "s3://sparkproject02/2019-Nov.csv")\
.load()
ecommerceData.printSchema()
ecommerce = ecommerceData.withColumn("event_time", date_format(col("event_time"), "EEEE"))
ecommerce.show()
output = ecommerce.select(ecommerce.event_time,ecommerce.event_type, ecommerce.product_id, ecommerce.user_id, ecommerce.brand, ecommerce.price, ecommerce.category_code, ecommerce.user_session ).cache()
output.show()
# ------------ Creating View -----------------#
output.createOrReplaceTempView("view_one")
# --------------------- Daywise Sales --------------------------------------------------------- #
# outputDfNew = spark.sql("select event_time, count(event_type) from view_one where event_type = 'purchase' group by event_time ")
# outputDfNew.show()
# -------------------- Daywise Views ----------------------------------------------------------- #
outputDfNew = spark.sql("select event_time, count(event_type) from view_one where event_type = 'view' group by event_time ")
outputDfNew.show()
outputDfNew.repartition(1).write.format("csv").mode("overwrite").option("path", "s3://sparkproject02/output/DatewiseSales").save()