我在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?
答案 0 :(得分:2)
您基本上有两个选择:
1)使用hadoop流连接器创建MapReduce作业(这里只需要地图部分)。从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';
答案 1 :(得分:0)
如果您需要处理文件中的数据或移动/ cp / rm / etc。他们围绕文件系统然后PySpark(Spark with Python接口)将是最好的选择之一(速度,内存)。