跳到内容

此函数允许您写入数据集。通过写入更高效的二进制存储格式,并通过指定相关的分区,您可以使其读取和查询速度更快。

用法

write_dataset(
  dataset,
  path,
  format = c("parquet", "feather", "arrow", "ipc", "csv", "tsv", "txt", "text"),
  partitioning = dplyr::group_vars(dataset),
  basename_template = paste0("part-{i}.", as.character(format)),
  hive_style = TRUE,
  existing_data_behavior = c("overwrite", "error", "delete_matching"),
  max_partitions = 1024L,
  max_open_files = 900L,
  max_rows_per_file = 0L,
  min_rows_per_group = 0L,
  max_rows_per_group = bitwShiftL(1, 20),
  ...
)

参数

dataset

Dataset, RecordBatch, Table, arrow_dplyr_query, 或 data.frame。如果是一个 arrow_dplyr_query,该查询将被评估,结果将被写入。这意味着您可以 select()filter()mutate() 等来转换数据,如果需要的话,在写入之前。

path

字符串路径、URI 或 SubTreeFileSystem,引用要写入的目录(如果目录不存在,则会创建它)

format

文件格式的字符串标识符。默认为使用 "parquet"(请参见 FileFormat

partitioning

Partitioning 或要用作分区键的列的字符向量(将作为路径段写入)。默认为使用当前的 group_by() 列。

basename_template

要写入的文件的名称的字符串模板。必须包含 "{i}",它将被一个自动递增的整数替换,以生成数据文件的基本名称。例如,"part-{i}.arrow" 将产生 "part-0.arrow", ...。如果未指定,则默认为 "part-{i}.<default extension>"

hive_style

logical: 将分区段写入为 Hive 样式 (key1=value1/key2=value2/file.ext) 或仅写入裸值。默认为 TRUE

existing_data_behavior

当目标目录中已存在数据时要使用的行为。必须是 "overwrite"、"error" 或 "delete_matching" 之一。

  • "overwrite"(默认)则任何新创建的文件将覆盖现有文件

  • "error" 如果目标目录不为空,则操作将失败

  • "delete_matching" 然后,如果数据将要写入这些分区,则写入器将删除任何现有的分区,并让未写入数据的分区保持原样。

max_partitions

任何批处理可以写入的最大分区数。默认为 1024L。

max_open_files

在写入操作期间可以保持打开的最大文件数。如果大于 0,则这将限制可以保持打开的最大文件数。如果尝试打开过多文件,则最近最少使用的文件将被关闭。如果此设置设置得太低,您最终可能会将数据碎片化为许多小文件。默认值为 900,这也允许扫描器在达到 Linux 默认限制 1024 之前打开一些文件。

max_rows_per_file

每个文件的最大行数。如果大于 0,则这将限制任何单个文件中放置的行数。默认为 0L。

min_rows_per_group

当累积到此行数时,将行组写入磁盘。默认为 0L。

max_rows_per_group

单个组中允许的最大行数,当超过此行数时,它将被拆分,下一组行将被写入下一个组。此值必须设置成大于 min_rows_per_group。默认为 1024 * 1024。

...

其他特定于格式的参数。有关可用的 Parquet 选项,请参见 write_parquet()。可用的 Feather 选项是

  • use_legacy_format logical: 写入格式化的数据,以便 Arrow 库版本 0.14 及更低版本可以读取它。默认为 FALSE。您还可以通过设置环境变量 ARROW_PRE_0_15_IPC_FORMAT=1 来启用此功能。

  • metadata_version:一个字符串,例如 "V5" 或等效的整数,指示 Arrow IPC MetadataVersion。默认值 (NULL) 将使用最新版本,除非环境变量 ARROW_PRE_1_0_METADATA_VERSION=1,在这种情况下它将是 V4。

  • codec:一个 Codec,将用于压缩写入文件的正文缓冲区。默认值 (NULL) 不会压缩正文缓冲区。

  • null_fallback:在使用 Hive 样式分区时,用于代替缺失值(NANULL)的字符。请参见 hive_partition()

不可见的输入 dataset

示例

# You can write datasets partitioned by the values in a column (here: "cyl").
# This creates a structure of the form cyl=X/part-Z.parquet.
one_level_tree <- tempfile()
write_dataset(mtcars, one_level_tree, partitioning = "cyl")
list.files(one_level_tree, recursive = TRUE)
#> [1] "cyl=4/part-0.parquet" "cyl=6/part-0.parquet" "cyl=8/part-0.parquet"

# You can also partition by the values in multiple columns
# (here: "cyl" and "gear").
# This creates a structure of the form cyl=X/gear=Y/part-Z.parquet.
two_levels_tree <- tempfile()
write_dataset(mtcars, two_levels_tree, partitioning = c("cyl", "gear"))
list.files(two_levels_tree, recursive = TRUE)
#> [1] "cyl=4/gear=3/part-0.parquet" "cyl=4/gear=4/part-0.parquet"
#> [3] "cyl=4/gear=5/part-0.parquet" "cyl=6/gear=3/part-0.parquet"
#> [5] "cyl=6/gear=4/part-0.parquet" "cyl=6/gear=5/part-0.parquet"
#> [7] "cyl=8/gear=3/part-0.parquet" "cyl=8/gear=5/part-0.parquet"

# In the two previous examples we would have:
# X = {4,6,8}, the number of cylinders.
# Y = {3,4,5}, the number of forward gears.
# Z = {0,1,2}, the number of saved parts, starting from 0.

# You can obtain the same result as as the previous examples using arrow with
# a dplyr pipeline. This will be the same as two_levels_tree above, but the
# output directory will be different.
library(dplyr)
two_levels_tree_2 <- tempfile()
mtcars %>%
  group_by(cyl, gear) %>%
  write_dataset(two_levels_tree_2)
list.files(two_levels_tree_2, recursive = TRUE)
#> [1] "cyl=4/gear=3/part-0.parquet" "cyl=4/gear=4/part-0.parquet"
#> [3] "cyl=4/gear=5/part-0.parquet" "cyl=6/gear=3/part-0.parquet"
#> [5] "cyl=6/gear=4/part-0.parquet" "cyl=6/gear=5/part-0.parquet"
#> [7] "cyl=8/gear=3/part-0.parquet" "cyl=8/gear=5/part-0.parquet"

# And you can also turn off the Hive-style directory naming where the column
# name is included with the values by using `hive_style = FALSE`.

# Write a structure X/Y/part-Z.parquet.
two_levels_tree_no_hive <- tempfile()
mtcars %>%
  group_by(cyl, gear) %>%
  write_dataset(two_levels_tree_no_hive, hive_style = FALSE)
list.files(two_levels_tree_no_hive, recursive = TRUE)
#> [1] "4/3/part-0.parquet" "4/4/part-0.parquet" "4/5/part-0.parquet"
#> [4] "6/3/part-0.parquet" "6/4/part-0.parquet" "6/5/part-0.parquet"
#> [7] "8/3/part-0.parquet" "8/5/part-0.parquet"