]> code.communitydata.science - cdsc_reddit.git/blob - comments_2_parquet_part1.py
Add code for running tf-idf at the weekly level.
[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 def parse_comment(comment, names= None):
12     if names is None:
13         names = ["id","subreddit","link_id","parent_id","created_utc","author","ups","downs","score","edited","subreddit_type","subreddit_id","stickied","is_submitter","body","error"]
14
15     try:
16         comment = json.loads(comment)
17     except json.decoder.JSONDecodeError as e:
18         print(e)
19         print(comment)
20         row = [None for _ in names]
21         row[-1] = "json.decoder.JSONDecodeError|{0}|{1}".format(e,comment)
22         return tuple(row)
23
24     row = []
25     for name in names:
26         if name == 'created_utc':
27             row.append(datetime.fromtimestamp(int(comment['created_utc']),tz=None))
28         elif name == 'edited':
29             val = comment[name]
30             if type(val) == bool:
31                 row.append(val)
32                 row.append(None)
33             else:
34                 row.append(True)
35                 row.append(datetime.fromtimestamp(int(val),tz=None))
36         elif name == "time_edited":
37             continue
38         elif name not in comment:
39             row.append(None)
40
41         else:
42             row.append(comment[name])
43
44     return tuple(row)
45
46
47 #    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')])
48
49 dumpdir = "/gscratch/comdata/raw_data/reddit_dumps/comments/"
50
51 files = list(find_dumps(dumpdir, base_pattern="RC_20*"))
52
53 pool = Pool(28)
54
55 stream = open_fileset(files)
56
57 N = int(1e4)
58
59 rows = pool.imap_unordered(parse_comment, stream, chunksize=int(N/28))
60
61 schema = pa.schema([
62     pa.field('id', pa.string(), nullable=True),
63     pa.field('subreddit', pa.string(), nullable=True),
64     pa.field('link_id', pa.string(), nullable=True),
65     pa.field('parent_id', pa.string(), nullable=True),
66     pa.field('created_utc', pa.timestamp('ms'), nullable=True),
67     pa.field('author', pa.string(), nullable=True),
68     pa.field('ups', pa.int64(), nullable=True),
69     pa.field('downs', pa.int64(), nullable=True),
70     pa.field('score', pa.int64(), nullable=True),
71     pa.field('edited', pa.bool_(), nullable=True),
72     pa.field('time_edited', pa.timestamp('ms'), nullable=True),
73     pa.field('subreddit_type', pa.string(), nullable=True),
74     pa.field('subreddit_id', pa.string(), nullable=True),
75     pa.field('stickied', pa.bool_(), nullable=True),
76     pa.field('is_submitter', pa.bool_(), nullable=True),
77     pa.field('body', pa.string(), nullable=True),
78     pa.field('error', pa.string(), nullable=True),
79 ])
80
81 from pathlib import Path
82 p = Path("/gscratch/comdata/output/reddit_comments.parquet_temp2")
83
84 if not p.is_dir():
85     if p.exists():
86         p.unlink()
87     p.mkdir()
88
89 else:
90     list(map(Path.unlink,p.glob('*')))
91
92 part_size = int(1e7)
93 part = 1
94 n_output = 0
95 writer = pq.ParquetWriter(f"/gscratch/comdata/output/reddit_comments.parquet_temp2/part_{part}.parquet",schema=schema,compression='snappy',flavor='spark')
96
97 while True:
98     if n_output > part_size:
99         if part > 1:
100             writer.close()
101
102         part = part + 1
103         n_output = 0
104     
105         writer = pq.ParquetWriter(f"/gscratch/comdata/output/reddit_comments.parquet_temp2/part_{part}.parquet",schema=schema,compression='snappy',flavor='spark')
106
107     n_output += N
108     chunk = islice(rows,N)
109     pddf = pd.DataFrame(chunk, columns=schema.names)
110     table = pa.Table.from_pandas(pddf,schema=schema)
111     if table.shape[0] == 0:
112         break
113     writer.write_table(table)
114
115

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