-
Notifications
You must be signed in to change notification settings - Fork 4
/
README.Rmd
184 lines (143 loc) · 3.95 KB
/
README.Rmd
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
184
---
title: "sparkwarc - WARC files in sparklyr"
output:
github_document:
fig_width: 9
fig_height: 5
---
# Install
Install using with:
```{r eval=FALSE}
devtools::install_github("javierluraschi/sparkwarc")
```
# Intro
The following example loads a very small subset of a WARC file from [Common Crawl](http://commoncrawl.org), a nonprofit 501 organization that crawls the web and freely provides its archives and datasets to the public.
```{r message=FALSE}
library(sparkwarc)
library(sparklyr)
library(DBI)
library(dplyr)
```
```{r connect-1, max.print=10}
sc <- spark_connect(master = "local")
```
```{r load-sample}
spark_read_warc(sc, path = spark_warc_sample_path(), name = "WARC")
```
```{sql query-1, connection=sc, max.print=1}
SELECT count(value)
FROM WARC
WHERE length(regexp_extract(value, '<html', 0)) > 0
```
```{r functions-1}
cc_regex <- function(ops) {
ops %>%
filter(regval != "") %>%
group_by(regval) %>%
summarize(count = n()) %>%
arrange(desc(count)) %>%
head(100)
}
cc_stats <- function(regex) {
tbl(sc, "warc") %>%
transmute(regval = regexp_extract(value, regex, 1)) %>%
cc_regex()
}
```
```{r query-2}
cc_stats("http-equiv=\"Content-Language\" content=\"(.*)\"")
```
```{r query-3}
cc_stats("<script .*src=\".*/(.+)\".*")
```
```{r query-4}
cc_stats("<([a-zA-Z]+)>")
```
```{r query-5}
cc_stats(" ([a-zA-Z]{5,10}) ")
```
```{r query-6}
cc_stats("<meta .*keywords.*content=\"([^,\"]+).*")
```
```{r query-7}
cc_stats("<script .*src=\".*/([^/]+.js)\".*")
```
```{r disconnect-1}
spark_disconnect(sc)
```
# Querying 1GB
```{r download-1}
warc_big <- normalizePath("~/cc.warc.gz") # Name a 5GB warc file
if (!file.exists(warc_big)) # If the file does not exist
download.file( # download by
gsub("s3n://commoncrawl/", # mapping the S3 bucket url
"https://commoncrawl.s3.amazonaws.com/", # into a adownloadable url
sparkwarc::cc_warc(1)), warc_big) # from the first archive file
```
```{r connect-2}
config <- spark_config()
config[["spark.memory.fraction"]] <- "0.9"
config[["spark.executor.memory"]] <- "10G"
config[["sparklyr.shell.driver-memory"]] <- "10G"
sc <- spark_connect(master = "local", config = config)
```
```{r load-full}
spark_read_warc(
sc,
"warc",
warc_big,
repartition = 8)
```
df <- data.frame(list(a = list("a,b,c")))
```{sql query-8, connection=sc, max.print=1}
SELECT count(value)
FROM WARC
WHERE length(regexp_extract(value, '<([a-z]+)>', 0)) > 0
```
```{sql query-9, connection=sc, max.print=1}
SELECT count(value)
FROM WARC
WHERE length(regexp_extract(value, '<html', 0)) > 0
```
```{r query-10}
cc_stats("http-equiv=\"Content-Language\" content=\"([^\"]*)\"")
```
```{r query-11}
cc_stats("WARC-Target-URI: http://([^/]+)/.*")
```
```{r query-12}
cc_stats("<([a-zA-Z]+)>")
```
```{r query-13}
cc_stats("<meta .*keywords.*content=\"([a-zA-Z0-9]+).*")
```
```{r disconnect-2}
spark_disconnect(sc)
```
# Querying 1TB
By [running sparklyr in EMR](https://aws.amazon.com/blogs/big-data/running-sparklyr-rstudios-r-interface-to-spark-on-amazon-emr/), one can configure an EMR cluster and load about **~5GB** of data using:
```{r eval=FALSE}
sc <- spark_connect(master = "yarn-client")
spark_read_warc(sc, "warc", cc_warc(1, 1))
tbl(sc, "warc") %>% summarize(n = n())
spark_disconnect_all()
```
To read the first 200 files, or about **~1TB** of data, first scale the cluster, consider maximizing resource allocation with the followin EMR config:
```
[
{
"Classification": "spark",
"Properties": {
"maximizeResourceAllocation": "true"
}
}
]
```
Followed by loading the `[1, 200]` file range with:
```{r eval=FALSE}
sc <- spark_connect(master = "yarn-client")
spark_read_warc(sc, "warc", cc_warc(1, 200))
tbl(sc, "warc") %>% summarize(n = n())
spark_disconnect_all()
```
To **query ~1PB** for the entire crawl, a custom script would be needed to load all the WARC files.