有人知道如何在SparkR版本1.4.0中读取文本文件吗?有火种包吗?
发布于 2015-07-01 18:23:50
星火1.6+
可以使用text输入格式将文本文件读入为DataFrame。
read.df(sqlContext=sqlContext, source="text", path="README.md")火花<= 1.5
简单地说,您没有。SparkR 1.4几乎完全脱离了低级别的API,只剩下有限的数据帧操作。你可以在旧SparkR网页上读到
截至2015年4月,SparkR已正式并入Apache,并将在即将发布的版本(1.4)中发布。(...)最初对R中星火的支持应该集中在高水平的操作上,而不是低水平的ETL上。
最接近的事情可能是使用spark-csv加载文本文件
> df <- read.df(sqlContext, "README.md", source = "com.databricks.spark.csv")
> showDF(limit(df, 5))
+--------------------+
| C0|
+--------------------+
| # Apache Spark|
|Spark is a fast a...|
|high-level APIs i...|
|supports general ...|
|rich set of highe...|
+--------------------+由于典型的RDD操作(如map、flatMap、reduce或filter )也消失了,这可能是您想要的结果。
现在,底层的API还在下面,所以您可以做下面这样的事情,但是--我怀疑这是一个好主意--。SparkR开发人员很可能有一个很好的理由让它成为私有的。引用:::手册页:
在代码中使用“:”通常是一个设计错误,因为相应的对象可能是在内部保存的,这是有充分理由的。如果您觉得需要访问对象,而不仅仅是检查,请考虑与包维护人员联系。
即使你愿意忽视好的编码实践,我也很可能不值得花时间。Pre1.4低级别的API速度慢得令人尴尬,而且笨拙,如果没有催化剂优化器的所有优点,那么对于内部1.4API来说,它很可能也是一样的。
> rdd <- SparkR:::textFile(sc, 'README.md')
> counts <- SparkR:::map(rdd, nchar)
> SparkR:::take(counts, 3)
[[1]]
[1] 14
[[2]]
[1] 0
[[3]]
[1] 78与textFile不同的是,并不是textFile忽略空行。
发布于 2015-07-01 09:40:48
请按http://ampcamp.berkeley.edu/5/exercises/sparkr.html链接
我们可以简单地使用-
textFile <- textFile(sc, "/home/cloudera/SparkR-pkg/README.md")在检查SparkR代码时,Context.R有textFile方法,因此理想情况下,SparkContext必须有textFile API才能创建RDD,但这在文档中是缺失的。
# Create an RDD from a text file.
#
# This function reads a text file from HDFS, a local file system (available on all
# nodes), or any Hadoop-supported file system URI, and creates an
# RDD of strings from it.
#
# @param sc SparkContext to use
# @param path Path of file to read. A vector of multiple paths is allowed.
# @param minPartitions Minimum number of partitions to be created. If NULL, the default
# value is chosen based on available parallelism.
# @return RDD where each item is of type \code{character}
# @export
# @examples
#\dontrun{
# sc <- sparkR.init()
# lines <- textFile(sc, "myfile.txt")
#}
textFile <- function(sc, path, minPartitions = NULL) {
# Allow the user to have a more flexible definiton of the text file path
path <- suppressWarnings(normalizePath(path))
# Convert a string vector of paths to a string containing comma separated paths
path <- paste(path, collapse = ",")
jrdd <- callJMethod(sc, "textFile", path, getMinPartitions(sc, minPartitions))
# jrdd is of type JavaRDD[String]
RDD(jrdd, "string")
}遵循链接https://github.com/apache/spark/blob/master/R/pkg/R/context.R
用于测试用例rdd.R
发布于 2015-12-03 13:52:13
事实上,您也可以使用databricks/spark包来处理tsv文件。
例如,
data <- read.df(sqlContext, "<path_to_tsv_file>", source = "com.databricks.spark.csv", delimiter = "\t")这里提供了许多选项- 数据库.火花.csv#特性
https://stackoverflow.com/questions/31157649
复制相似问题