|
| 1 | +library(tidyverse) |
| 2 | +library(arrow) |
| 3 | + |
| 4 | +# meta data |
| 5 | +(t1 <- int32()) |
| 6 | +(t2 <- utf8()) |
| 7 | +(t5 <- timestamp(unit = TimeUnit$MILLI)) |
| 8 | + |
| 9 | +# lists |
| 10 | +list_of(t1) |
| 11 | + |
| 12 | +# shema |
| 13 | +schema(x = int32(), y = float64()) |
| 14 | + |
| 15 | +# :scream_cat: |
| 16 | +# |
| 17 | +# pa.schema( |
| 18 | +# [ |
| 19 | +# pa.field('x', pa.int32()), |
| 20 | +# pa.field('y', pa.float64()) |
| 21 | +# ] |
| 22 | +# ) |
| 23 | +# |
| 24 | + |
| 25 | +schema(x = int32(), y = list_of(float64())) |
| 26 | + |
| 27 | +#------- arrays |
| 28 | + |
| 29 | +# arr = pa.array([1, 2, 3]) |
| 30 | +arr <- array(1:3, 5:80) |
| 31 | +arr |
| 32 | +arr$as_vector() |
| 33 | + |
| 34 | +#------- read_arrow / write_arrow |
| 35 | +tbl <- tibble(x=1:10, y=rnorm(10)) |
| 36 | +write_arrow(tbl, "/tmp/test.arrow") |
| 37 | +readr::write_rds(tbl, "/tmp/test.rds") |
| 38 | +fs::file_info(c("/tmp/test.arrow", "/tmp/test.rds")) |
| 39 | + |
| 40 | +(data <- read_arrow("/tmp/test.arrow")) |
| 41 | + |
| 42 | +# tibble <-> arrow::RecordBatch |
| 43 | +(batch <- record_batch(tbl)) |
| 44 | +batch$num_columns() |
| 45 | +batch$num_rows() |
| 46 | +batch$to_file("/tmp/test") |
| 47 | +readBin("/tmp/test", what = raw(), n = 1000) |
| 48 | +batch$schema() |
| 49 | +all.equal(tbl, data) |
| 50 | + |
| 51 | +batch <- read_record_batch("/tmp/test") |
| 52 | +batch$schema() |
| 53 | +batch$column(0) |
| 54 | +batch$column(0)$as_vector() |
| 55 | + |
| 56 | +as_tibble(batch) |
| 57 | + |
| 58 | +# tibble <-> arrow::Table |
| 59 | +tab <- arrow::table(tbl) |
| 60 | +tab |
| 61 | +tab$schema() |
| 62 | +tab$num_columns() |
| 63 | +tab$num_rows() |
| 64 | + |
| 65 | +# read_arrow, write_arrow |
| 66 | +tbl <- tibble(x = rnorm(20), y = seq_len(20)) |
| 67 | +write_arrow(tbl, tf) |
| 68 | + |
0 commit comments