• 大数据学习之HDFS基本API操作(上)06


      1 package it.dawn.HDFSPra;
      2 
      3 import java.io.FileNotFoundException;
      4 import java.io.IOException;
      5 import java.net.URI;
      6 import java.net.URISyntaxException;
      7 import java.util.Arrays;
      8 
      9 import org.apache.hadoop.conf.Configuration;
     10 import org.apache.hadoop.fs.FileStatus;
     11 import org.apache.hadoop.fs.FileSystem;
     12 import org.apache.hadoop.fs.LocatedFileStatus;
     13 import org.apache.hadoop.fs.Path;
     14 import org.apache.hadoop.fs.RemoteIterator;
     15 import org.junit.Before;
     16 import org.junit.Test;
     17 
     18 /**
     19  * @version 1.0
     20  * @author Dawn
     21  * @since 2019年4月27日22:42:32
     22  * 
     23  * 注意:1.需要配置hadoop环境
     24  *         2.需要编译好的winutil包
     25  */
     26 public class HdfsClientDemo01 {
     27     
     28     public static FileSystem fs=null;
     29     public static String hdfs="hdfs://192.168.40.11:9000";
     30     
     31     //初始化加载
     32     @Before
     33     public void init() throws IOException, InterruptedException, URISyntaxException 
     34     {
     35         System.setProperty("hadoop.home.dir", "E:\hadoop2.7.3\hadoop-2.7.3");
     36         // 1.加载配置
     37         Configuration conf= new Configuration();
     38         // 2.设置副本数 默认3块
     39         conf.set("dfs.replication", "2");
     40         // 3.设置块大小 默认128m
     41         conf.set("dfs.blocksize", "64m");
     42         // 4.构造客户端
     43         fs=FileSystem.get(new URI(hdfs), conf, "root");
     44     }
     45     
     46     /*
     47      * 上传文件
     48      * hdfs dfs -put /文件名 /hdfs目录
     49      */
     50     @Test
     51     public void hdfsUpData() throws IllegalArgumentException, IOException 
     52     {
     53         //上传文件
     54         fs.copyFromLocalFile(new Path("f:/temp/xxx.txt"), new Path("/"));
     55         //2.关闭资源
     56         fs.close();
     57     }
     58     
     59 
     60     /*
     61      * 下载文件
     62      * hdfs dfs -get /hdfs文件 /文件目录
     63      */
     64     @Test
     65     public void hdfsDownload() throws IllegalArgumentException, IOException {
     66         //下载文件
     67         fs.copyToLocalFile(new Path("/xxx.txt"), new Path("f:/"));
     68         
     69         fs.close();
     70     }
     71     
     72     
     73     /*
     74      * 1:在hdfs中创建文件夹
     75      * hdfs dfs -mkdir /文件名
     76      */
     77     @Test
     78     public void hdfsMkdir() throws IllegalArgumentException, IOException 
     79     {
     80         //1.调用创建文件夹方法
     81         fs.mkdirs(new Path("/dawn/ss"));
     82         System.out.println("创建成功");
     83         //2.关闭资源
     84         fs.close();
     85     }
     86     
     87     /*
     88      * 2:在hdfs中 移动/修改 文件 
     89      * 相当于Ctrl+x
     90      */
     91     @Test
     92     public void hdfsRename() throws IllegalArgumentException, IOException 
     93     {
     94         //1.调用移动并修改
     95         fs.rename(new Path("/test/a.txt"), new Path("/dawn/renamea.txt"));
     96         //2.关闭资源
     97         fs.close();
     98     }
     99     
    100     /*
    101      * 3:在hdfs中删除文件夹
    102      * hdfs dfs -rm -r /
    103      */
    104     @Test
    105     public void hdfsRm() throws IllegalArgumentException, IOException
    106     {
    107         //fs.delete(new Path("/hunterhenshuai"));
    108         //1.调用删除文件方法 参数1:删除的路径 参数2:是否递归删除
    109         fs.delete(new Path("/test"), true);
    110         fs.close();
    111         
    112     }
    113     
    114     /*
    115      * 4:查询hdfs下制定的目录信息
    116      */
    117     @Test
    118     public void hdfsLs() throws FileNotFoundException, IllegalArgumentException, IOException 
    119     {
    120         //1.调用方法 返回远程迭代器 
    121         RemoteIterator<LocatedFileStatus> iter=fs.listFiles(new Path("/dawn"), true);
    122         
    123         //2.取迭代器数据
    124         while(iter.hasNext()) 
    125         {
    126             //拿数据
    127             LocatedFileStatus status=iter.next();
    128             
    129             System.out.println("文件路径为:"+status.getPath());
    130             System.out.println("块大小为:"+status.getBlockSize() / (1024*1024) );
    131             System.out.println("文件长度:"+ status.getLen());
    132             System.out.println("副本数量为:"+status.getReplication());
    133             System.out.println("块信息为:"+Arrays.asList(status.getBlockLocations()));
    134             
    135             System.out.println("============================>");
    136         }
    137         
    138         //3.关闭资源
    139         fs.close();
    140         
    141     }
    142     
    143     /*
    144      * 5:判断文件还是文件夹
    145      */
    146     @Test
    147     public void findIsFOrD() throws FileNotFoundException, IllegalArgumentException, IOException {
    148         //1.展示状态信息
    149         FileStatus[] listStatus = fs.listStatus(new Path("/"));
    150         
    151         //2.遍历所有文件
    152         for(FileStatus ls:listStatus) {
    153             if(ls.isFile()) {
    154                 //文件
    155                 System.out.println("文件----f----" +ls.getPath().getName());
    156             }else {
    157                 //文件夹
    158                 System.out.println("文件夹----d----" +ls.getPath().getName());
    159             }
    160         }
    161     }
    162 
    163 }
  • 相关阅读:
    Apache Jmeter 性能测试
    linux 达梦数据库 命令行 卸载
    vue控制台报错Duplicate keys detected: 'xxxx'. This may cause an update error.解决方案
    springboot报错说 Failed to parse multipart servlet request; nested exception is java.io.IOException
    简单理解Callable接口
    Windows下设置Mongodb用户名密码
    Win10 搭建FTP环境,并使用Java实现上传,下载,删除
    【小组展示】1
    【计网II】
    【密码学】AES
  • 原文地址:https://www.cnblogs.com/hidamowang/p/10781139.html
Copyright © 2020-2023  润新知