我之前问过一个类似的问题,但是在做了一些探索之后,我对正在发生的事情有了更好的了解,但我想看看其他人是否有替代我的方法的解决方案。
问题
假设您正在尝试编写一个 Hadoop 流作业,该作业在 hdfs 上压缩一堆非常大的文件。 Hadoop Streaming 指南建议您编写一个映射器将文件从 hdfs 复制到本地节点,完成您的工作,然后将文件复制回 hdfs。这是一个小脚本,内联解释了一些额外的代码,用于执行稍微更基本的任务:只需重命名某个文件
剧本
#!/bin/bash
# Remove "s from the environment variable to work around a stupid bug in hadoop.
export HADOOP_CLIENT_OPTS=`echo $HADOOP_CLIENT_OPTS | tr -d '"'`
# Get just the size of the file on the local disk.
function localSize() {
ls -l $1 | awk '{ print $5 }'
}
# Get just the size of the file on HDFS. Oddly, the first command includes a
# new line at the start of the size, so we remove it by using a substring.
function hdfsSize() {
s=`hadoop dfs -ls /some/other/path/$1 | awk '{ print $5 }'`
echo ${s:1}
}
while read line
do
ds=ourFile.dat
# Copy the file from HDFS to local disk.
hadoop dfs -copyToLocal /path/to/some/large/file/$ds $ds
# Spin until the file is fully copied.
while [ ! -f $ds ]
do
echo "spin"
sleep 1
done
# Delete the renamed version of the file and copy it.
hadoop dfs -rm /some/other/path/blah
hadoop dfs -copyFromLocal $ds /some/other/path/blah
# Print out the sizes of the file on local disk and hdfs, they *should* be equal
localSize $ds
hdfsSize blah
# If they aren't equal, spin until they are.
while [ "`localSize $ds`" != "`hdfsSize blah`" ]
do
echo "copy spin"
sleep 1
done
# Print out the file size at the end, just for fun.
hadoop dfs -ls /some/other/path/blah
done
输出
运行脚本后,我们得到这个输出
spin
spin
spin
Deleted hdfs://kracken:54310/some/other/path/blah
200890778
67108864
copy spin
Found 1 items
-rw-r--r-- 3 hadoop supergroup 200890778 2011-10-06 16:00 /home/stevens35/blah
问题
似乎很清楚,hadoop dfs -copyToLocal
并且hadoop dfs -copyFromLocal
似乎在相关文件完成传输之前返回,如spin
和copy spin
输出所示。我的猜测是 Hadoop 流 jvm 正在采用hadoop dfs
命令创建的线程,因此即使hadoop dfs
退出文件传输线程也会继续运行,但这只是一个猜测。当文件很大时,这变得特别烦人,并且 Hadoop 流在最后一个文件完成复制之前退出;似乎文件传输在中途终止,而您在 HDFS 上留下了一个部分文件。我的这个 hack 似乎至少可以确保文件完成复制。
我应该注意我使用的是 Cloudera 的 hadoop 版本 0.20.2+737。
有没有人遇到过这个问题?您找到了哪些替代解决方法?该问题是否已在 Hadoop 的任何新版本中得到解决?