JAVA API操作HDFS文件系统

来自:http://blog.csdn.net/kkdelta/article/details/19910657

一个通过Java API操作HDFS文件系统的例子,本例子使用的是hadoop0.20的版本,在windows的eclipse下运行的时候,需要将core-site.xml和hdfs-site.xml放在src/bin目录中。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class TestHDFSFile {  
  2.   
  3.     private String localPath = "C:/D/JavaWorkSpace/bigdata/temp/";  
  4.     private String hdfsPath = "hdfs://192.168.2.6:9000/user/hadoop/temp/";  
  5.   
  6.     public static void main(String[] args) throws Exception {  
  7.         // new TestHDFSFile().testUpload();  
  8.         // new TestHDFSFile().testCreate();  
  9.         //new TestHDFSFile().testRename();  
  10.         //new TestHDFSFile().testDel();  
  11.         //new TestHDFSFile().testgetModifyTime();  
  12.         //new TestHDFSFile().testExists();  
  13.         //new TestHDFSFile().testFileBlockLocation();  
  14.           
  15.         new TestHDFSFile().testGetHostName();  
  16.           
  17.     }  
  18.   
  19.     // 上传本地文件到HDFS  
  20.     public void testUpload() throws Exception {  
  21.   
  22.         Configuration conf = new Configuration();  
  23.         // conf.addResource(new Path(localPath + "core-site.xml"));  
  24.         FileSystem hdfs = FileSystem.get(conf);  
  25.         Path src = new Path(localPath + "file01.txt");  
  26.         Path dst = new Path(hdfsPath);  
  27.         hdfs.copyFromLocalFile(src, dst);  
  28.   
  29.         System.out.println("Upload to " + conf.get("fs.default.name"));  
  30.         FileStatus files[] = hdfs.listStatus(dst);  
  31.         for (FileStatus file : files) {  
  32.             System.out.println(file.getPath());  
  33.         }  
  34.     }  
  35.   
  36.     // 创建HDFS文件  
  37.     public void testCreate() throws Exception {  
  38.         Configuration conf = new Configuration();  
  39.         byte[] buff = "hello world!".getBytes();  
  40.   
  41.         FileSystem hdfs = FileSystem.get(conf);  
  42.         Path dst = new Path(hdfsPath + "hello.txt");  
  43.         FSDataOutputStream outputStream = null;  
  44.         try {  
  45.             outputStream = hdfs.create(dst);  
  46.             outputStream.write(buff, 0, buff.length);  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.   
  50.         } finally {  
  51.             if (outputStream != null) {  
  52.                 outputStream.close();  
  53.             }  
  54.         }  
  55.   
  56.         FileStatus files[] = hdfs.listStatus(dst);  
  57.         for (FileStatus file : files) {  
  58.             System.out.println(file.getPath());  
  59.         }  
  60.     }  
  61.   
  62.     // 重命名HDFS文件  
  63.   
  64.     public void testRename() throws Exception {  
  65.   
  66.         Configuration conf = new Configuration();  
  67.   
  68.         FileSystem hdfs = FileSystem.get(conf);  
  69.         Path dst = new Path(hdfsPath);  
  70.   
  71.         Path frpath = new Path(hdfsPath + "hello.txt");  
  72.         Path topath = new Path(hdfsPath + "hello2.txt");  
  73.   
  74.         hdfs.rename(frpath, topath);  
  75.   
  76.         FileStatus files[] = hdfs.listStatus(dst);  
  77.         for (FileStatus file : files) {  
  78.             System.out.println(file.getPath());  
  79.         }  
  80.     }  
  81.   
  82.     // 刪除HDFS文件  
  83.     public void testDel() throws Exception {  
  84.   
  85.         Configuration conf = new Configuration();  
  86.   
  87.         FileSystem hdfs = FileSystem.get(conf);  
  88.         Path dst = new Path(hdfsPath);  
  89.   
  90.         Path topath = new Path(hdfsPath+ "hello2.txt");  
  91.   
  92.         boolean ok = hdfs.delete(topath, false);  
  93.         System.out.println(ok ? "删除成功" : "删除失败");  
  94.   
  95.         FileStatus files[] = hdfs.listStatus(dst);  
  96.         for (FileStatus file : files) {  
  97.             System.out.println(file.getPath());  
  98.         }  
  99.     }  
  100.   
  101.     // 查看HDFS文件的最后修改时间  
  102.     public void testgetModifyTime() throws Exception {  
  103.   
  104.         Configuration conf = new Configuration();  
  105.   
  106.         FileSystem hdfs = FileSystem.get(conf);  
  107.         Path dst = new Path(hdfsPath);  
  108.   
  109.         FileStatus files[] = hdfs.listStatus(dst);  
  110.         for (FileStatus file : files) {  
  111.             System.out.println(file.getPath() + "\t"  
  112.                     + file.getModificationTime());  
  113.               
  114.             System.out.println(file.getPath() + "\t"  
  115.                     + new Date(file.getModificationTime()));  
  116.               
  117.         }  
  118.     }  
  119.   
  120.     // 查看HDFS文件是否存在  
  121.   
  122.     public void testExists() throws Exception {  
  123.   
  124.         Configuration conf = new Configuration();  
  125.           
  126.         FileSystem hdfs = FileSystem.get(conf);  
  127.         Path dst = new Path(hdfsPath + "file01.txt");  
  128.   
  129.         boolean ok = hdfs.exists(dst);  
  130.         System.out.println(ok ? "文件存在" : "文件不存在");  
  131.     }  
  132.   
  133.     // 查看某个文件在HDFS集群的位置  
  134.     public void testFileBlockLocation() throws Exception {  
  135.   
  136.         Configuration conf = new Configuration();  
  137.   
  138.         FileSystem hdfs = FileSystem.get(conf);  
  139.         Path dst = new Path(hdfsPath + "file01.txt");  
  140.   
  141.         FileStatus fileStatus = hdfs.getFileStatus(dst);  
  142.         BlockLocation[] blockLocations = hdfs.getFileBlockLocations(fileStatus,  
  143.                 0, fileStatus.getLen());  
  144.         for (BlockLocation block : blockLocations) {  
  145.             System.out.println(Arrays.toString(block.getHosts()) + "\t"  
  146.                     + Arrays.toString(block.getNames()));  
  147.         }  
  148.     }  
  149.   
  150.     // 获取HDFS集群上所有节点名称  
  151.     public void testGetHostName() throws Exception {  
  152.   
  153.         Configuration conf = new Configuration();  
  154.           
  155.         DistributedFileSystem hdfs = (DistributedFileSystem) FileSystem  
  156.                 .get(conf);  
  157.         DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();  
  158.   
  159.         for (DatanodeInfo dataNode : dataNodeStats) {  
  160.             System.out.println(dataNode.getHostName() + "\t"  
  161.                     + dataNode.getName());  
  162.         }  
  163.     }  
  164. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值