wah*_*ulu 5 simulation hadoop r amazon-emr
我正在尝试使用Amazon Elastic Map Reduce来运行数百万个案例的一系列模拟.这是一个没有reducer的Rscript流媒体作业.我在EMR调用中使用Identity Reducer --reducer org.apache.hadoop.mapred.lib.IdentityReducer.
当手动传递一行字符串时,脚本文件在测试并在Linux机器上的命令行本地运行时工作正常echo "1,2443,2442,1,5" | ./mapper.R,我得到了我期望的一行结果.然而,当我使用EMR上的输入文件中的大约10,000个案例(行)测试我的模拟时,我只得到10个输入行中的十几行输出.我已经尝试了几次,但我无法弄清楚原因.Hadoop作业运行正常,没有任何错误.看起来输入行似乎被跳过,或者可能是Identity Reducer发生了什么.对于有输出的情况,结果是正确的.
我的输入文件是一个具有以下数据格式的csv,一系列由逗号分隔的五个整数:
1,2443,2442,1,5
2,2743,4712,99,8
3,2443,861,282,3177
etc...
Run Code Online (Sandbox Code Playgroud)
这是mapper.R的 R脚本
#! /usr/bin/env Rscript
# Define Functions
trimWhiteSpace <- function(line) gsub("(^ +)|( +$)", "", line)
splitIntoWords <- function(line) unlist(strsplit(line, "[[:space:]]+"))
# function to read in the relevant data from needed data files
get.data <- function(casename) {
list <- lapply(casename, function(x) {
read.csv(file = paste("./inputdata/",x, ".csv", sep = ""),
header = TRUE,
stringsAsFactors = FALSE)})
return(data.frame(list))
}
con <- file("stdin")
line <- readLines(con, n = 1, warn = FALSE)
line <- trimWhiteSpace(line)
values <- unlist(strsplit(line, ","))
lv <- length(values)
cases <- as.numeric(values[2:lv])
simid <- paste("sim", values[1], ":", sep = "")
l <- length(cases) # for indexing
## create a vector for the case names
names.vector <- paste("case", cases, sep = ".")
## read in metadata and necessary data columns using get.data function
metadata <- read.csv(file = "./inputdata/metadata.csv", header = TRUE,
stringsAsFactors = FALSE)
d <- cbind(metadata[,1:3], get.data(names.vector))
## Calculations that use df d and produce a string called 'output'
## in the form of "id: value1 value2 value3 ..." to be used at a
## later time for agregation.
cat(output, "\n")
close(con)
Run Code Online (Sandbox Code Playgroud)
此模拟的(通用)EMR调用是:
ruby elastic-mapreduce --create --stream --input s3n://bucket/project/input.txt --output s3n://bucket/project/output --mapper s3n://bucket/project/mapper.R --reducer org.apache.hadoop.mapred.lib.IdentityReducer --cache-archive s3n://bucket/project/inputdata.tar.gz#inputdata --name Simulation --num-instances 2
Run Code Online (Sandbox Code Playgroud)
如果有人对我可能遇到这些问题的原因有任何见解,我愿意接受建议,以及对R脚本的任何更改/优化.
我的另一个选择是将脚本转换为函数并使用R多核软件包运行并行应用程序,但我还没有尝试过.我想让这个工作在EMR上.我使用JD Long和Pete Skomoroch的 R/EMR示例作为创建脚本的基础.
没有任何明显的东西跳出来。但是,您可以使用只有 10 行的简单输入文件来运行该作业吗?确保这 10 行是没有在您的大测试用例中运行的场景。尝试此操作以消除您的输入导致 R 脚本无法生成答案的可能性。
调试 EMR 作业本身就是一项技能。
编辑:
这是一次彻底的钓鱼探险,但使用 AWS GUI 启动 EMR 交互式猪会话。“交互式猪”会话保持正常运行,以便您可以通过 ssh 访问它们。您也可以从命令行工具执行此操作,但从 GUI 中执行此操作会更容易一些,因为您只需执行一次即可。然后通过 ssh 进入集群,将测试用例传输到缓存文件和映射器文件中,然后运行以下命令:
cat infile.txt | yourMapper.R > outfile.txt
这只是为了测试您的映射器是否可以在没有 Hadoop 位妨碍的情况下解析 EMR 环境中的 infile。
编辑2:
我将上面的文本留在那里供后代使用,但真正的问题是您的脚本永远不会返回到标准输入来获取更多数据。因此,每个映射器都会运行一次,然后结束。如果运行上述一行,您将只能得到一个结果,而不是 infile.txt 中每一行的结果。如果您甚至在本地计算机上运行了cat测试,则应该会弹出错误!
让我们看看 Pete在 R 示例中的字数统计:
#! /usr/bin/env Rscript
trimWhiteSpace <- function(line) gsub("(^ +)|( +$)", "", line)
splitIntoWords <- function(line) unlist(strsplit(line, "[[:space:]]+"))
## **** could wo with a single readLines or in blocks
con <- file("stdin", open = "r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
line <- trimWhiteSpace(line)
words <- splitIntoWords(line)
## **** can be done as cat(paste(words, "\t1\n", sep=""), sep="")
for (w in words)
cat(w, "\t1\n", sep="")
}
close(con)
Run Code Online (Sandbox Code Playgroud)
您的脚本缺少的部分是:
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
#do your dance
#do your dance quick
#come on everybody tell me what's the word
#word up
}
Run Code Online (Sandbox Code Playgroud)
当然,你应该替换 Cameo 的 Word Up! 的歌词。与你的实际逻辑。
请记住,正确的调试音乐可以使过程不那么痛苦:
http://www.youtube.com/watch?v=MZjAantupsA