forked from dancingfrog/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises.qmd
492 lines (391 loc) · 13.8 KB
/
exercises.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
```{r}
# source("data/setup.R")
library(arrow)
library(dplyr)
library(tidyr)
nyc_taxi <- arrow::open_dataset("data/nyc-taxi")
nyc_taxi
# FileSystemDataset with 36 Parquet files
# vendor_name: string
# pickup_datetime: timestamp[ms]
# dropoff_datetime: timestamp[ms]
# passenger_count: int64
# trip_distance: double
# pickup_longitude: double
# pickup_latitude: double
# rate_code: string
# store_and_fwd: string
# dropoff_longitude: double
# dropoff_latitude: double
# payment_type: string
# fare_amount: double
# extra: double
# mta_tax: double
# tip_amount: double
# tolls_amount: double
# total_amount: double
# improvement_surcharge: double
# congestion_surcharge: double
# pickup_location_id: int64
# dropoff_location_id: int64
# year: int32
# month: int32
nyc_taxi_group_by_year <- nyc_taxi |>
group_by(year)
library(tictoc)
tic()
nyc_taxi_summarize <- nyc_taxi_group_by_year |>
summarize(
all_trips = n(),
shared_trips = sum(passenger_count > 1, na.rm = TRUE),
grt_100 = sum(total_amount > 100)
)
nyc_taxi_summarize |> collect()
toc()
nyc_taxi_sum_with_pct_shared <-
nyc_taxi_summarize |>
mutate(pct_shared = shared_trips / all_trips * 100)
nyc_taxi_sum_with_pct_shared |> collect()
library(tictoc)
tic(); nyc_taxi_sum_with_pct_shared |> collect(); toc()
nyc_taxi_group_by_month <- nyc_taxi_group_by_year |>
group_by(month)
nyc_taxi_group_by_month_2019 <- nyc_taxi_group_by_year |>
filter(year == 2019) |>
group_by(month)
nyc_taxi_group_by_month_2019 |>
summarize(max_trip_distance = max(trip_distance))
nyc_taxi_max_distance_by_month_2019 <- nyc_taxi_group_by_month_2019 |>
summarize(max_trip_distance = max(trip_distance)) |>
arrange(month)
nyc_taxi_max_distance_by_month_2019 |> collect()
tic(); nyc_taxi_max_distance_by_month_2019 |> arrange(month) |> collect(); toc()
nyc_taxi_amount_grt_100 <- nyc_taxi_group_by_year |> filter(total_amount > 100)
nyc_taxi_amount_grt_100 |> nrow()
nyc_taxi_group_by_month_2020 <- nyc_taxi_group_by_year |>
filter(year == 2020) |>
group_by(month)
nyc_taxi_september_2020 <- nyc_taxi_group_by_month_2020 |>
filter(month == 9)
nyc_taxi_september_2020_vendors_ending_in_S <- nyc_taxi_september_2020 |>
filter(stringr::str_ends(vendor_name, "S"))
nyc_taxi_september_2020_vendors_ending_in_S |> nrow()
nyc_taxi_september_2020_vendors_ending_in_S |> head() |> collect()
fare_pounds <- nyc_taxi |>
mutate(fare_amount_pounds = fare_amount * 0.79) |>
head() |>
collect()
fare_pounds <- nyc_taxi |>
mutate(across(ends_with("amount"), list(pounds = ~.x * 0.79))) |>
select(contains("amount")) |>
head() |>
collect()
# ## Does not work...
# nyc_taxi |>
# group_by(vendor_name) |>
# summarise(max_fare = max(fare_amount)) |>
# pivot_longer(!vendor_name, names_to = "metric") |>
# collect()
# # Error in pivot_longer(summarise(group_by(nyc_taxi, vendor_name), max_fare = max(fare_amount)), :
# # could not find function "pivot_longer"
## Joins
vendors <- tibble::tibble(
code = c("VTS", "CMT", "DDS"),
full_name = c(
"Verifone Transportation Systems",
"Creative Mobile Technologies",
"Digital Dispatch Systems"
)
)
nyc_taxi |>
left_join(vendors, by = c("vendor_name" = "code")) |>
select(vendor_name, full_name, pickup_datetime) |>
head(3) |>
collect()
nyc_taxi_zones <- arrow_table(
read_csv_arrow("data/taxi_zone_lookup.csv") |>
select(location_id = LocationID,
borough = Borough)
)
nyc_taxi_zones
# Table
# 265 rows x 2 columns
# $location_id <int32>
# $borough <string>
nyc_taxi |>
left_join(nyc_taxi_zones, by = c("pickup_location_id" = "location_id")) |>
group_by(pickup_location_id) |>
summarize(
trips_to_pickup_zone = n()
) |>
collect()
# Error in `compute.arrow_dplyr_query()`:
# ! Invalid: Incompatible data types for corresponding join field keys: FieldRef.Name(pickup_location_id) of type int64 and FieldRef.Name(location_id) of type int32
# Hide Traceback
# ▆
# 1. ├─dplyr::collect(left_join(nyc_taxi, nyc_taxi_zones, by = c(pickup_location_id = "location_id")))
# 2. └─arrow:::collect.arrow_dplyr_query(...)
# 3. └─arrow:::compute.arrow_dplyr_query(x)
# 4. └─base::tryCatch(...)
# 5. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
# 6. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
# 7. └─value[[3L]](cond)
# 8. └─arrow:::augment_io_error_msg(e, call, schema = schema())
# 9. └─rlang::abort(msg, call = call)
nyc_taxi
nyc_taxi_zones_arrow <- arrow_table(
read_csv_arrow("data/taxi_zone_lookup.csv") |>
select(
location_id = LocationID,
borough = Borough,
pickup_zone = Zone,
service_zone
),
schema = schema(
location_id = int64(),
borough = utf8(),
pickup_zone = utf8(),
service_zone = utf8()
)
)
nyc_taxi_zones_arrow
# Table
# 265 rows x 4 columns
# $location_id <int64>
# $borough <string>
# $pickup_zone <string>
# $service_zone <string>
tic()
nyc_taxi |>
left_join(nyc_taxi_zones_arrow, by = c("pickup_location_id" = "location_id")) |>
group_by(pickup_location_id) |>
summarize(
trips_to_pickup_zone = n()
) |>
collect()
toc()
airport_pickups <- nyc_taxi |>
left_join(nyc_taxi_zones_arrow, by = c("pickup_location_id" = "location_id")) |>
filter(stringr::str_detect(pickup_zone, "Airport")) |>
group_by(pickup_zone) |>
summarize(
trips_to_pickup_zone = n()
)
tic()
airport_pickups |>
collect()
toc()
```
```{r}
## Schema stuff
seattle_csv <- arrow::open_dataset(source = "data/seattle-library-checkouts.csv", format = "csv")
seattle_schema <- seattle_csv$schema$code()
seattle_arrow <- arrow::open_dataset(
source = "data/seattle-library-checkouts.csv",
format = "csv",
col_types = schema(ISBN = string()),
# schema = schema(
# UsageClass = utf8(),
# CheckoutType = utf8(),
# MaterialType = utf8(),
# CheckoutYear = int64(),
# CheckoutMonth = int64(),
# Checkouts = int64(),
# Title = utf8(),
# ISBN = string(),
# Creator = utf8(),
# Subjects = utf8(),
# Publisher = utf8(),
# PublicationYear = utf8()
# )
# skip = 1
)
seattle_arrow |> nrow()
# [1] 41389466 # <= with complete schema, no skip = 1
# [1] 41389465
seattle_checkouts_by_year <- seattle_arrow |>
group_by(CheckoutYear) |>
summarise(sum(Checkouts))
seattle_checkouts_by_year |>
collect() |> system.time()
seattle_checkouts_by_year <- seattle_arrow |>
group_by(CheckoutYear)
seattle_checkouts_by_year |>
write_dataset(path = "data/seattle-checkout-parquet-demo",
format = "parquet")
arrow::open_dataset("data/seattle-checkout-parquet-demo") |>
filter(CheckoutYear == 2021, MaterialType == "BOOK") |>
group_by(CheckoutMonth) |>
summarise(TotalCheckout = sum(Checkouts)) |>
arrange(desc(CheckoutMonth)) |>
collect() |>
system.time()
seattle_checkouts_by_type <- seattle_arrow |>
group_by(CheckoutType)
seattle_checkouts_by_type |>
write_dataset(path = "data/seattle-checkout-parquet-demo2",
format = "parquet")
arrow::open_dataset("data/seattle-checkout-parquet-demo2") |>
filter(CheckoutYear == 2021, MaterialType == "BOOK") |>
group_by(CheckoutMonth) |>
summarise(TotalCheckout = sum(Checkouts)) |>
arrange(desc(CheckoutMonth)) |>
collect() |>
system.time()
```
```{r}
## Does not work in R....
rural_places <- open_dataset("s3://cori-risi-apps/examples/who-wins-b2s/rural_places_2500_plus.json")
# Error in `open_dataset()`:
# ! Invalid: Error creating dataset. Could not read schema from 'cori-risi-apps/examples/who-wins-b2s/rural_places_2500_plus.json'. Is this a 'parquet' file?: Could not open Parquet input source 'cori-risi-apps/examples/who-wins-b2s/rural_places_2500_plus.json': Parquet magic bytes not found in footer. Either the file is corrupted or this is not a parquet file.
# ℹ Did you mean to specify a 'format' other than the default (parquet)?
# Run `rlang::last_trace()` to see where the error occurred.
```
```{r}
## DuckDB stuff ----------------------------------------------------------------
if (!require(duckdb)) {
install.packages("duckdb")
library(duckdb)
}
library(DBI)
# # to start an in-memory database
# con <- dbConnect(duckdb())
# to use a local database file
con <- DBI::dbConnect(duckdb::duckdb(), dbdir = "data/posit-conf-arrow.duckdb")
# # This crashed my session, btw, so you probably want to pipe the result to additional functions...
# nyc_taxi |> arrow::to_duckdb(con = con)
# This "works", but the result is not an arrow object
nyc_taxi_to_from_duckdb <- nyc_taxi |>
to_duckdb() |> # send data to duckdb |>
to_arrow() # return data back to arrow
# # This does not work (crashes)
# nyc_taxi |> arrow::to_duckdb(con = con) |> arrow::to_arrow() |> nrow()
# This also does not work (crashes)...
nyc_taxi_to_from_duckdb <- nyc_taxi |>
to_duckdb(con = con) |> # send data to duckdb |>
to_arrow() # return data back to arrow
filter(year == 2019) |>
group_by(month) |>
summarize(
all_trips = n(),
shared_trips = sum(passenger_count > 1, na.rm = TRUE),
grt_100 = sum(total_amount > 100)
) |>
collect()
# Use duckdb to pivot (without specified con)...
nyc_taxi_pivot <- nyc_taxi |>
group_by(vendor_name) |>
summarise(max_fare = max(fare_amount)) |>
to_duckdb() |> # send data to duckdb
tidyr::pivot_longer(!vendor_name, names_to = "metric") |>
to_arrow() # return data back to arrow
nyc_taxi_pivot |> collect()
# Use duckdb to pivot...
nyc_taxi_pivot <- nyc_taxi |>
group_by(vendor_name) |>
summarise(max_fare = max(fare_amount)) |>
to_duckdb(con = con) |> # send data to duckdb
tidyr::pivot_longer(!vendor_name, names_to = "metric") |>
to_arrow() # return data back to arrow
nyc_taxi_pivot |> nyc_taxi_pivotcollect()
dbDisconnect(con)
```
```{r}
### AWS S3 <=> JSON stuff.... ----------------------------------------------------
library(duckdb)
if (!dir.exists("data")) dir.create("data")
con <- DBI::dbConnect(duckdb::duckdb(), dbdir = "data/posit-conf-arrow.duckdb")
dbExecute(con, "INSTALL json;")
dbExecute(con, "LOAD json;")
dbExecute(con, "INSTALL httpfs;")
dbExecute(con, "LOAD httpfs;")
dbExecute(con, "INSTALL aws;")
dbExecute(con, "LOAD aws;")
dbExecute(con, "
CREATE TABLE IF NOT EXISTS example (j JSON);
INSERT INTO example VALUES
('{\"family\": \"anatidae\", \"species\": [\"duck\", \"goose\"], \"coolness\": 42.42}'),
('{\"family\": \"canidae\", \"species\": [\"labrador\", \"bulldog\"], \"hair\": true}');
");
example_json <- dbGetQuery(con, "SELECT * FROM example")
example_json_transform <- dbGetQuery(con, "SELECT json_transform(j, '{\"family\": \"VARCHAR\", \"coolness\": \"DOUBLE\"}') FROM example;")
rural_places_db <- dbGetQuery(con, "SELECT features FROM 's3://cori-risi-apps/examples/who-wins-b2s/rural_places_2500_plus.json'")
geojson_collection_struct <- dbGetQuery(con, "
SELECT typeof(json_transform('{}', '{
\"type\": \"VARCHAR\",
\"name\": \"VARCHAR\",
\"crs\": { \"type\": \"VARCHAR\", \"properties\": { \"name\": \"VARCHAR\" } },
\"features\": [
{ \"type\": \"VARCHAR\", \"properties\": \"VARCHAR\", \"geometry\": { \"type\": \"VARCHAR\", \"coordinates\": [ \"DOUBLE\" ] } }
]
}'));
")[1,]
dbExecute(con, "
CREATE TABLE IF NOT EXISTS rural_places as
SELECT *
FROM read_json(
's3://cori-risi-apps/examples/who-wins-b2s/rural_places_2500_plus.json',
columns={
\"type\": \"VARCHAR\",
\"name\": \"VARCHAR\",
\"crs\": \"STRUCT(
type VARCHAR,
properties STRUCT(name VARCHAR)
)\",
\"features\": \"STRUCT(
type VARCHAR,
properties VARCHAR,
geometry STRUCT(
type VARCHAR,
coordinates DOUBLE[]
)
)[]\"
}
)
")
rural_places_df <- dbGetQuery(con, "SELECT features from rural_places")$features[[1]] |> as.data.frame()
rural_places_df |> nrow()
# [1] 2882
rural_places_arrow <- rural_places_df |> to_duckdb() |> to_arrow()
rural_places_arrow |> nrow()
# [1] NA
rural_places_arrow |> collect() |> nrow()
# [1] 2882
rural_places_properties <- lapply(rural_places_df$properties |> as.list(), jsonlite::fromJSON)
rural_places_geometry <- rural_places_df$geometry # <= TODO: What type is this???
# ... how to include in parquet?
rural_places_props <- data.frame(matrix(ncol=length(rural_places_properties[[1]]),nrow=0, dimnames=list(NULL, names(rural_places_properties[[1]]))))
rural_places_funky <- list()
# Convert list of lists to data.frame... better way to do this?
for (r in c(1:length(rural_places_properties))) {
for (c in c(1:length(rural_places_properties[[r]]))) {
# Replace NULLs with "NA"
rural_places_properties[[r]][sapply(rural_places_properties[[r]], is.null)] <- NA
if (length(unlist(rural_places_properties[[r]])) == length(rural_places_properties[[1]])) { # <= Drop any record with missing (null or funky) columns
rural_places_props[r,] <- unlist(rural_places_properties[[r]])
} else {
append(rural_places_funky, rural_places_properties[[r]])
}
}
}
rural_places_arrow_by_state <- rural_places_props |>
to_duckdb() |>
to_arrow() |>
group_by(STUSPS)
rural_places_arrow_by_state |>
write_dataset(path = "data/rural_places",
format = "parquet")
rural_places <- arrow::open_dataset("data/rural_places") # <= also available at s3://cori-risi-apps/examples/who-wins-b2s/rural_places
# rural_places <- dbGetQuery(con, "SELECT * FROM parquet_scan('s3://cori-risi-apps/examples/who-wins-b2s/rural_places/STUSPS=*/*.parquet')") |>
# to_duckdb() |>
# to_arrow()
rural_places_pc_logit_score <- rural_places |>
mutate(prop_score = as.double(pc_logit_prediction))
rural_places_max_prop_by_state <- rural_places_pc_logit_score |>
group_by(STUSPS) |>
summarize(
max_prop_score = max(as.double(prop_score), na.rm = TRUE)
) |>
collect()
dbDisconnect(con)
```