如何在R中读取Parquet并将其转换为R DataFrame?

问题描述 投票:22回答:6

我想用R编程语言处理Apache Parquet文件(在我的例子中,在Spark中生成)。

是否有R读卡器?或者正在进行一项工作?

如果没有,那么到达那里最方便的方式是什么?注意:有Java和C ++绑定:https://github.com/apache/parquet-mr

r apache-spark parquet sparkr
6个回答
5
投票

通过网纹,您可以使用从python到镶木地板文件的pandas。这可以节省您运行spark实例的麻烦。

library(reticulate)
library(dplyr)
pandas <- import("pandas")
read_parquet <- function(path, columns = NULL) {

  path <- path.expand(path)
  path <- normalizePath(path)

  if (!is.null(columns)) columns = as.list(columns)

  xdf <- pandas$read_parquet(path, columns = columns)

  xdf <- as.data.frame(xdf, stringsAsFactors = FALSE)

  dplyr::tbl_df(xdf)

}

read_parquet(PATH_TO_PARQUET_FILE)

27
投票

如果您正在使用Spark,那么随着Spark 1.4的发布,这现在相对简单,请参阅下面的示例代码,该代码使用现在属于Apache Spark核心框架的SparkR包。

# install the SparkR package
devtools::install_github('apache/spark', ref='master', subdir='R/pkg')

# load the SparkR package
library('SparkR')

# initialize sparkContext which starts a new Spark session
sc <- sparkR.init(master="local")

# initialize sqlContext
sq <- sparkRSQL.init(sc)

# load parquet file into a Spark data frame and coerce into R data frame
df <- collect(parquetFile(sq, "/path/to/filename"))

# terminate Spark session
sparkR.stop()

@ https://gist.github.com/andyjudson/6aeff07bbe7e65edc665展示了一个扩展示例

如果您不使用Spark,我不知道您可以使用的任何其他软件包。


11
投票

或者SparkR,您现在可以使用sparklyr

# install.packages("sparklyr")
library(sparklyr)

sc <- spark_connect(master = "local")

spark_tbl_handle <- spark_read_parquet(sc, "tbl_name_in_spark", "/path/to/parquetdir")

regular_df <- collect(spark_tbl_handle)

spark_disconnect(sc)

8
投票

您可以使用arrow包。它与Python pyarrow中的相同,但现在也为R打包而不需要Python。由于CRAN尚未提供,您必须先手动安装Arrow C ++:

git clone https://github.com/apache/arrow.git
cd arrow/cpp && mkdir release && cd release

# It is important to statically link to boost libraries
cmake .. -DARROW_PARQUET=ON -DCMAKE_BUILD_TYPE=Release -DARROW_BOOST_USE_SHARED:BOOL=Off
make install

然后你可以安装R arrow包:

devtools::install_github("apache/arrow/r")

并使用它来加载Parquet文件

library(arrow)
#> 
#> Attaching package: 'arrow'
#> The following object is masked from 'package:utils':
#> 
#>     timestamp
#> The following objects are masked from 'package:base':
#> 
#>     array, table
read_parquet("somefile.parquet", as_tibble = TRUE)
#> # A tibble: 10 x 2
#>        x       y
#>    <int>   <dbl>
#> …

4
投票

Spark已经更新,并且有许多新的东西和功能被弃用或重命名。

Andy上面的回答是为spark v.1.4工作,但是在spark v.2.3上,这是对我有用的更新。

  1. 下载apache spark https://spark.apache.org/downloads.html的最新版本(链接中的第3点)
  2. 提取.tgz文件。
  3. devtool安装rstudioinstall.packages('devtools')
  4. 打开terminal并按照以下步骤操作 # This is the folder of extracted spark `.tgz` of point 1 above export SPARK_HOME=extracted-spark-folder-path cd $SPARK_HOME/R/lib/SparkR/ R -e "devtools::install('.')"
  5. 回到rstudio # load the SparkR package library(SparkR) # initialize sparkSession which starts a new Spark session sc <- sparkR.session(master="local") # load parquet file into a Spark data frame and coerce into R data frame df <- collect(read.parquet('.parquet-file-path')) # terminate Spark session sparkR.stop()

2
投票

要在Amazon S3存储桶中读取镶木地板文件,请尝试使用s3a而不是s3n。当使用EMR 1.4.0,RStudio和Spark 1.5.0读取镶木地板文件时,这对我有用。


0
投票

你可以简单地使用arrow package

install.packages("arrow")
library(arrow)
read_parquet("myfile.parquet")
© www.soinside.com 2019 - 2024. All rights reserved.