]> code.communitydata.science - cdsc_reddit.git/blob - comments_2_parquet_part1.py
faea040402a30f66c147dd034ed70ed7e4ac1e86
[cdsc_reddit.git] / comments_2_parquet_part1.py
1 #!/usr/bin/env python3
2 import json
3 from datetime import datetime
4 from multiprocessing import Pool
5 from itertools import islice
6 from helper import find_dumps, open_fileset
7 import pandas as pd
8 import pyarrow as pa
9 import pyarrow.parquet as pq
10
11 globstr_base = "/gscratch/comdata/reddit_dumps/comments/RC_20*"
12
13 def parse_comment(comment, names= None):
14     if names is None:
15         names = ["id","subreddit","link_id","parent_id","created_utc","author","ups","downs","score","edited","subreddit_type","subreddit_id","stickied","is_submitter","body","error"]
16
17     try:
18         comment = json.loads(comment)
19     except json.decoder.JSONDecodeError as e:
20         print(e)
21         print(comment)
22         row = [None for _ in names]
23         row[-1] = "json.decoder.JSONDecodeError|{0}|{1}".format(e,comment)
24         return tuple(row)
25
26     row = []
27     for name in names:
28         if name == 'created_utc':
29             row.append(datetime.fromtimestamp(int(comment['created_utc']),tz=None))
30         elif name == 'edited':
31             val = comment[name]
32             if type(val) == bool:
33                 row.append(val)
34                 row.append(None)
35             else:
36                 row.append(True)
37                 row.append(datetime.fromtimestamp(int(val),tz=None))
38         elif name == "time_edited":
39             continue
40         elif name not in comment:
41             row.append(None)
42
43         else:
44             row.append(comment[name])
45
46     return tuple(row)
47
48
49 #    conf = sc._conf.setAll([('spark.executor.memory', '20g'), ('spark.app.name', 'extract_reddit_timeline'), ('spark.executor.cores', '26'), ('spark.cores.max', '26'), ('spark.driver.memory','84g'),('spark.driver.maxResultSize','0'),('spark.local.dir','/gscratch/comdata/spark_tmp')])
50
51 dumpdir = "/gscratch/comdata/raw_data/reddit_dumps/comments"
52
53 files = list(find_dumps(dumpdir, base_pattern="RC_20*.*"))
54
55 pool = Pool(28)
56
57 stream = open_fileset(files)
58
59 N = 100000
60
61 rows = pool.imap_unordered(parse_comment, stream, chunksize=int(N/28))
62
63 schema = pa.schema([
64     pa.field('id', pa.string(), nullable=True),
65     pa.field('subreddit', pa.string(), nullable=True),
66     pa.field('link_id', pa.string(), nullable=True),
67     pa.field('parent_id', pa.string(), nullable=True),
68     pa.field('created_utc', pa.timestamp('ms'), nullable=True),
69     pa.field('author', pa.string(), nullable=True),
70     pa.field('ups', pa.int64(), nullable=True),
71     pa.field('downs', pa.int64(), nullable=True),
72     pa.field('score', pa.int64(), nullable=True),
73     pa.field('edited', pa.bool_(), nullable=True),
74     pa.field('time_edited', pa.timestamp('ms'), nullable=True),
75     pa.field('subreddit_type', pa.string(), nullable=True),
76     pa.field('subreddit_id', pa.string(), nullable=True),
77     pa.field('stickied', pa.bool_(), nullable=True),
78     pa.field('is_submitter', pa.bool_(), nullable=True),
79     pa.field('body', pa.string(), nullable=True),
80     pa.field('error', pa.string(), nullable=True),
81 ])
82
83 with pq.ParquetWriter("/gscratch/comdata/output/reddit_comments.parquet_temp",schema=schema,compression='snappy',flavor='spark') as writer:
84     while True:
85         chunk = islice(rows,N)
86         pddf = pd.DataFrame(chunk, columns=schema.names)
87         table = pa.Table.from_pandas(pddf,schema=schema)
88         if table.shape[0] == 0:
89             break
90         writer.write_table(table)
91
92     writer.close()

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