4 # 1. from gz to arrow parquet (this script)
5 # 2. from arrow parquet to spark parquet (submissions_2_parquet_part2.py)
7 from datetime import datetime
8 from multiprocessing import Pool
9 from itertools import islice
10 from helper import find_dumps, open_fileset
13 import pyarrow.parquet as pq
18 parser = simdjson.Parser()
20 def parse_submission(post, names = None):
22 names = ['id','author','subreddit','title','created_utc','permalink','url','domain','score','ups','downs','over_18','has_media','selftext','retrieved_on','num_comments','gilded','edited','time_edited','subreddit_type','subreddit_id','subreddit_subscribers','name','is_self','stickied','quarantine','error']
25 post = parser.parse(post)
26 except (ValueError) as e:
29 row = [None for _ in names]
30 row[-1] = "Error parsing json|{0}|{1}".format(e,post)
36 if name == 'created_utc' or name == 'retrieved_on':
37 val = post.get(name,None)
39 row.append(datetime.fromtimestamp(int(post[name]),tz=None))
42 elif name == 'edited':
49 row.append(datetime.fromtimestamp(int(val),tz=None))
50 elif name == "time_edited":
52 elif name == 'has_media':
53 row.append(post.get('media',None) is not None)
55 elif name not in post:
58 row.append(post[name])
61 def parse_dump(partition):
64 stream = open_fileset([f"/gscratch/comdata/raw_data/reddit_dumps/submissions/{partition}"])
65 rows = map(parse_submission,stream)
67 pa.field('id', pa.string(),nullable=True),
68 pa.field('author', pa.string(),nullable=True),
69 pa.field('subreddit', pa.string(),nullable=True),
70 pa.field('title', pa.string(),nullable=True),
71 pa.field('created_utc', pa.timestamp('ms'),nullable=True),
72 pa.field('permalink', pa.string(),nullable=True),
73 pa.field('url', pa.string(),nullable=True),
74 pa.field('domain', pa.string(),nullable=True),
75 pa.field('score', pa.int64(),nullable=True),
76 pa.field('ups', pa.int64(),nullable=True),
77 pa.field('downs', pa.int64(),nullable=True),
78 pa.field('over_18', pa.bool_(),nullable=True),
79 pa.field('has_media',pa.bool_(),nullable=True),
80 pa.field('selftext',pa.string(),nullable=True),
81 pa.field('retrieved_on', pa.timestamp('ms'),nullable=True),
82 pa.field('num_comments', pa.int64(),nullable=True),
83 pa.field('gilded',pa.int64(),nullable=True),
84 pa.field('edited',pa.bool_(),nullable=True),
85 pa.field('time_edited',pa.timestamp('ms'),nullable=True),
86 pa.field('subreddit_type',pa.string(),nullable=True),
87 pa.field('subreddit_id',pa.string(),nullable=True),
88 pa.field('subreddit_subscribers',pa.int64(),nullable=True),
89 pa.field('name',pa.string(),nullable=True),
90 pa.field('is_self',pa.bool_(),nullable=True),
91 pa.field('stickied',pa.bool_(),nullable=True),
92 pa.field('quarantine',pa.bool_(),nullable=True),
93 pa.field('error',pa.string(),nullable=True)])
95 if not os.path.exists("/gscratch/comdata/output/temp/reddit_submissions.parquet/"):
96 os.mkdir("/gscratch/comdata/output/temp/reddit_submissions.parquet/")
98 with pq.ParquetWriter(f"/gscratch/comdata/output/temp/reddit_submissions.parquet/{partition}",schema=schema,compression='snappy',flavor='spark') as writer:
100 chunk = islice(rows,N)
101 pddf = pd.DataFrame(chunk, columns=schema.names)
102 table = pa.Table.from_pandas(pddf,schema=schema)
103 if table.shape[0] == 0:
105 writer.write_table(table)
109 def gen_task_list(dumpdir="/gscratch/comdata/raw_data/reddit_dumps/submissions"):
110 files = list(find_dumps(dumpdir,base_pattern="RS_20*.*"))
111 with open("parse_submissions_task_list",'w') as of:
113 partition = os.path.split(fpath)[1]
114 of.write(f'python3 submissions_2_parquet_part1.py parse_dump {partition}\n')
116 if __name__ == "__main__":
117 fire.Fire({'parse_dump':parse_dump,
118 'gen_task_list':gen_task_list})