【问题标题】:Rhadoop - wordcount using rmrRhadoop - 使用 rmr 的字数统计
【发布时间】:2015-05-15 13:58:34
【问题描述】:

我正在尝试使用 Rhadoop 包运行一个简单的 rmr 作业,但它不起作用。这是我的 R 脚本

print("Initializing variable.....")
Sys.setenv(HADOOP_HOME="/usr/hdp/2.2.4.2-2/hadoop")
Sys.setenv(HADOOP_CMD="/usr/hdp/2.2.4.2-2/hadoop/bin/hadoop")
print("Invoking functions.......")
#Referece taken from Revolution Analytics
wordcount = function(    input,     output = NULL,     pattern = " ")
{
mapreduce(
      input = input ,
      output = output,
      input.format = "text",
      map = wc.map,
      reduce = wc.reduce,
      combine = T)
}

wc.map =
      function(., lines) {
        keyval(
          unlist(
            strsplit(
              x = lines,
              split = pattern)),
          1)}

wc.reduce =
      function(word, counts ) {
        keyval(word, sum(counts))}

#Function Invoke

wordcount('/user/hduser/rmr/wcinput.txt')

我在脚本上面运行

Rscript wordcount.r

我遇到了错误。

[1] "Initializing variable....."
[1] "Invoking functions......."
Error in wordcount("/user/hduser/rmr/wcinput.txt") :
could not find function "mapreduce"
Execution halted

请告诉我是什么问题。

【问题讨论】:

    标签: r hadoop rhadoop


    【解决方案1】:

    首先,您必须在代码中设置HADOOP_STREAMING 环境变量。

    试试下面的代码,注意代码假设你已经把你的文本文件复制到hdfs文件夹examples/wordcount/data

    R 代码:

    Sys.setenv("HADOOP_CMD"="/usr/local/hadoop/bin/hadoop")
    Sys.setenv("HADOOP_STREAMING"="/usr/local/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.4.0.jar")
    
    # load librarys
    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)
    

    输出:

    word count
      AS    16
      As     5
      B.     1
      BE    13
      BY    23
      By     7
    

    供您参考,here 是另一个运行 R word count map reduce 程序的示例。

    希望这会有所帮助。

    【讨论】:

    猜你喜欢
    • 2015-06-21
    • 2014-06-03
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多