]> code.communitydata.science - cdsc_reddit.git/blob - datasets/comments_2_parquet_part2.py
calculate some user-level attributes to detect bots
[cdsc_reddit.git] / datasets / 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",compression='snappy')
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/users/nathante/reddit_comments_by_subreddit.parquet_new", 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/users/nathante/reddit_comments_by_author.parquet_new", mode='overwrite',compression='snappy')

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