有:采购记录。使用sparklyr::sdf_sql将spark表读取到tbl_spark中。
太大,无法使用DBI::dbGetQuery输出到驱动程序节点上的本地R数据帧(10^8行和30+列(
Person_id | Trip_date | 产品 | 价格
---|---|---|
1 | 2021年1月30日 | 苹果<1.50美元>|
1 | 2021年1月30日 | 香蕉0.89美元 |
1 | 2021年1月27日 | 橙子<1.00美元>|
1 | 2021年1月27日 | 苹果<1.20美元>|
2 | 2021年1月29日 | 葡萄<2.00美元>|
2 | 2021年1月29日 | 苹果<1.20美元>|
2 | 2021年1月28日 | 葡萄<2.50美元>
我尝试了以下代码,结果如下:
library(sparklyr)
sc <- spark_connect(method = "databricks")
Price <- c("$1.50", "$0.89", "$1.00", "$1.20", "$2.00", "$1.20", "$2.50")
Product <- c("Apples", "Bananas", "Oranges", "Apples", "Grapes", "Apples", "Grapes")
Trip_data <- c("30-01-2021", "30-01-2021", "27-01-2021", "27-01-2021", "29-01-2021", "29-01-202", "28-01-2021")
Person_id <- c(1, 1, 1, 1, 2, 2, 2)
df <- data.frame(Price, Product, Trip_data, Person_id)
Fruit_purchases <- sdf_copy_to(sc, df, name = "Fruit_purchases", overwrite = TRUE)
Fruit_buyers <- Fruit_purchases %>% distinct(Person_id, .keep_all = TRUE)
print(sessionInfo())
Fruit_buyers
R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] sparklyr_1.7.7 SparkR_3.3.0
loaded via a namespace (and not attached):
[1] rstudioapi_0.13 magrittr_2.0.3 uuid_1.1-0 tidyselect_1.1.2
[5] R6_2.5.1 rlang_1.0.2 fastmap_1.1.0 fansi_1.0.3
[9] blob_1.2.3 httr_1.4.3 dplyr_1.0.9 tools_4.1.3
[13] parallel_4.1.3 config_0.3.1 utf8_1.2.2 cli_3.3.0
[17] DBI_1.1.3 dbplyr_2.2.0 htmltools_0.5.2 ellipsis_0.3.2
[21] yaml_2.3.5 rprojroot_2.0.3 assertthat_0.2.1 digest_0.6.29
[25] tibble_3.1.7 Rserve_1.8-11 lifecycle_1.0.1 forge_0.2.0
[29] crayon_1.5.1 tidyr_1.2.0 purrr_0.3.4 base64enc_0.1-3
[33] htmlwidgets_1.5.4 vctrs_0.4.1 glue_1.6.2 compiler_4.1.3
[37] pillar_1.7.0 r2d3_0.2.6 generics_0.1.2 jsonlite_1.8.0
[41] pkgconfig_2.0.3
# Source: spark<?> [?? x 4]
# Ordered by: `__row_num_95d8a414_5373_41a6_b78c_af4235f365b6`
Price Product Trip_data Person_id
<chr> <chr> <chr> <dbl>
1 $1.50 Apples 30-01-2021 1
2 $2.00 Grapes 29-01-2021 2