我希望能够用R编程语言处理Apache Parquet文件(在我的情况下,这些文件是由Spark生成的)。
是否有可用的R读取器?或者正在开发中吗?
如果没有,最快的方法是什么?注意:有Java和C++绑定:https://github.com/apache/parquet-mr
我希望能够用R编程语言处理Apache Parquet文件(在我的情况下,这些文件是由Spark生成的)。
是否有可用的R读取器?或者正在开发中吗?
如果没有,最快的方法是什么?注意:有Java和C++绑定:https://github.com/apache/parquet-mr
最简单的方法是使用这个 arrow
软件包,它可以在 CRAN上找到。
install.packages("arrow")
library(arrow)
read_parquet("somefile.parquet")
以前可以通过Python使用pyarrow
来完成这个任务,但现在R也打包了这个功能,无需使用Python。
如果您不想从CRAN安装,可以直接构建或从GitHub安装:
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>
#> …
install.packages("arrow")
library(arrow)
read_parquet("myfile.parquet")
arrow
现在已经可以在 CRAN 上直接安装的变化。 - B.Liu如果你正在使用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,我不知道还有哪个软件包可供使用。
parquetFile(sqlContext...)
已经被弃用。请使用parquetFile(...)
代替。" --和-- "f'已经被弃用。请使用
read.parquet代替。" 不幸的是,
parquetFile和
read.parquet`都没有文档记录,因此现在实现的正确语法不清楚。 - MichaelChirico如果您不想使用 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)
install.packages("dplyr")
- nessa.gp使用reticulate,您可以从Python中使用pandas访问parquet文件。这样可以避免运行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)
Spark已经更新,有许多新的东西和功能被弃用或重命名。
Andy在上面的答案适用于Spark v.1.4,但在Spark v.2.3上,这是适用于我的更新。
Download latest version of apache spark https://spark.apache.org/downloads.html (point 3 in the link)
extract the .tgz
file.
install devtool
package in rstudio
install.packages('devtools')
Open terminal
and follow these steps
# 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('.')"
Go back to 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()
miniparquet
是一个新的专用软件包。使用以下命令进行安装:
devtools::install_github("hannesmuehleisen/miniparquet")
以下是来自文档的示例:
library(miniparquet)
f <- system.file("extdata/userdata1.parquet", package="miniparquet")
df <- parquet_read(f)
str(df)
# 'data.frame': 1000 obs. of 13 variables:
# $ registration_dttm: POSIXct, format: "2016-02-03 07:55:29" "2016-02-03 17:04:03" "2016-02-03 01:09:31" ...
# $ id : int 1 2 3 4 5 6 7 8 9 10 ...
# $ first_name : chr "Amanda" "Albert" "Evelyn" "Denise" ...
# $ last_name : chr "Jordan" "Freeman" "Morgan" "Riley" ...
# $ email : chr "ajordan0@com.com" "afreeman1@is.gd" "emorgan2@altervista.org" "driley3@gmpg.org" ...
# $ gender : chr "Female" "Male" "Female" "Female" ...
# $ ip_address : chr "1.197.201.2" "218.111.175.34" "7.161.136.94" "140.35.109.83" ...
# $ cc : chr "6759521864920116" "" "6767119071901597" "3576031598965625" ...
# $ country : chr "Indonesia" "Canada" "Russia" "China" ...
# $ birthdate : chr "3/8/1971" "1/16/1968" "2/1/1960" "4/8/1997" ...
# $ salary : num 49757 150280 144973 90263 NA ...
# $ title : chr "Internal Auditor" "Accountant IV" "Structural Engineer" "Senior Cost Accountant" ...
# $ comments : chr "1E+02" "" "" "" ...
如果要读取Amazon S3存储桶中的Parquet文件,请尝试使用s3a而不是s3n。当我使用EMR 1.4.0、RStudio和Spark 1.5.0读取Parquet文件时,这对我很有效。
data.table::rbindlist(lapply(Sys.glob("path_to_parquet/part-*.parquet"), arrow::read_parquet))
最近我发布了一个R包来读取parquet和delta文件。它基本上使用arrow包,但是可以在本地和云中处理delta文件。
你可以像这样使用:
readparquetR(pathtoread="C:/users/...",format="delta")# format can be parquet or delta
readparquetR(pathtoread="blobpath/subdirectory/", filelocation = "azure", format="delta", containerconnection = your_connection)
欢迎自由使用或贡献 https://github.com/mkparkin/Rinvent
conda install r-arrow
安装。 - Keith Hughitt