]> code.communitydata.science - cdsc_reddit.git/blob - submissions_2_parquet_part2.py
Add code for running tf-idf at the weekly level.
[cdsc_reddit.git] / submissions_2_parquet_part2.py
1 #!/usr/bin/env python3
2
3 # spark script to make sorted, and partitioned parquet files 
4
5 import pyspark
6 from pyspark.sql import functions as f
7 from pyspark.sql import SparkSession
8 import os
9
10 spark = SparkSession.builder.getOrCreate()
11
12 sc = spark.sparkContext
13
14 conf = pyspark.SparkConf().setAppName("Reddit submissions to parquet")
15 conf = conf.set("spark.sql.shuffle.partitions",2000)
16 conf = conf.set('spark.sql.crossJoin.enabled',"true")
17 conf = conf.set('spark.debug.maxToStringFields',200)
18 sqlContext = pyspark.SQLContext(sc)
19
20 df = spark.read.parquet("/gscratch/comdata/output/temp/reddit_submissions.parquet/")
21
22 df = df.withColumn("subreddit_2", f.lower(f.col('subreddit')))
23 df = df.drop('subreddit')
24 df = df.withColumnRenamed('subreddit_2','subreddit')
25 df = df.withColumnRenamed("created_utc","CreatedAt")
26 df = df.withColumn("Month",f.month(f.col("CreatedAt")))
27 df = df.withColumn("Year",f.year(f.col("CreatedAt")))
28 df = df.withColumn("Day",f.dayofmonth(f.col("CreatedAt")))
29 df = df.withColumn("subreddit_hash",f.sha2(f.col("subreddit"), 256)[0:3])
30
31 # next we gotta resort it all.
32 df = df.repartition("subreddit")
33 df2 = df.sort(["subreddit","CreatedAt","id"],ascending=True)
34 df2 = df.sortWithinPartitions(["subreddit","CreatedAt","id"],ascending=True)
35 df2.write.parquet("/gscratch/comdata/output/temp/reddit_submissions_by_subreddit.parquet2", mode='overwrite',compression='snappy')
36
37
38 # # we also want to have parquet files sorted by author then reddit. 
39 df = df.repartition("author")
40 df3 = df.sort(["author","CreatedAt","id"],ascending=True)
41 df3 = df.sortWithinPartitions(["author","CreatedAt","id"],ascending=True)
42 df3.write.parquet("/gscratch/comdata/output/temp/reddit_submissions_by_author.parquet2", mode='overwrite',compression='snappy')

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