-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.qmd
547 lines (381 loc) · 13.5 KB
/
index.qmd
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
---
format:
revealjs:
theme: ["theme/q-theme.scss"]
slide-number: c/t
logo: "https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Flat.png"
footer: "[https://jthomasmock.github.io/arrow-dplyr](https://jthomasmock.github.io/arrow-dplyr)"
code-copy: true
center-title-slide: false
include-in-header: heading-meta.html
code-link: true
code-overflow: wrap
highlight-style: a11y
height: 1080
width: 1920
execute:
eval: true
echo: true
---
<h1> Outrageously efficient<br>exploratory data analysis </h1>
<h2> with Apache Arrow and `dplyr` </h2>
<hr>
<h3> Tom Mock, Customer Enablement Lead at </h3>
<h3> 2022-06-03 </h3>
<br>
<h3> `r fontawesome::fa("github", "black")` [github.com/jthomasmock/arrow-dplyr](https://github.com/jthomasmock/arrow-dplyr)
![](https://arrow.apache.org/img/offbrand_hex_2.png){.absolute top=425 left=1100 width="300"}
![](https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/dplyr.png){.absolute top=680 left=1250 width="300"}
![](https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Flat.png){.absolute top=360 left=965 height="65"}
## `arrow`
> `arrow` is software development platform for building high performance applications that process and transport large data sets
. . .
* The `arrow` R package is an interface to data via the `arrow` backend, and has deep integration with `dplyr`:
- Ungrouped `mutate()`, `filter()`, `select()` was available in `arrow` 5.0
- `group_by()` + `summarize()` aggregation was added in `arrow` 6.0
- More complex `dplyr` operations were added in `arrow`7.0 and 8.0
. . .
* `arrow` data can also be "handed off" to `duckdb` with `to_duckdb()` for any `dbplyr` commands without data conversion. IE no serialization or data copying costs are incurred.
## Working with bigger data?
* Relational databases (IE SQL) are still around and hugely popular but...
. . .
* Data and specifically _local_ files are getting bigger
. . .
* Additionally, many Data Warehouses/Data Lakes use flat-file storage (`.csv`, `.parquet`, `.json` etc) - there are query engines in many environments, but you can often end up with large extracts.
. . .
So, how do you work with data extracts that aren't already in a database, and are bigger than your memory?
## Pause for one second
If it _can_ fit in memory, then try out:
* [`vroom::vroom()`](https://vroom.r-lib.org/) or [`data.table::fread()`](https://rdatatable.gitlab.io/data.table/reference/fread.html) for fast file reads _into_ R
* [`vroom(col_select = c(column_name))`](https://vroom.r-lib.org/reference/vroom.html) also allows for partial reads (ie specific columns)
* `arrow` itself also has crazy fast file reads on many file types
. . .
* [`data.table`](https://rdatatable.gitlab.io/data.table/index.html) or the `dplyr` front-end to `data.table` via [`dtplyr`](https://dtplyr.tidyverse.org/) for fast and efficient in-memory analysis
. . .
* Lastly, the [`collapse`](https://sebkrantz.github.io/collapse/) R package for limited capability, but hyper-performant data manipulation
## Fill your quiver with `arrow`s
```{r}
library(arrow) # interface to arrow
library(dplyr) # expressive and consistent interface for data analysis
library(tictoc) # timing out computations
```
[Using `tictoc` to [watch cute dog videos]{.fragment .strike fragment-index=2}]{.fragment fragment-index=1} [time our computations.]{.fragment fragment-index=2}
:::: {.columns .fragment fragment-index=1}
::: {.column width="50%"}
![](https://media0.giphy.com/media/EOIQA7mySTulzp7PoR/giphy.gif?cid=ecf05e47o0cq51yelvgpewdyud8ya013gejduf351z5233lq&rid=giphy.gif&ct=g){width="600"}
![](https://media4.giphy.com/media/DIrGyd84DwA48/giphy.gif?cid=ecf05e47977mxiw5gkfbd23znh0gxgra4kcqxa82c5e0laqs&rid=giphy.gif&ct=g){width="600"}
:::
::: {.column width="50%"}
![](https://media0.giphy.com/media/348lrWhJAUiuhkcePl/giphy.gif?cid=ecf05e476kk82d9kk7u3rub2wec0qpz2c32kxt2504hf1p7v&rid=giphy.gif&ct=g){width="600"}
![](https://media4.giphy.com/media/OVz2F93KQzSqQ/giphy.gif?cid=ecf05e476kk82d9kk7u3rub2wec0qpz2c32kxt2504hf1p7v&rid=giphy.gif&ct=g){width="600"}
:::
::::
## On to `arrow`
There are great examples of data analysis on big data (2 billion rows) in the [`arrow` docs](https://arrow.apache.org/docs/r/articles/dataset.html).
. . .
For today, I'm going to focus on biggish data but manageable data!
. . .
<hr>
If we were to use CSVs, this would be about 2.19 GB of data
```{r}
fs::dir_info("data-csv") |> summarise(size = sum(size)) |> pull()
```
. . .
But because we're using `arrow`, we can use more efficient parquet files. This data on disk is about 82% smaller at 388 MB.
```{r}
fs::dir_info("data-parquet", recurse = TRUE) |> summarise(size = sum(size)) |> pull()
```
. . .
So not _THAT_ big but 372 columns and 1.1 million rows is plenty.
```{r}
tic()
ds <- arrow::open_dataset("data-parquet", partitioning = "season")
dims <- glue::glue(
"{ds |> names() |> length()} cols by ",
"{scales::label_number(big.mark = ',')(ds |> count() |> collect() |> pull())} rows"
)
toc()
```
```{r, echo = FALSE}
dims
```
## `nflfastR` data
The data we're focused on today is big enough (1 million rows by 372 columns, about 2.2 GB uncompressed) and corresponds to _every_ NFL play in _every_ game from 1999 to 2021. The data is all available from the [{nflverse} Github](https://github.com/nflverse/nflfastR-data).
. . .
To use it efficiently, we've partitioned the data up into each season
:::: {.columns}
::: {.column width="50%"}
```
data-parquet/
├── 1999
│ └── data.parquet
├── 2000
│ └── data.parquet
├── 2001
│ └── data.parquet
├── 2002
│ └── data.parquet
├── 2003
│ └── data.parquet
├── 2004
│ └── data.parquet
├── 2005
│ └── data.parquet
├── 2006
│ └── data.parquet
├── 2007
│ └── data.parquet
├── 2008
│ └── data.parquet
├── 2009
│ └── data.parquet
├── 2010
│ └── data.parquet
├── 2011
│ └── data.parquet
├── 2012
│ └── data.parquet
├── 2013
│ └── data.parquet
├── 2014
│ └── data.parquet
```
:::
::: {.column width="50%"}
```
├── 2015
│ └── data.parquet
├── 2016
│ └── data.parquet
├── 2017
│ └── data.parquet
├── 2018
│ └── data.parquet
├── 2019
│ └── data.parquet
├── 2020
│ └── data.parquet
└── 2021
└── data.parquet
```
:::
::::
## Nock the `arrow`
We can prepare our data to be used with `arrow::open_dataset()`
```{r}
ds <- open_dataset("data-parquet", partitioning = "season")
ds
```
## Pull the `arrow` back with `dplyr`
```{r}
#| code-line-numbers: "|8"
summarize_df <- ds |>
filter(season == 2021, play_type %in% c("pass", "run")) |>
filter(!is.na(epa)) |>
select(posteam, epa) |>
group_by(posteam) |>
summarize(n = n(), avg_epa = mean(epa))
print(summarize_df)
```
. . .
Note that while the computation has occurred, we really haven't "seen it" yet. Printing just reports back the 3x columns and their type.
## Release the `arrow` into memory with `collect()`
We can execute the `collect()` function to _finally_ pull the output into memory and display the result.
. . .
```{r collectArrange}
#| code-line-numbers: "|2"
summarize_df |>
collect() |>
arrange(desc(avg_epa))
```
## Release the `arrow` into memory with `collect()`
Once it's pulled into memory, it's like any other in-memory object!
. . .
```{r, fig.dim=c(8,3), dpi=500, fig.align='center'}
library(ggplot2)
collect(summarize_df) |>
ggplot(aes(x = forcats::fct_reorder(posteam, desc(avg_epa)), y = avg_epa)) +
theme_minimal() + geom_hline(yintercept = 0, color = "black") +
geom_col(aes(color = posteam, fill = posteam), width = 0.75) +
nflplotR::scale_fill_nfl(alpha = 0.75, aesthetics = c("color", "fill")) +
guides(color = "none", fill = "none") +
theme(axis.text.x = nflplotR::element_nfl_logo()) +
labs(x = "", y = "Average Expected Points Added", title = "2021 Offensive Expected Points Added")
```
## Bigger and faster
We can operate across all the rows extremely quickly!
```{r}
tic()
all_comp <- ds |>
filter(!is.na(epa)) |>
group_by(posteam, play_type) |>
summarize(
n = n(),
avg_epa = mean(epa),
.groups = "drop"
) |>
collect() |>
mutate(total = scales::label_number(big.mark = ",")(sum(n)))
toc()
```
```{r, echo=FALSE}
all_comp
```
## Better exploratory data analysis
While `arrow` + `dplyr` can be combined for extremely efficient and fast data analysis, having to `collect()` into memory when the results may be very large is not ideal.
. . .
Enter the [`arrow::to_duckdb()` function](https://arrow.apache.org/docs/r/reference/to_duckdb.html)! This is essentially a zero-cost operation that will treat the on-disk data in place as a [`duckdb` database](https://duckdb.org/)!
. . .
```{r}
tic()
duck_out <- ds |>
select(posteam, play_type, season, defteam, epa) |>
filter(!is.na(epa)) |>
to_duckdb() |>
filter(epa >= 0)
toc()
```
```{r, echo=FALSE}
duck_out
```
## More `arrow`s for more `duckdb`s
:::: {.columns}
::: {.column width="45%"}
```{r}
ds |>
select(posteam, play_type, season, epa) |>
filter(
!is.na(epa),
play_type %in% c("run", "pass")) |>
to_duckdb() |>
filter(epa >= 0)
```
`lazy query [?? x 4]` indicates a `dbplyr` connection, prints 10 rows and the remaining dataset hasn't been pulled into memory yet!
:::
::: {.column width="45%"}
:::
::::
## More `arrow`s for more `duckdb`s
:::: {.columns}
::: {.column width="45%"}
```{r}
ds |>
select(posteam, play_type, season, epa) |>
filter(
!is.na(epa),
play_type %in% c("run", "pass")) |>
to_duckdb() |>
filter(epa >= 0)
```
`lazy query [?? x 4]` indicates a `dbplyr` connection, prints 10 rows and the remaining dataset hasn't been pulled into memory yet!
:::
::: {.column width="45%"}
```{r}
#| code-line-numbers: "|9|10"
ds |>
select(posteam, play_type, season, epa) |>
filter(!is.na(epa)) |>
to_duckdb() |>
filter(
epa >= 0,
play_type %in% c("run", "pass")
) |>
arrange(desc(epa)) |>
print(n = 22)
```
We can easily print more!
:::
::::
## Rapid fire question -> answer
Just like with `dplyr` in memory, you can write and answer queries almost as fast as you can think them up!
. . .
```{r}
tic()
ds |>
select(posteam, play_type, season, defteam, epa) |>
filter(!is.na(epa)) |>
to_duckdb() |>
filter(epa >= 5)
toc()
```
## `duckdb` adds more options
Note that it _also_ opens up additional functions via `dbplyr` that may not be added yet into `arrow`'s conversion layer.
:::: {.columns}
::: {.column width="45%"}
```{r, error=TRUE}
ds |>
select(posteam, play_type, epa) |>
mutate(total_n = n()) |>
group_by(posteam) |>
summarize(n = n(), total = min(total_n))
```
:::
::::
## `duckdb` adds more options
Note that it _also_ opens up additional functions via `dbplyr` that may not be added yet into `arrow`'s conversion layer.
:::: {.columns}
::: {.column width="45%"}
```{r, error=TRUE}
ds |>
select(posteam, play_type, epa) |>
mutate(total_n = n()) |>
group_by(posteam) |>
summarize(n = n(), total = min(total_n))
```
:::
::: {.column width="45%"}
```{r, warning=FALSE}
#| code-line-numbers: "|3"
ds |>
select(posteam, play_type, epa) |>
to_duckdb() |>
mutate(total_n = n()) |>
group_by(posteam) |>
summarize(n = n(), total = min(total_n))
```
:::
::::
## All together now
`dplyr`, `arrow`, and `duckdb` form a powerful trifecta for efficiently and effectively exploring large datasets.
. . .
Once you have explored and want to bring it into memory, it's also fast!
. . .
```{r}
read_arrow <- function(){
dim(df <- arrow::open_dataset("data-parquet", partitioning = "season") |> filter(season == 2021) |> collect())
}
read_dt <- function() dim(df_dt <- data.table::fread("data-csv/2021.csv"))
read_vroom <- function() dim(dfv <- vroom::vroom("data-csv/2021.csv"))
read_csv_arrow <- function() dim(dfc <- arrow::read_csv_arrow("data-csv/2021.csv"))
read_csv_readr <- function() dim(dfc <- readr::read_csv("data-csv/2021.csv", lazy = FALSE))
```
. . .
```{r, message=FALSE, warning=FALSE, cache=TRUE}
tibble(dim = c("rows", "cols"), arrow = read_arrow(), dt = read_dt(), vroom = read_vroom(), arrow_csv = read_csv_arrow(), readr = read_csv_readr())
```
## We can `bench::mark()`
```{r, eval=FALSE,echo=FALSE}
read_arrow <- \() all_21_arrow <- ds |> filter(season == 2021) |> collect()
read_dt <- \() all_21_dt <- data.table::fread("data-csv/2021.csv", nThread = 10)
read_vroom <- \() all_21_dt <- vroom::vroom("data-csv/2021.csv")
read_csv_arrow <- \() all_21_dt <- arrow::read_csv_arrow("data-csv/2021.csv")
```
All are pretty fast!
```{r, cache=TRUE, message=FALSE, warning = FALSE}
bench::mark(
read_arrow(), # arrow::open_dataset()
read_dt(), # data.table::fread()
read_vroom(), # vroom::vroom()
read_csv_arrow(), # arrow::read_arrow_csv()
read_csv_readr(), # readr::read_csv()
min_time = 0.1, iterations = 10
) |>
arrange(median)
```
. . .
But again - the beauty of `arrow` is not just that it's fast!
. . .
It's fast at exploring the data _BEFORE_ even having to wait for long reads OR having to get a workstation with enough memory to read it all in and compute on it!
. . .
So go out and use `arrow` + `dplyr` with `duckdb` for outrageously efficient exploratory data analysis!
## {background-image="howard.jpg" background-size="contain"}