3 # spark script to make sorted, and partitioned parquet files
5 from pyspark.sql import functions as f
6 from pyspark.sql import SparkSession
8 spark = SparkSession.builder.getOrCreate()
10 df = spark.read.parquet("/gscratch/comdata/output/reddit_comments.parquet_temp2")
12 df = df.withColumn("subreddit_2", f.lower(f.col('subreddit')))
13 df = df.drop('subreddit')
14 df = df.withColumnRenamed('subreddit_2','subreddit')
16 df = df.withColumnRenamed("created_utc","CreatedAt")
17 df = df.withColumn("Month",f.month(f.col("CreatedAt")))
18 df = df.withColumn("Year",f.year(f.col("CreatedAt")))
19 df = df.withColumn("Day",f.dayofmonth(f.col("CreatedAt")))
21 df = df.repartition('subreddit')
22 df2 = df.sort(["subreddit","CreatedAt","link_id","parent_id","Year","Month","Day"],ascending=True)
23 df2 = df2.sortWithinPartitions(["subreddit","CreatedAt","link_id","parent_id","Year","Month","Day"],ascending=True)
24 df2.write.parquet("/gscratch/comdata/output/reddit_comments_by_subreddit.parquet", mode='overwrite', compression='snappy')
26 df = df.repartition('author')
27 df3 = df.sort(["author","CreatedAt","subreddit","link_id","parent_id","Year","Month","Day"],ascending=True)
28 df3 = df3.sortWithinPartitions(["author","CreatedAt","subreddit","link_id","parent_id","Year","Month","Day"],ascending=True)
29 df3.write.parquet("/gscratch/comdata/output/reddit_comments_by_author.parquet", mode='overwrite')