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

met*_*sim 31 r apache-spark parquet sparkr

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

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

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

And*_*son 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()
Run Code Online (Sandbox Code Playgroud)

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

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


Aur*_*èle 12

或者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)
Run Code Online (Sandbox Code Playgroud)


Uwe*_*orn 11

您可以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
Run Code Online (Sandbox Code Playgroud)

然后,您可以安装R arrow软件包:

devtools::install_github("apache/arrow/r")
Run Code Online (Sandbox Code Playgroud)

并使用它加载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>
#> …
Run Code Online (Sandbox Code Playgroud)

  • {arrow}最近进入了CRAN:https://cran.r-project.org/web/packages/arrow/index.html (4认同)

Jon*_*han 5

使用网状结构,您可以将python中的熊猫用于镶木地板文件。这样可以避免您运行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)
Run Code Online (Sandbox Code Playgroud)


fc9*_*.30 5

您可以简单地使用arrow软件包

install.packages("arrow")
library(arrow)
read_parquet("myfile.parquet")
Run Code Online (Sandbox Code Playgroud)