2020-06-03


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

public class HdfsClient {
   //代码重构
    //1定义
    Configuration conf = null;
    FileSystem fs =null;
    private static final String HDFS_PATH="hdfs://abner:9000";
    private static final String USER_NAME="abner";

    //下面这样的结构方便加入类

    @Before//在它之前要做的
    public void before() throws IOException, InterruptedException {
        conf=new Configuration();
        conf.set("dfs.replication","2");//设置副本数(在网页50070页面的显示规格)
        conf.set("dfs.blocksize","5m");
        fs=FileSystem.get(URI.create(HDFS_PATH),conf,USER_NAME);
        System.out.println("Set Configuration Successfully!!!");
    }

    @Test
    public void testMkdir() throws IOException {
        System.out.println("Execute Mkdir Command !!!");
        fs.mkdirs(new Path("/test0/ltf_test"));
    }
   @Test
   public void testCopyFromLocalFile() throws IOException {
       System.out.println("Execute CopyFromLocalFile Command !!!");
       fs.copyFromLocalFile(new Path("D:\\BigData\\Hadoop\\hadoopTest\\copyTest\\test.txt"), new Path("/test0/test00/test001.txt"));
   }

   @Test
   public void testRename() throws IOException {
        System.out.println("Execute Rename Command !!!");
        fs.rename(new Path("/test0/test00/test001.txt"),new Path("/test0/rename_ltf.txt"));
   }

   @Test
   public void testDelete() throws IOException {
       System.out.println("Execute Delete Command !!!");
       //fs.delete(new Path("/rename_test.txt"));
         
       fs.delete(new Path("/rename_test.txt"),true);//recursive 递归向下删除
   }

    @Test
   public void testListFiles() throws IOException {
        //迭代器:Java泛型,<>里是泛型。远程迭代器。
       RemoteIterator<LocatedFileStatus> listFils = fs.listFiles(new Path("/test0"),true);

       while(listFils.hasNext()){                          //list之后还有就运行
           LocatedFileStatus status = listFils.next();

           //输出详情
           //输出文件名称,块大小,权限,用户组
           System.out.println("文件名称:"+status.getPath().getName());
           System.out.println("块大小:"+status.getBlockSize());
           System.out.println("权限:"+status.getPermission());
           System.out.println("用户组:"+status.getGroup());

           //获取存储的块信息
           BlockLocation [] blockLocations = status .getBlockLocations();
           for (BlockLocation blockLocation: blockLocations){
               String[] blockLocationHosts= blockLocation.getHosts();

               for(String blockLocationHost:blockLocationHosts){
                   System.out.println("节点:"+blockLocationHost);
               }
           }
           System.out.println("----------分割线---------------");
       }
   }

    //判断是否是文件夹
    @Test
    public void testListStatus() throws IOException {
        System.out.println("Execute ListStatus Command !!!");
        FileStatus[] listStatus = fs.listStatus(new Path("/test0"));
        for (FileStatus fileStatus:listStatus){
            if (fileStatus.isFile()){
                System.out.println("File: "+fileStatus.getPath().getName());
                System.out.println("------------------ ");
            }
            else {
                System.out.println("Directory: "+fileStatus.getPath().getName());
                System.out.println("------------------------------");
            }
        }
    }

   @After
   public void after() throws IOException {
        System.out.println("Close Resource  !!!");
        fs.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值