Rhadoop — подсчет слов с использованием rmr

Я пытаюсь запустить простое задание rmr с использованием пакета Rhadoop, но оно не работает. Вот мой 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

Пожалуйста, дайте мне знать, в чем проблема.


person Shashi    schedule 15.05.2015    source источник


Ответы (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

Для справки: здесь приведен еще один пример запуск программы сокращения количества слов R.

Надеюсь это поможет.

person Manohar Swamynathan    schedule 22.05.2015
comment
Эта программа дает проблемы с потоковой передачей. Как их решить? Проверьте это stackoverflow.com/questions/42572023/having-issues-with- радуп - person Jaffer Wilson; 03.03.2017
comment
На ваш вопрос был дан ответ stackoverflow.com/questions/ 29419318/, посмотрите, поможет ли это. - person Manohar Swamynathan; 05.03.2017