step1
用elipse 建立一个新的工程,并命名为ListHDFS
step2
右键点击工程,Properties->Java Build Path->Libraries点击Add External JARs,选择在${HADOOP_HOME}上的.jar文件
step3
新建Class文件,并命名为ListAllFile.java
step4
输入文件
- <pre name="code" class="java">import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileStatus;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.FileUtil;
- import org.apache.hadoop.fs.Path;
- public class ListAllFile {
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(conf);
- Path[] paths = new Path[args.length];
- for (int i=0; i<paths.length; i++) {
- paths[i] = new Path(args[i]);
- }
- FileStatus[] status = fs.listStatus(paths);
- Path[] listedPaths = FileUtil.stat2Paths(status);
- for(Path p: listedPaths) {
- System.out.println(p);
- }
- }
- }
step5
确认没有错误后右键点击工程->export, 在Select and export destination: 下面输入JAR,选择JAR file,去掉.classpath 和 .project选项,并且输入JAR文件的输出路径,缺省情况下输出在workspace根路径下。
step6
在JAR的输出路径上键入
hadoop jar ListAllFiles.jar ListAllFile /user/ (前提是hadoop的运行环境已经启动,并且hadoop已经添加进PATH环境变量)