【发布时间】:2014-06-16 06:40:07
【问题描述】:
我正在尝试使用 R (Hadoop Streaming) 编写一个基本的 MapReduce。以下是我写的Mapper函数:
#! /usr/bin/env Rscript
con <- file("stdin",open = "r")
while(length(line <- readLines(con = con,n = 1,warn = FALSE)) > 0 )
{
line1 <- gsub("^\\s+|\\s+$", "", line)
if(is.null(strsplit(line1," ")) == FALSE){
x <- as.numeric(unlist(strsplit(line1," "))[[1]])
y <- as.numeric(unlist(strsplit(line1," "))[[2]])
x2 <- x*x
xy <- x*y
cat(x,"\t",y,"\t",xy,"\t",x2,"\n")
}
}
close(con)
此输入文件包含两列,如下所示:
1 15.55511341
2 27.53983952
3 39.7767569
4 47.44065279
5 55.0606804
6 68.57527802
7 77.03639749
8 80.92939421
9 94.4431412
10 106.5353655
我尝试使用以下命令直接在命令提示符下运行此映射器:
cat ../data/Input.txt | ./mapper.R
但是,我收到以下错误消息:
Error in unlist(strsplit(line1, " "))[[2]] : subscript out of bounds
In addition: Warning message:
NAs introduced by coercion
Execution halted
看起来我在代码中犯了一些基本错误。有人可以帮我解决这个问题吗?
【问题讨论】:
-
输入文本的第一行有 2 个空格。第二个,3个等等。在使用
strsplit拆分它们之前,您需要评估每行的空格数。 -
你是怎么找到空格的?在文本文件中,我只看到两个值之间的选项卡。任何我尝试添加以下两行来修剪空格: trimWhiteSpace
-
所以,你的例子是不可重现的。最好使用
'\t'剥离制表。 -
令人惊讶的是,它不适用于 '\t'...它仅适用于 ''...不知道为什么...顺便说一句,我发现了错误...请参阅我的评论在下面的答案中
标签: r hadoop mapreduce hadoop-streaming