Hadoop系统操作类FileSystem

FileSystem类,Hadoop文件API的起点,是一个一个与文件系统交互的抽象类,其对HDFS的操作由不同的具体实现子类来实现。

通过下面的方法来获取一个具体的FileSystem实例:
获取用于HDFS系统的实例:public static FileSystem get(Configuration conf) throws IOException
获取用于本地文件系统的实例:public static LocalFileSystem getLocal(Configuration conf) throws IOException
  
  
FileStatus类:可以获取元数据的信息,包括文件的长度,权限,修改时间等等
public class FileStatus
  
  
   
   extends 
   
   Object
  
  
  
  
   
   implements 
   
   Writable, 
   
   Comparable
  
  
Interface that represents the client side information for a file. 
例子:  
  
  
[java] view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileStatus;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6. public class FileSystemDemo01 {  
  7.     public static void main(String[] args) throws IOException {  
  8.         Configuration conf = new Configuration();  
  9.         FileSystem hdfs = FileSystem.get(conf);/*操作HDFS文件系统的类*/  
  10.           
  11.     //  FileSystem local = FileSystem.getLocal(conf);/*操作本地文件系统的类*/  
  12.           
  13.         Path inputDir = new Path(args[0]);/*获取文件的路径*/  
  14.         /*FileStatue类*/  
  15.         FileStatus[] inputFiles = hdfs.listStatus(inputDir);/*得到文件路径目录下文件列表*/  
  16.           
  17.         System.out.println(inputFiles.length);  
  18.         System.out.println(inputFiles[0].toString());  
  19.         System.out.println(inputFiles[0].getGroup());  
  20.         System.out.println(inputFiles[0].getModificationTime());  
  21.     }  
  22. }  
  23. /*输出结果: 
  24. 1 
  25. org.apache.hadoop.fs.FileStatus@a82e7892 
  26. supergroup 
  27. 1336053927670 
  28.  
  29.  */  
  
  
一个综合例子:把本地D:\test下的文件复制到HDFS文件/usr/test2下
 
 
  
  
[java] view plain copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FSDataInputStream;  
  5. import org.apache.hadoop.fs.FSDataOutputStream;  
  6. import org.apache.hadoop.fs.FileStatus;  
  7. import org.apache.hadoop.fs.FileSystem;  
  8. import org.apache.hadoop.fs.Path;  
  9.   
  10.   
  11. public class PutMerge {  
  12.     public static void main(String[] args) throws IOException {  
  13.         Configuration conf = new Configuration();  
  14.         FileSystem hdfs = FileSystem.get(conf);  
  15.         FileSystem local = FileSystem.getLocal(conf);  
  16.           
  17.         Path localDir = new Path(args[1]);  
  18.         Path hdfsDir = new Path(args[0]);  
  19.           
  20.         FileStatus[] inputFile = local.listStatus(localDir);  
  21.         FSDataOutputStream out = hdfs.create(hdfsDir);  
  22.           
  23.         for(int i=0;i<inputFile.length;i++){  
  24.             System.out.println(inputFile[i].getPath().getName());  
  25.             FSDataInputStream in = local.open(inputFile[i].getPath());  
  26.             byte[] buffer = new byte[255];  
  27.             int byteRead = 0;  
  28.             while((byteRead=in.read(buffer))>0){  
  29.                 out.write(buffer,0,byteRead);  
  30.             }  
  31.             out.close();  
  32.         }  
  33.     }  
  34. }  
  35. /*输出结果 
  36.  12/05/04 13:37:24 WARN conf.Configuration: DEPRECATED: hadoop-site.xml found in the classpath. Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, mapred-site.xml and hdfs-site.xml to override properties of core-default.xml, mapred-default.xml and hdfs-default.xml respectively 
  37. 4d aa.txt 
  38. aa.txt 
  39. bbaa.txt 
  40. ccaa.txt 
  41.   
  42. */  
http://blog.csdn.net/jtlyuan/article/details/7535983
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值