-
Notifications
You must be signed in to change notification settings - Fork 17
/
common_crawl.py
183 lines (129 loc) · 5.51 KB
/
common_crawl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import sys
import json
from hadoop.io import SequenceFile
from itertools import islice, izip
from collections import Counter
import datetime
import boto
from boto.s3.connection import S3Connection
import cloud
# this key, secret access to aws-publicdatasets only -- created for WwOD 13 student usage
# turns out there is an anonymous mode in boto for public data sets:
# https://github.com/keiw/common_crawl_index/commit/ad341d0a41a828f260c9c08419dadff0dac6cf5b#L0R33
# conn=S3Connection(anon=True) will work instead of conn= S3Connection(KEY, SECRET) -- but there seems to be
# a bug in how S3Connection gets pickled for anon=True -- so for now, just use the KEY, SECRET
KEY = 'AKIAJH2FD7572FCTVSSQ'
SECRET = '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'
conn = S3Connection(KEY, SECRET)
bucket = conn.get_bucket('aws-publicdatasets')
def SequenceFileIterator(path):
reader = SequenceFile.Reader(path)
key_class = reader.getKeyClass()
value_class = reader.getValueClass()
key = key_class()
value = value_class()
position = reader.getPosition()
while reader.next(key, value):
yield (position, key.toString(), value.toString())
position = reader.getPosition()
reader.close()
def valid_segments():
# get valid_segments
# https://commoncrawl.atlassian.net/wiki/display/CRWL/About+the+Data+Set
k = bucket.get_key("common-crawl/parse-output/valid_segments.txt")
s = k.get_contents_as_string()
valid_segments = filter(None, s.split("\n"))
return valid_segments
# you might find this conversion function between DataFrame and a list of a regular dict useful
# https://gist.github.com/mikedewar/1486027#comment-804797
def df_to_dictlist(df):
return [{k:df.values[i][v] for v,k in enumerate(df.columns)} for i in range(len(df))]
def cc_file_type(path):
fname = path.split("/")[-1]
if fname[-7:] == '.arc.gz':
return 'arc.gz'
elif fname[:9] == 'textData-':
return 'textData'
elif fname[:9] == 'metadata-':
return 'metadata'
elif fname == '_SUCCESS':
return 'success'
else:
return 'other'
def segment_stats(seg_id, stop=None):
from pandas import DataFrame
all_files = islice(bucket.list(prefix="common-crawl/parse-output/segment/{0}/".format(seg_id), delimiter="/"),stop)
df = DataFrame([{'size': f.size if hasattr(f, 'size') else 0, 'name':f.name, 'type':cc_file_type(f.name)} for f in all_files])
return {'count': df_to_dictlist(df[['size','type']].groupby('type').count()[['size']].T)[0],
'size': df_to_dictlist(df[['size', 'type']].groupby('type').sum().astype('int64').T)[0]}
# another version of segment_stats that doesn't use DataFrame
def segment_stats2(seg_id, stop=None):
from collections import Counter
file_count = Counter()
byte_count = Counter()
all_files = islice(bucket.list(prefix="common-crawl/parse-output/segment/{0}/".format(seg_id), delimiter="/"),stop)
for f in all_files:
file_type = cc_file_type(f.name)
file_count.update({file_type: 1})
byte_count.update({file_type: f.size if hasattr(f, 'size') else 0})
return {'count': dict(file_count),
'size': dict(byte_count)}
def run_jobs(valid_segments, n_tasks=None, local=True, f=segment_stats2, env='/rdhyee/Working_with_Open_Data'):
# http://docs.picloud.com/cloud_cloudmp.html
if local:
CLOUD = cloud.mp
else:
CLOUD = cloud
jids = CLOUD.map(f, valid_segments[:n_tasks], _env=env)
return jids
def print_iresults(jids, local=True):
if local:
CLOUD = cloud.mp
else:
CLOUD = cloud
file_counter = Counter()
byte_counter = Counter()
problems = []
for (i, result) in enumerate(CLOUD.iresult(jids)):
try:
file_counter.update(result['count'])
byte_counter.update(result['size'])
print i, byte_counter['arc.gz']
except Exception as e:
print i, e
problems.append((seg_id, e))
def tally_results(jids, local=True):
"""tabulates results for jids"""
from pandas import DataFrame
if local:
CLOUD = cloud.mp
else:
CLOUD = cloud
jobs_info = CLOUD.info(jids,
info_requested=['created', 'finished', 'runtime', 'cputime']
)
jobs_counter= Counter()
[jobs_counter.update(dict([(k, v[k]) for k in ('cputime.system', 'cputime.user', 'runtime')])) for v in jobs_info.values()]
file_counter = Counter()
byte_counter = Counter()
problems = []
for (i, result) in enumerate(CLOUD.iresult(jids)):
try:
file_counter.update(result['count'])
byte_counter.update(result['size'])
except Exception as e:
print i, e
problems.append((seg_id, e))
# generate something to plot
started = [{'jid':k, 'time':v['finished'] - datetime.timedelta(seconds=v['runtime']), 'count': 1} for (k,v) in jobs_info.items()]
finished = [{'jid':k, 'time':v['finished'], 'count': -1} for (k,v) in jobs_info.items()]
df = DataFrame(started + finished)
return ({
'runtime': jobs_counter['runtime'],
'cost': (jobs_counter['runtime'])/3600. * 0.05,
'count': dict(file_counter),
'size': dict(byte_counter),
'problems':problems,
'cores_vs_time_x': df.sort_index(by='time')['time'],
'cores_vs_time_y': df.sort_index(by='time')['count'].cumsum()
})