我在 HDFS 中有一个目录,其中包含大约 10,000 个 .xml 文件。我有一个 python 脚本“processxml.py”,它接受一个文件并对其进行一些处理。是否可以对 hdfs 目录中的所有文件运行该脚本,或者我是否需要先将它们复制到本地才能执行此操作?
例如,当我在本地目录中的文件上运行脚本时,我有:
cd /path/to/files
for file in *.xml
do
python /path/processxml.py
$file > /path2/$file
done
所以基本上,我将如何做同样的事情,但这次文件位于 hdfs 中?
最佳答案
你基本上有两个选择:
1) 使用hadoop Streaming Connector 创建MapReduce 作业(这里只需要Map 部分)。从 shell 或 shell 脚本内使用此命令:
hadoop jar <the location of the streamlib> \
-D mapred.job.name=<name for the job> \
-input /hdfs/input/dir \
-output /hdfs/output/dir \
-file your_script.py \
-mapper python your_script.py \
-numReduceTasks 0
2) 创建 PIG 脚本并发送您的 Python 代码。以下是该脚本的基本示例:
input_data = LOAD '/hdfs/input/dir';
DEFINE mycommand `python your_script.py` ship('/path/to/your/script.py');
updated_data = STREAM input_data THROUGH mycommand PARALLEL 20;
STORE updated_data INTO 'hdfs/output/dir';
关于python - 通过Python处理HDFS中的多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35070998/