HDFS-Java整合

1、创建一个项目

2、导入相关的Jar包



创建一个JUint测试类:

[html]   view plain  copy
  1. package com.hdfs.testHdfs;  
  2.   
  3. import java.io.FileInputStream;  
  4.   
  5. import java.io.FileOutputStream;  
  6.   
  7. import java.io.InputStream;  
  8.   
  9. import java.io.OutputStream;  
  10.   
  11. import java.net.URI;  
  12.   
  13. import org.apache.commons.compress.utils.IOUtils;  
  14.   
  15. import org.apache.hadoop.conf.Configuration;  
  16.   
  17. import org.apache.hadoop.fs.FSDataInputStream;  
  18.   
  19. import org.apache.hadoop.fs.FSDataOutputStream;  
  20.   
  21. import org.apache.hadoop.fs.FileStatus;  
  22.   
  23. import org.apache.hadoop.fs.FileSystem;  
  24.   
  25. import org.apache.hadoop.fs.Path;  
  26.   
  27. import org.junit.Before;  
  28.   
  29. import org.junit.Test;  
  30.   
  31. /**  
  32.  *   
  33.  * Title: HDFSTest  
  34.  * Description: 测试HDFS  
  35.  * Company: http://blog.csdn.net/lu1005287365/  
  36.  * @author  LYW  
  37.  * @date    2016年3月22日 19:35:07  
  38.  * @version 1.0  
  39.  */  
  40. public class HDFSTest {  
  41.   
  42.     // hdfs操作核心类  
  43.     FileSystem fileSystem = null;  
  44.   
  45.     @Before  
  46.   
  47.     public void setUp() throws Exception {  
  48.   
  49.         URI uri = new URI("hdfs://hadoop:9000");  
  50.   
  51.         Configuration configuration = new Configuration();  
  52.         /**  
  53.          * 这里Root如果不设置的话 将没有权限  
  54.          * 这里推荐使用root 不设置 可以在hdfs-site.xml配置 意思是关闭权限验证  
  55.          * 所有用户都可以操作  
  56.          *  <property>  
  57.                 <name>dfs.permissions</name>  
  58.                 <value>false</value>  
  59.             </property>  
  60.          *   
  61.          */  
  62.         fileSystem = FileSystem.get(uri, configuration, "root");  
  63.   
  64.     }  
  65.   
  66.     @Test  
  67.     public void getPathAllFiles() throws Exception {  
  68.   
  69.         // 递归方式取得指定目录下的文件信息  
  70.   
  71.         FileStatus[] fileStatus = fileSystem.listStatus(new Path("/"));  
  72.   
  73.         // 输出取得的文件权限和文件名称  
  74.         System.out.println("权限\t\t名称\t\t文件大小\t 修改时间");  
  75.         for (FileStatus fileStatus1 : fileStatus) {  
  76.             System.out.print(fileStatus1.getPermission() + "\t" + fileStatus1.getPath());  
  77.             System.out.print("\t"+fileStatus1.getLen());  
  78.             System.out.print("\t"+fileStatus1.getModificationTime());  
  79.             System.out.println();  
  80.         }  
  81.   
  82.     }  
  83.   
  84.     // 上传  
  85.   
  86.     @Test  
  87.     public void put() throws Exception {  
  88.         // 如果同名是否覆盖   
  89.         boolean overwrite = true;  
  90.         // DFS文件输出流  
  91.         FSDataOutputStream outputStream = fileSystem.create(new Path("/测试文件.zip"), overwrite);  
  92.         // 本地输入流  
  93.         InputStream inputStream = new FileInputStream("D://测试文件.zip");  
  94.         // 使用工具类上传  
  95.         IOUtils.copy(inputStream, outputStream, 4096);  
  96.     }  
  97.   
  98.     // 下载  
  99.     @Test  
  100.     public void get() throws Exception {  
  101.         // DFS文件输入流  
  102.         FSDataInputStream inputStreamOption = fileSystem.open(new Path("/测试文件.zip"));  
  103.         // 本地文件输出流  
  104.         OutputStream outputStream = new FileOutputStream("E:\\测试文件.zip");  
  105.         // 使用工具类下载  
  106.         IOUtils.copy(inputStreamOption, outputStream, 4096);  
  107.     }  
  108.   
  109.     // 删除  
  110.     @Test  
  111.     public void del() throws Exception {  
  112.         fileSystem.delete(new Path("/input"), true);  
  113.   
  114.     }  
  115.   

希望多多指点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值