【发布时间】:2014-06-26 03:26:42
【问题描述】:
我在 R 中编写了一个 map reduce 代码,以便在 Amazon EMR 中运行。
我的输入文件格式:
URL1 word1 word2 word3
URL2 word4 word2 word3
URL3 word1 word7 word2
我期望输出为:URLs are concat with spaces
word1 URL1 URL3
word2 URL1 URL2 URL3
word3 URL1 URL2
.. ... ..
但 EMR 使用 3 个 reducer 并创建 3 个输出文件。文件明智的输出是正确的,它正在组合值,没有重复的键。但是如果我们一起查看这 3 个文件,就会发现有重复的键。
输出文件 1:
word1 URL1 URL3
word2 URL1
.. ..
输出文件 2:
word2 URL2 URL3
word3 URL1
.. ..
看,word2 被分发到 2 个文件中。我需要一把钥匙在一个文件里。
我正在使用 EMR 中的 Hadoop Streaming。请建议我正确设置以删除不同文件中的重复键。
我认为我的映射器工作正常。这是我的减速机:
process <- function(mat){
rows = nrow(mat)
cols = ncol(mat)
for(i in 1:rows)
{
for(j in i+1:rows)
{
if(j<=rows)
{
if(toString(mat[i,1])==toString(mat[j,1]))
{
x<-paste(mat[i,2],mat[j,2],sep=" ")
mat[i,2]=x
mat<-mat[-j,]
rows<-rows-1
}
}
}
}
write.table(mat, file=stdout(), quote=FALSE, row.names=FALSE, col.names=FALSE)
}
reduce <- function(input){
#create column names to make is easier to work with the data set
names <- c("word", "value")
cols = as.list(vector(length=2, mode="character"))
names(cols) <- names
#read from the input
hsTableReader(file=input, cols, ignoreKey=TRUE, chunkSize=100000, FUN=process, sep=" ")
}
【问题讨论】:
-
你能分享你正在运行的代码吗?
标签: r hadoop mapreduce elastic-map-reduce emr