]> code.communitydata.science - cdsc_reddit.git/blobdiff - submissions_2_parquet_part2.py
Build comments dataset similarly to submissions and improve partitioning scheme
[cdsc_reddit.git] / submissions_2_parquet_part2.py
index 1708548f18c12446e72d5d95964f4cc215bc0d64..bd538e28ef581864c53eaf650e87f67b44b3800c 100644 (file)
@@ -2,12 +2,8 @@
 
 # spark script to make sorted, and partitioned parquet files 
 
 
 # spark script to make sorted, and partitioned parquet files 
 
-import pyspark
 from pyspark.sql import functions as f
 from pyspark.sql import functions as f
-from pyspark.sql.types import *
-from pyspark import SparkConf, SparkContext
-from pyspark.sql import SparkSession, SQLContext
-import os
+from pyspark.sql import SparkSession
 
 spark = SparkSession.builder.getOrCreate()
 
 
 spark = SparkSession.builder.getOrCreate()
 
@@ -31,12 +27,16 @@ df = df.withColumn("Day",f.dayofmonth(f.col("CreatedAt")))
 df = df.withColumn("subreddit_hash",f.sha2(f.col("subreddit"), 256)[0:3])
 
 # next we gotta resort it all.
 df = df.withColumn("subreddit_hash",f.sha2(f.col("subreddit"), 256)[0:3])
 
 # next we gotta resort it all.
-df2 = df.sort(["subreddit","author","id","Year","Month","Day"],ascending=True)
+df = df.repartition("subreddit")
+df2 = df.sort(["subreddit","CreatedAt","id"],ascending=True)
+df2 = df.sortWithinPartitions(["subreddit","CreatedAt","id"],ascending=True)
 df2.write.parquet("/gscratch/comdata/output/reddit_submissions_by_subreddit.parquet", partitionBy=["Year",'Month'], mode='overwrite')
 
 
 # # we also want to have parquet files sorted by author then reddit. 
 df2.write.parquet("/gscratch/comdata/output/reddit_submissions_by_subreddit.parquet", partitionBy=["Year",'Month'], mode='overwrite')
 
 
 # # we also want to have parquet files sorted by author then reddit. 
-df3 = df.sort(["author","CreatedAt","subreddit","id","Year","Month","Day"],ascending=True)
+df = df.repartition("author")
+df3 = df.sort(["author","CreatedAt","id"],ascending=True)
+df3 = df.sortWithinPartitions(["author","CreatedAt","id"],ascending=True)
 df3.write.parquet("/gscratch/comdata/output/reddit_submissions_by_author.parquet", partitionBy=["Year",'Month'], mode='overwrite')
 
 os.remove("/gscratch/comdata/output/reddit_submissions.parquet_temp")
 df3.write.parquet("/gscratch/comdata/output/reddit_submissions_by_author.parquet", partitionBy=["Year",'Month'], mode='overwrite')
 
 os.remove("/gscratch/comdata/output/reddit_submissions.parquet_temp")

Community Data Science Collective || Want to submit a patch?