sar*_*ara 3 python directory hadoop file python-2.7
如何使用python脚本从hdfs(hadoop)目录中获取文件列表?
我试过以下行:
dir = sc.textFile(“ hdfs://127.0.0.1:1900 / directory”).collect()
该目录具有文件“ file1,file2,file3 .... fileN”的列表。通过使用该行,我仅获得了所有内容列表。但是我需要获取文件名列表。
谁能帮我找出这个问题?
提前致谢。
使用子流程
import subprocess
p = subprocess.Popen("hdfs dfs -ls <HDFS Location> | awk '{print $8}'",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line
Run Code Online (Sandbox Code Playgroud)
编辑:没有python的答案。第一个选项也可用于递归打印所有子目录。可以根据您的要求省略或更改最后一个重定向语句。
hdfs dfs -ls -R <HDFS LOCATION> | awk '{print $8}' > output.txt
hdfs dfs -ls <HDFS LOCATION> | awk '{print $8}' > output.txt
Run Code Online (Sandbox Code Playgroud)
编辑:更正awk命令中缺少的引号。
| 归档时间: |
|
| 查看次数: |
6145 次 |
| 最近记录: |