3 from datetime import datetime
4 from multiprocessing import Pool
5 from itertools import islice
6 from helper import find_dumps, open_fileset
9 import pyarrow.parquet as pq
11 globstr_base = "/gscratch/comdata/reddit_dumps/comments/RC_20*"
13 def parse_comment(comment, names= 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"]
18 comment = json.loads(comment)
19 except json.decoder.JSONDecodeError as e:
22 row = [None for _ in names]
23 row[-1] = "json.decoder.JSONDecodeError|{0}|{1}".format(e,comment)
28 if name == 'created_utc':
29 row.append(datetime.fromtimestamp(int(comment['created_utc']),tz=None))
30 elif name == 'edited':
37 row.append(datetime.fromtimestamp(int(val),tz=None))
38 elif name == "time_edited":
40 elif name not in comment:
44 row.append(comment[name])
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')])
51 dumpdir = "/gscratch/comdata/raw_data/reddit_dumps/comments"
53 files = list(find_dumps(dumpdir, base_pattern="RC_20*.*"))
57 stream = open_fileset(files)
61 rows = pool.imap_unordered(parse_comment, stream, chunksize=int(N/28))
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),
83 with pq.ParquetWriter("/gscratch/comdata/output/reddit_comments.parquet_temp",schema=schema,compression='snappy',flavor='spark') as writer:
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:
90 writer.write_table(table)