将文件放入 hdfs 中不存在的目录需要两个步骤。正如@rt-vybor 所说,使用 mkdir 的“-p”选项来创建多个缺少的路径元素。但是由于OP询问如何将文件放入hdfs,下面也执行hdfs put,注意你也可以(可选)检查put是否成功,并有条件地删除本地副本。
先在hdfs中创建相关目录路径,然后将文件放入hdfs中。您想在放入 hdfs 之前检查文件是否存在。您可能想要记录/显示文件已成功放入 hdfs。以下结合了所有步骤。
fn=myfile.txt
if [ -f $fn ] ; then
bfn=`basename $fn` #trim path from filename
hdfs dfs -mkdir -p /here/is/some/non/existant/path/in/hdfs/
hdfs dfs -put $fn /here/is/some/non/existant/path/in/hdfs/$bfn
hdfs dfs -ls /here/is/some/non/existant/path/in/hdfs/$bfn
success=$? #check whether file landed in hdfs
if [ $success ] ; then
echo "remove local copy of file $fn"
#rm -f $fn #uncomment if you want to remove file
fi
fi
你可以把它变成一个shell脚本,采用hadoop路径和文件列表(也只创建一次路径),
#!/bin/bash
hdfsp=${1}
shift;
hdfs dfs -mkdir -p /here/is/some/non/existant/path/in/hdfs/
for fn in $*; do
if [ -f $fn ] ; then
bfn=`basename $fn` #trim path from filename
hdfs dfs -put $fn /here/is/some/non/existant/path/in/hdfs/$bfn
hdfs dfs -ls /here/is/some/non/existant/path/in/hdfs/$bfn >/dev/null
success=$? #check whether file landed in hdfs
if [ $success ] ; then
echo "remove local copy of file $fn"
#rm -f $fn #uncomment if you want to remove file
fi
fi
done