hadoop学习笔记4:java实现hdfs -ls/-lsr

作业1:java代码实现hadoop fs -lsr的功能,调用FileSystem.listStatus(...)方法,显示结果,要求结果显示样式如hadoop fs -ls xxx的结果类似。
 


作业2:java 代码实现 hadoop fs -lsr 的功能
hadoop学习笔记4:java实现hdfs -ls/-lsr_第1张图片

------上面两题的代码如下:

package   hdfs;

import   java.io.IOException;
import   java.net.URI;
import   java.net.URISyntaxException;

import   org.apache.commons.lang.time.DateFormatUtils;
import   org.apache.hadoop.conf.Configuration;
import   org.apache.hadoop.fs.FileStatus;
import   org.apache.hadoop.fs.FileSystem;
import   org.apache.hadoop.fs.Path;

public   class   TestFIleSystem {
          public   static   void   main(String[] args)   throws   IOException,
                     URISyntaxException {
                 final   FileSystem fs = FileSystem.get(   new   URI( "hdfs://chaoren1:9012"   ),
                              new   Configuration());
               FileStatus[] listStatus = fs.listStatus(   new   Path( "/"   ));
                 // java代码实现hadoop fs - lsr的功能,调用FileSystem.listStatus(...)方法,显示结果,要求结果显示样式如 hadoop fs -ls  xxx的结果类似。
                 //ls (fs);
                 // java 代码实现 hadoop fs - lsr 的功能
               lsr(fs, listStatus);

       }

          private   static   void   lsr(FileSystem fs, FileStatus[] listStatus)
                        throws   IOException {

                 for   (FileStatus fileStatus : listStatus) {
                        // 判断是否是文件夹
                        final   boolean   isdir = fileStatus.isDir();

                        final   String permission = fileStatus.getPermission().toString();
                        final   short   replication = fileStatus.getReplication();

                        final   String group = fileStatus.getGroup().toString();
                        final   String owner = fileStatus.getOwner().toString();
                        final   long   blockSize = fileStatus.getBlockSize();
                        final   String modificationTime = DateFormatUtils.format (
                                  fileStatus.getModificationTime(),   "yyyy-MM-dd HH:mm:ss" );
                        final   String path = fileStatus.getPath().toString();
                        final   String substringPath = path.substring(20, path.length());
                     System.   out .println((isdir ?   "d"   :   "-"   )+ permission +   "  "
                                  + (replication > 0 ? replication :   "-" ).toString() +   " "
                                  + owner +   " "   + group +   "  "   + blockSize +   "\t\t"
                                  + modificationTime +   " "   + substringPath);
                        if   (isdir) {
                            lsr(fs, fs.listStatus(fileStatus.getPath()));
                     }

              }

       }

          private   static   void   ls(   final   FileSystem fs)   throws   IOException {

                 final   FileStatus[] listStatus = fs.listStatus( new   Path(   "/" ));
                 for   (FileStatus fileStatus : listStatus) {
                        // 判断是否是文件夹
                        final   boolean   isdir = fileStatus.isDir();

                        final   String permission = fileStatus.getPermission().toString();
                        final   short   replication = fileStatus.getReplication();

                        final   String group = fileStatus.getGroup().toString();
                        final   String owner = fileStatus.getOwner().toString();
                        final   long   blockSize = fileStatus.getBlockSize();
                        final   String modificationTime = DateFormatUtils.format (
                                  fileStatus.getModificationTime(),   "yyyy-MM-dd HH:mm:ss" );
                        final   String path = fileStatus.getPath().toString();
                        final   String substringPath = path.substring(20, path.length());
                     System.   out .println((isdir ?   "d"   :   "-"   ) + permission +   "  "
                                  + (replication > 0 ? replication :   "-" ).toString() +   " "
                                  + owner +   " "   + group +   "  "   + blockSize +   "\t\t"
                                  + modificationTime +   " "   + substringPath);
              }
       }
}


作业3: 从NameNode的源代码中找到NameNode是一个RPC Server的证据
      (提示,只需要找到RPC.getServer的调用即可)

hadoop学习笔记4:java实现hdfs -ls/-lsr_第2张图片

你可能感兴趣的:(hadoop学习笔记,hadoop集群,java,源代码,rpc)