【问题标题】:how to save the output to the particular path in sparkR如何将输出保存到 sparkR 中的特定路径
【发布时间】:2015-08-26 06:32:43
【问题描述】:

我正在使用 spark-submit 运行 pi.R 示例。输出显示在终端上,但我想将输出保存到 hdfs 路径。

/SparkR-pkg/lib/SparkR/sparkR-submit --master yarn-client examples/pi.R yarn-client 4

以上命令用于运行 pi.r 示例

这里是 pi.R 代码

library(SparkR)
args <- commandArgs(trailing = TRUE)
if (length(args) < 1) {
print("Usage: pi <master> [<slices>]")
q("no")
}
sc <- sparkR.init(args[[1]], "PiR")
slices <- ifelse(length(args) > 1, as.integer(args[[2]]), 2)
n <- 100000 * slices
piFunc <- function(elem) {
rands <- runif(n = 2, min = -1, max = 1)
val <- ifelse((rands[1]^2 + rands[2]^2) < 1, 1.0, 0.0)
val
}
piFuncVec <- function(elems) {
message(length(elems))
rands1 <- runif(n = length(elems), min = -1, max = 1)
rands2 <- runif(n = length(elems), min = -1, max = 1)
val <- ifelse((rands1^2 + rands2^2) < 1, 1.0, 0.0)
sum(val)
}
rdd <- parallelize(sc, 1:n, slices)
count <- reduce(lapplyPartition(rdd, piFuncVec), sum)
cat("Pi is roughly", 4.0 * count / n, "\n")
cat("Num elements in RDD ", count(rdd), "\n")

我想将上述输出保存在 hdfs 位置。任何帮助将不胜感激

【问题讨论】:

    标签: r hadoop sparkr


    【解决方案1】:

    我没有在集群上测试这个,但我猜常规的 R 代码在这里可以工作:

    output <- paste("Pi is roughly", 4.0 * count / n, "\n")
    output <- paste(output, "Num elements in RDD ", count(rdd), "\n")
    write(output,file="pathToFile") #can be location.any
    

    【讨论】:

    • 当我运行上面的代码时,我得到了这个错误。 ifelse(append, "a", "w") 中的错误:找不到对象'w'调用:写->猫->文件- > ifelse
    • 使用 write(output,file="pathToFile", append=TRUE) 有帮助吗?
    • 即使我将附加设置为 true,我也会遇到同样的错误,在文件(文件,ifelse(附加,“a”,“w”))中:无法打开文件 'hdfs://ip -172-31-41-199.us-west-2.compute.internal:8020/user/karun/output/test1.txt':没有这样的文件或目录
    • 是否 file.exists('hdfs://ip-172-31-41-199.us-west-2.compute.internal:8020/user/karun/output') 返回 TRUE 或假?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2021-04-24
    • 2017-10-29
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多