通过 Python 处理 HDFS 中的多个文件
Processing multiple files in HDFS via Python
我在 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 job(这里你只需要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';
如果您需要处理文件中的数据或move/cp/rm/etc。它们围绕文件系统然后 PySpark(具有 Python 接口的 Spark)将是最好的选择之一(速度,内存)。
我在 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 job(这里你只需要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';
如果您需要处理文件中的数据或move/cp/rm/etc。它们围绕文件系统然后 PySpark(具有 Python 接口的 Spark)将是最好的选择之一(速度,内存)。