使用apache arrow读取一个R数据帧中的分区拼花地板目录(所有文件)

使用apache arrow读取一个R数据帧中的分区拼花地板目录(所有文件),r,rstudio,parquet,apache-arrow,R,Rstudio,Parquet,Apache Arrow,如何在没有火花的情况下用箭头将分区拼花地板文件读入R 形势 使用火花管创建拼花文件并保存在S3上 使用RStudio/RShiny读取,其中一列作为索引,以进行进一步分析 拼花地板文件结构 从我的Spark创建的拼花地板文件由几个部分组成 tree component_mapping.parquet/ component_mapping.parquet/ ├── _SUCCESS ├── part-00000-e30f9734-71b8-4367-99c4-65096143cc17-c000.s

如何在没有火花的情况下用箭头将分区拼花地板文件读入R

形势

使用火花管创建拼花文件并保存在S3上 使用RStudio/RShiny读取,其中一列作为索引,以进行进一步分析 拼花地板文件结构

从我的Spark创建的拼花地板文件由几个部分组成

tree component_mapping.parquet/
component_mapping.parquet/
├── _SUCCESS
├── part-00000-e30f9734-71b8-4367-99c4-65096143cc17-c000.snappy.parquet
├── part-00001-e30f9734-71b8-4367-99c4-65096143cc17-c000.snappy.parquet
├── part-00002-e30f9734-71b8-4367-99c4-65096143cc17-c000.snappy.parquet
├── part-00003-e30f9734-71b8-4367-99c4-65096143cc17-c000.snappy.parquet
├── part-00004-e30f9734-71b8-4367-99c4-65096143cc17-c000.snappy.parquet
├── etc
如何将此组件_mapping.parquet读入R

我试过的

如果我只读取目录中的一个文件,它就会工作

install.packages("arrow")
library(arrow)
my_df<-read_parquet("component_mapping.parquet/part-00000-e30f9734-71b8-4367-99c4-65096143cc17-c000.snappy.parquet")
如何正确设置属性以读取完整目录

# should be this methods
$read_dictionary(column_index)
or
$set_read_dictionary(column_index, read_dict)

非常感谢您的帮助

通过设置单个文件读取器的选项,您无法读取文件目录。如果内存不是问题,那么现在您可以在目录列表上lappy/map,并在单个data.frame中rbind/bind_行。可能有一个purrr函数可以干净地执行此操作。在对文件的迭代中,如果只需要已知的数据子集,还可以对每个文件进行选择/筛选

在Arrow项目中,我们正在积极开发一个多文件数据集API,该API将允许您执行您尝试执行的操作,以及将行和列选择下推到单个文件以及更多。请继续关注。

解决方案:使用箭头将分区拼花地板文件从本地文件系统读取到R数据框中

由于我希望避免在RShiny服务器上使用任何Spark或Python,因此无法使用sparkyr、SparkR或networkite和dplyr等其他库,如中所述

我现在用arrow、Lappy和RbinList解决了我的任务

期待apache arrow功能可用 感谢解决方案:使用箭头将分区拼花地板文件从S3读入R数据框

由于我现在花了很长时间才找到解决方案,而我在web上找不到任何东西,所以我想分享这个解决方案,了解如何从S3读取分区拼花文件

library(arrow)
library(aws.s3)

bucket="mybucket"
prefix="my_prefix"

# using aws.s3 library to get all "part-" files (Key) for one parquet folder from a bucket for a given prefix pattern for a given component
files<-rbindlist(get_bucket(bucket = bucket,prefix=prefix))$Key

# apply the aws.s3::s3read_using function to each file using the arrow::read_parquet function to decode the parquet format
data <- lapply(files, function(x) {s3read_using(FUN = arrow::read_parquet, object = x, bucket = bucket)})

# concatenate all data together into one data.frame
data <- do.call(rbind, data)

What a mess but it works.
@neal-richardson is there a using arrow directly to read from S3? I couldn't find something in the documentation for R

正如@neal richardson在他的回答中所暗示的,在这方面已经做了更多的工作,并且使用我目前运行的4.0.0版本的arrow软件包,这是可能的

我注意到您的文件使用了snappy压缩,这需要在安装之前有一个特殊的构建标志。此处有安装文档:

Dataset API使用多文件数据集实现您正在寻找的功能。虽然文档中还没有包含各种各样的示例,但它确实提供了一个明确的起点

下面的示例显示了从给定目录中读取多文件数据集并将其转换为内存中的R数据帧的最小示例。该API还支持筛选条件和选择列的子集,尽管我自己仍在尝试弄清楚语法

library(arrow)

## Define the dataset
DS <- arrow::open_dataset(sources = "/path/to/directory")
## Create a scanner
SO <- Scanner$create(DS)
## Load it as n Arrow Table in memory
AT <- SO$ToTable()
## Convert it to an R data frame
DF <- as.data.frame(AT)

但这是由spark生成的拼花锉刀的子结构。如果我通过拼花工具阅读它,我也只输入主名称,它会在一个列表中列出所有内容。因此,唯一的解决方案是预先连接所有文件或手动迭代每个文件。但是我在读取操作中失去了任何并行性有多种方法可以并行化R中的文件读取,只是目前没有烘焙到arrow包中。如果你想把它内置在arrow中,请尽快回来查看,它马上就来了。哦,这是个好消息,我这几天会试试
# should be this methods
$read_dictionary(column_index)
or
$set_read_dictionary(column_index, read_dict)
my_df <-data.table::rbindlist(lapply(Sys.glob("component_mapping.parquet/part-*.parquet"), arrow::read_parquet))
library(arrow)
library(aws.s3)

bucket="mybucket"
prefix="my_prefix"

# using aws.s3 library to get all "part-" files (Key) for one parquet folder from a bucket for a given prefix pattern for a given component
files<-rbindlist(get_bucket(bucket = bucket,prefix=prefix))$Key

# apply the aws.s3::s3read_using function to each file using the arrow::read_parquet function to decode the parquet format
data <- lapply(files, function(x) {s3read_using(FUN = arrow::read_parquet, object = x, bucket = bucket)})

# concatenate all data together into one data.frame
data <- do.call(rbind, data)

What a mess but it works.
@neal-richardson is there a using arrow directly to read from S3? I couldn't find something in the documentation for R
Sys.setenv("ARROW_WITH_SNAPPY" = "ON")
install.packages("arrow",force = TRUE)
library(arrow)

## Define the dataset
DS <- arrow::open_dataset(sources = "/path/to/directory")
## Create a scanner
SO <- Scanner$create(DS)
## Load it as n Arrow Table in memory
AT <- SO$ToTable()
## Convert it to an R data frame
DF <- as.data.frame(AT)