]> code.communitydata.science - cdsc_reddit.git/blob - comments_2_parquet_part2.py
62580acf605e92c39a4f93c5f15f272d612b8e41
[cdsc_reddit.git] / comments_2_parquet_part2.py
1 #!/usr/bin/env python3
2
3 # spark script to make sorted, and partitioned parquet files 
4
5 from pyspark.sql import functions as f
6 from pyspark.sql import SparkSession
7
8 spark = SparkSession.builder.getOrCreate()
9
10 df = spark.read.parquet("/gscratch/comdata/output/reddit_comments.parquet_temp2")
11
12 df = df.withColumn("subreddit_2", f.lower(f.col('subreddit')))
13 df = df.drop('subreddit')
14 df = df.withColumnRenamed('subreddit_2','subreddit')
15
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")))
20
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')
25
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',compression='snappy')

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