r中hadoop reducer函数的键值对过滤

dz6r00yl  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(362)

我想知道如何在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)
ecbunoof

ecbunoof1#

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

相关问题