|
| 1 | +# Date Ingestion from SQL: A Commented Example |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +[TileDB](https://www.tiledb.com/) provides the *Universal Data Engine* |
| 6 | +that can be accessed in a variety of ways. Users sometimes wonder how to |
| 7 | +transfer data from existing databases. This short vignettes shows an |
| 8 | +example relying on the [DBI](https://cran.r-project.org/package=DBI) |
| 9 | +package for R. It offers a powerful and convenient abstraction layer on |
| 10 | +top a number of database backends with connection packages that adhere |
| 11 | +to, and utilise, the DBI framework. Some examples are the packages |
| 12 | +(listed in alphabetical order) |
| 13 | +[duckdb](https://cran.r-project.org/package=duckdb), |
| 14 | +[RClickhouse](https://cran.r-project.org/package=RClickhouse), |
| 15 | +[RGreenplum](https://cran.r-project.org/package=RGreenplum), |
| 16 | +[RJDBC](https://cran.r-project.org/package=RJDBC), |
| 17 | +[RMariaDB](https://cran.r-project.org/package=RMariaDB), |
| 18 | +[RMySQL](https://cran.r-project.org/package=RMySQL), |
| 19 | +[ROracle](https://cran.r-project.org/package=ROracle), |
| 20 | +[RPostgres](https://cran.r-project.org/package=RPostgres), |
| 21 | +[RPostgreSQL](https://cran.r-project.org/package=RPostgreSQL), |
| 22 | +[RPresto](https://cran.r-project.org/package=RPresto), |
| 23 | +[RRedshiftSQL](https://cran.r-project.org/package=RRedshiftSQL), |
| 24 | +[RSQLite](https://cran.r-project.org/package=RSQLite), and many more as |
| 25 | +seen via the [CRAN page](https://cran.r-project.org/package=DBI). |
| 26 | + |
| 27 | +We provide a simple example using |
| 28 | +[RPostgreSQL](https://cran.r-project.org/package=RPostgreSQL) and an |
| 29 | +existing database of historical stockmarket price data. |
| 30 | + |
| 31 | +## Load Required Packages |
| 32 | + |
| 33 | +The basic setup is straightforward. We load the required package |
| 34 | +[RPostgreSQL](https://cran.r-project.org/package=RPostgreSQL) which in |
| 35 | +turn imports [DBI](https://cran.r-project.org/package=DBI) as well as |
| 36 | +[tiledb](https://cran.r-project.org/package=tiledb). We use |
| 37 | +[data.table](https://cran.r-project.org/package=data.table) for its |
| 38 | +print method, the [tibble](https://cran.r-project.org/package=tibble) |
| 39 | +package offers an alternative): |
| 40 | + |
| 41 | +``` r |
| 42 | +library(RPostgreSQL) |
| 43 | +library(data.table) |
| 44 | +library(tiledb) |
| 45 | +``` |
| 46 | + |
| 47 | +## Connect to Database |
| 48 | + |
| 49 | +This step uses the DBI abstraction. A compliant backend driver can be |
| 50 | +loaded via `dbDriver`, and a connection can be established via |
| 51 | +`dbConnect` using appropriate arguments `dbname`, `user`, `password`, |
| 52 | +`host`, and `port`, as needed, with proper dispatching the |
| 53 | +implementation provided by the driver. The details depend on the chosen |
| 54 | +backend, this can be as simple as |
| 55 | +`con <- dbConnect(RSQLite::SQLite(), ":memory:")` in the case of |
| 56 | +[RSQLite](https://cran.r-project.org/package=RSQLite) and an in-memory |
| 57 | +(and likely transient) database. |
| 58 | + |
| 59 | +``` r |
| 60 | +## a local SQL db we have here -- about 617k rows |
| 61 | +dbSetup <- function() { |
| 62 | + drv <- dbDriver("PostgreSQL") |
| 63 | + con <- dbConnect(drv, |
| 64 | + user="...omitted...", |
| 65 | + password="...omitted...", # Could use e.g. Sys.getenv("DB_PASSWD") |
| 66 | + dbname="...omitted...") |
| 67 | + con |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Fetch Data |
| 72 | + |
| 73 | +In the next step we fetch the data—and for simplicity issue just one |
| 74 | +`select` statement returning a single `data.frame` (or here a |
| 75 | +`data.table` variant). In larger-than-memory settings the SQL query |
| 76 | +could easily bucket by symbols, or date range, or … |
| 77 | + |
| 78 | +``` r |
| 79 | +getDataFromSQL <- function() { |
| 80 | + con <- dbSetup() |
| 81 | + sql <- "select * from stockprices order by symbol, date;" |
| 82 | + res <- dbGetQuery(con, sql) |
| 83 | + dbDisconnect(con) |
| 84 | + setDT(res) # create data.table |
| 85 | + res |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Writing Data to TileDB |
| 90 | + |
| 91 | +Having read the data into memory we can use the TileDB R function |
| 92 | +`fromDataFrame`. It has numerous option to configure, as well as |
| 93 | +sensible defaults (to for example enable ZSTD compression). Here we |
| 94 | +select the first two columns for symbol and data as dimensions. Symbols, |
| 95 | +being text, do not set a domain set. For the date we set two ‘safe’ |
| 96 | +outer values for the range. |
| 97 | + |
| 98 | +``` r |
| 99 | +storeDataTDB <- function(dat, uri) { |
| 100 | + fromDataFrame(dat, uri, |
| 101 | + col_index=1:2, |
| 102 | + tile_domain=list(date=c(as.numeric(as.Date("1985-01-01")), |
| 103 | + as.numeric(as.Date("2030-12-31"))))) |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +The `mode="append"` argument of `fromDataFrame` can be used to append to |
| 108 | +an existing array to support chunked operation. |
| 109 | + |
| 110 | +### Reading Data Back In |
| 111 | + |
| 112 | +Reading data from TileDB is a very standard operation of opening the |
| 113 | +URI, possibly specifying the return type and possibly subsetting by |
| 114 | +dimension values, or attributes. Here, for simplicity, we just read |
| 115 | +everything. |
| 116 | + |
| 117 | +``` r |
| 118 | +getDataTDB <- function(uri) { |
| 119 | + set_allocation_size_preference(1e7) # larger than local default value |
| 120 | + arr <- tiledb_array(uri, return_as="data.frame") |
| 121 | + res <- arr[] |
| 122 | + res |
| 123 | +} |
| 124 | + |
| 125 | +uri <- "/tmp/tiledb/beancounter" |
| 126 | + |
| 127 | +res <- getDataFromSQL(con) |
| 128 | +storeData(dat, uri) |
| 129 | +chk <- getDataTDB(uri) |
| 130 | +print(dim(chk)) |
| 131 | +cat("Done!\n") |
| 132 | +``` |
| 133 | + |
| 134 | +## See Also |
| 135 | + |
| 136 | +The vignette [TileDB MariaDB |
| 137 | +Examples](https://tiledb-inc.github.io/TileDB-R/articles/tiledb-mariadb-examples.md) |
| 138 | +shows to use MariaDB via the MyTile integration of TileDB as a direct |
| 139 | +backend. |
| 140 | + |
| 141 | +The [TileDB R Tutorial at useR! |
| 142 | +2021](https://dirk.eddelbuettel.com/papers/useR2021_tiledb_tutorial.pdf) |
| 143 | +contained a worked example of writing *much* larger data set in chunks. |
| 144 | +The process is very similar to the simple example we showed here – and |
| 145 | +in addition requires a suffient domain range for the dimension along |
| 146 | +with a (sequential or parallel) loop of reading chunks and writing them |
| 147 | +to TileDB. |
| 148 | + |
| 149 | +## Summary |
| 150 | + |
| 151 | +This vignette provides a commented walk-through of a worked example of a |
| 152 | +SQL-to-TileDB data ingestion. |
0 commit comments