在R中的hadoop reducer函数中过滤键值对

时间:2015-10-12 09:30:50

标签: r hadoop mapreduce

我想知道如何在hadoop reducer函数中设置过滤掉键值对的条件。例如,在下面给出的单词计数示例中,如何获得计数大于某个阈值的单词,例如3。

library(rmr2)
library(rhdfs)

# initiate rhdfs package
hdfs.init()

map <- function(k,lines) {
  words.list <- strsplit(lines, '\\s')
  words <- unlist(words.list)
  return( keyval(words, 1) )
}

reduce <- function(word, counts) {
  keyval(word, sum(counts))
}

wordcount <- function (input, output=NULL) {
  mapreduce(input=input, output=output, input.format="text", map=map, reduce=reduce)
}

## read text files from folder example/wordcount/data
hdfs.root <- 'example/wordcount'
hdfs.data <- file.path(hdfs.root, 'data')

## save result in folder example/wordcount/out
hdfs.out <- file.path(hdfs.root, 'out')

## Submit job
out <- wordcount(hdfs.data, hdfs.out) 

## Fetch results from HDFS
results <- from.dfs(out)
results.df <- as.data.frame(results, stringsAsFactors=F)
colnames(results.df) <- c('word', 'count')

head(results.df)

1 个答案:

答案 0 :(得分:1)

<figure>
  <img src="http://lorempixel.com/200/150" />
  <figcaption>some text</figcaption>
</figure>