-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_23.R
135 lines (101 loc) · 2.66 KB
/
db_23.R
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
library(DBI)
library(duckdb)
library(dplyr)
library(dbplyr)
### Database dumps #############################################################
# Connection -------------------------------------------------------------------
if (fs::file_exists("flights.duckdb")) {
fs::file_delete("flights.duckdb")
}
con_rw <- dbConnect(duckdb::duckdb(), dbdir = "flights.duckdb")
flights_duckdb <- copy_to(
con_rw,
nycflights13::flights,
name = "flights",
temporary = FALSE
)
dbDisconnect(con_rw)
# Exploration ----
con <- dbConnect(
duckdb::duckdb(),
dbdir = "flights.duckdb",
read_only = TRUE
)
flights_duckdb <- tbl(con, "flights")
# Method 1: via local data frame ----
flights_duckdb |>
filter(month == 1) |>
collect() |>
duckplyr::df_to_parquet("flights-jan.parquet")
flights_duckdb |>
collect() |>
duckplyr::df_to_parquet("flights.parquet")
# Method 2: via DBI ----
sql_jan <- flights_duckdb |>
filter(month == 1) |>
dbplyr::sql_render()
fs::dir_create("flights-arrow")
res <- dbSendQuery(con, sql_jan)
i <- 0
repeat {
df <- dbFetch(res, n = 10000)
if (nrow(df) == 0) break
path <- fs::path("flights-arrow", sprintf("part-%05d.parquet", i))
duckplyr::df_to_parquet(df, path)
i <- i + 1
message("Written ", nrow(df), " rows to ", path)
}
dbClearResult(res)
fs::dir_tree("flights-arrow/")
# Method 3: via parquetize ----
parquetize::dbi_to_parquet(
con,
sql_jan,
"flights-parquetized",
max_rows = 10000
)
fs::dir_tree("flights-parquetized/")
# Method 4: via DBI and arrow ----
con_adbi <- dbConnect(
adbi::adbi(duckdb::duckdb_adbc()),
path = "flights.duckdb"
)
sql <- "SELECT * FROM flights"
res <- dbSendQueryArrow(con_adbi, sql)
stream <- dbFetchArrow(res)
arrow::write_dataset(
arrow::as_record_batch_reader(stream),
"flights-adbi/"
)
dbClearResult(res)
# Partitions ----
nycflights13::flights |>
arrow::write_dataset(
"flights-part-arrow/",
partitioning = "month"
)
fs::dir_tree("flights-part-arrow/")
# Adding partitions to a dataset ----
write_month <- function(month) {
sql <- flights_duckdb |>
filter(month == !!month) |>
dbplyr::sql_render()
dir <- fs::path(
"flights-part-manual",
sprintf("month=%d", month)
)
fs::dir_create(dir)
df <- dbGetQuery(con, sql)
duckplyr::df_to_parquet(
df,
fs::path(dir, "part-0.parquet")
)
}
write_month(1)
write_month(2)
write_month(3)
fs::dir_tree("flights-part-manual")
# Exercises -------------------------------------------------------------------------
# 1. Write code to create a partitioned dataset with the `flights` table,
# partitioned by `origin`.
# - Hint: The dataset only contains flights departing from New York City airports.