ga_reducer.R 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # To identify the type of the script, here it is RScript
  2. #! /usr/bin/env Rscript
  3. # Defining the variables with their initial values
  4. city.key = NA
  5. page.value = 0.0
  6. # To initiating the connection to standard input
  7. input = file("stdin", open="r")
  8. # 循环遍历所有行,mapper中提取的数据进行合并计算
  9. while (length(currentLine = readLines(input, n=1)) > 0) {
  10. # Splitting the line into vectors by tab(�\t�) separator
  11. fields = strsplit(currentLine, "\t")
  12. # Capturing key and value form the fields
  13. key = fields[[1]][1]
  14. value = as.character(fields[[1]][2])
  15. # Setting up key and values
  16. if (is.na(city.key)) {
  17. city.key = key
  18. page.value = value
  19. }
  20. else {
  21. if (city.key == key) {
  22. page.value = c(page.value, value)
  23. } else {
  24. page.value = unique(page.value)
  25. #Printing key and value to standard output
  26. print(list(city.key, page.value),stdout())
  27. city.key = key
  28. page.value = value
  29. }
  30. }
  31. }
  32. print(list(city.key, page.value), stdout())
  33. # Closing the connection
  34. close(input)