HDFS API实操

public static final HDFS_PATH="hdfs url";
FileSystem fileSystem = null;
Configuration configuration =null;

@Before
public void setUp() throws Exception{
    System.out.println("----------setUp---------");
    configuration = new Configuration();
    /**
    *构造一个访问指定HDFS系统的客户端对象
    *第一个参数: HDFS的URI
    *第二个参数: 客户端指定的配置参数
    *第三个参数: 客户端的身份(用户名)
    */
    fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
}
/**
 * 创建HDFS目录
 */
@Test
public void mkdir() throws Exception{
    fileSystem.mkdir(new Path("/hdfs/test"));
}

/**
 * 查看HDFS文件
 */
@Test
public void readHDFSFile() throws Exception{
    FSDataInputStream in = fileSystem.open(new Path("hdfs 文件路径"));
    IOUtils.copyBytes(in, System.out, 1024);
}

/**
 * 创建HDFS文件 获取副本数  取hdfs-default.xml里的默认值
 */
@Test
public void readHDFSFile() throws Exception{
    FSDataOutStream out = fileSystem.create(new Path("hdfs 文件路径"));
    out.writeUTF("Hello HDFS");
    out.flush();
    out.close();//查看文件副本数 是3
    System.out.println(configuration.get("dfs.replication"));
    
    configruation.setReplication("1");
    out = fileSystem.create(new Path("hdfs 文件路径"));
    out.writeUTF("Hello HDFS replication");
    out.flush();
    out.close(); //查看文件副本数 是1
    System.out.println(configuration.get("dfs.replication"));
}

/**
 * 重命名HDFS文件
 */
@Test
public void renameHDFSFile() throws Exception{
    Path oldPath = new Path("");
    Path newPath = new Path("");
    boolean result = fileSystem.rename(oldPath,newPath);
    System.out.println(result);
}

/**
 * 拷贝本地文件到HDFS文件系统
 */
@Test
public void copyFromLocalFile() throws Exception{
    Path src = new Path("");
    Path dst = new Path("");
    boolean result = fileSystem.copyFromLocalFile(src,dst);
    System.out.println(result);
}

/**
 * 拷贝本地大文件到HDFS文件系统:进度条展示
 */
@Test
public void copyBigFile() throws Exception{
    InputStream in = new BufferedInputStream(new FileInputStream(new File("本地大文件")));
    FSDataOutputStream out = fileSystem.create(new Path("HDFS 目标文件"),new Processable(){
        public void progress(){
            System.out.println(".");
        }
    });
    IOUtils.copyBytes(in, out, 4096);
}
/**
 * 拷贝HDFS文件系统到本地
 */
@Test
public void copyToLocalFile() throws Exception{
    Path src = new Path("");
    Path dst = new Path("");
    boolean result = fileSystem.copyToLocalFile(src,dst);
    System.out.println(result);
}

/**
 * 查看目标文件夹的文件
 */
@Test
public void getFileStatus() throws Exception{
    FileStatus[] statues = fileSystem.listStatus(new Path("HDFS 文件夹路径"));
    for(FileStatus file : statues){
        String isDir = file.isDirectory? "文件夹":"文件";
        String permission = file.getPermission().toString();
        short replication = file.getReplication();
        long length = file.getLen();
        String path = file.getPath().toString();
        System.out.println(isDir +"\t" + permission
        +"\t"+replication + "\t"+length
        +"\t"+path);
    }
}

/**
 * 列出HDFS文件夹下所有文件, true 递归读取
 */
@Test
public void listFiles() throws Exception{
    RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("HDFS 文件夹路径"),true);
    while(files.hasNext()){
        LocatedFileStatus file = files.next();
        String isDir = file.isDirectory? "文件夹":"文件";
        String permission = file.getPermission().toString();
        short replication = file.getReplication();
        long length = file.getLen();
        String path = file.getPath().toString();
        System.out.println(isDir +"\t" + permission
        +"\t"+replication + "\t"+length
        +"\t"+path);
    }
}

/**
 * 查看HDFS文件的block信息
 */
@Test
public void getFileBlockLocations() throws Exception{
    FileStatus[] fileStatus = fileSystem.listStatus(new Path("HDFS 文件夹路径"));
    BlockLocation[] blocks = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen() )
    for(BlockLocation block : blocks){
        for(String name :block.getNames()){
            System.out.println(name + " : "+ block.getOffset()+" : "+ block.getLength());
        }
    }
}

/**
 * 删除HDFS文件
 */
@Test
public void getFileBlockLocations() throws Exception{
    boolean result = fileSystem.delete(new Path("HDFS 文件夹路径"),true);
    System.out.println(result);
}

@After
public void tearDown(){
    FileSystem fileSystem = null;
    Configuration configuration =null;
    System.out.println("----------tearDown---------");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值