HDFS的API操作(java)

package com.atguigu.hdfs;

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.IOException;
import java.net.URI;

public class HDFSClient {
    FileSystem fileSystem;

    /**
     * 上传
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void put() throws IOException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "2");
        configuration.set("dfs.blocksize", "67108864");


        //1. 新建HDFS对象
        FileSystem fileSystem = FileSystem.get(URI.create("hdfs://hadoop102:8020"),
                configuration, "atguigu");


        //2. 操作集群
        fileSystem.copyFromLocalFile(
                new Path("D:\\Atguigu\\302_尚硅谷大数据技术之hadoop\\2.资料\\01_jar包\\hadoop-3.1.3.tar.gz"),
                new Path("/1.tar.gz"));

        //3. 关闭资源
        fileSystem.close();
    }

    @Before
    public void before() throws IOException, InterruptedException {
        //1. 新建HDFS对象
        fileSystem = FileSystem.get(URI.create("hdfs://hadoop102:8020"),
                new Configuration(), "atguigu");
    }

    @After
    public void after() throws IOException {
        fileSystem.close();
    }

    /**
     * 下载
     *
     */
    @Test
    public void get() throws IOException {
        fileSystem.copyToLocalFile(
                new Path("/1.tar.gz"),
                new Path("d:/")
        );
    }

    /**
     * 追加
     */
    @Test
    public void append() throws IOException {

        FSDataOutputStream append = fileSystem.append(
                new Path("/README.txt")
        );

        append.write("TestAPI".getBytes());

        IOUtils.closeStream(append);
    }

    /**
     * 查看文件和文件夹
     * @throws IOException
     */
    @Test
    public void ls() throws IOException {
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.getPath());
            System.out.println(fileStatus.getOwner());
            System.out.println("=================");
        }
    }

    /**
     * 查看文件
     * @throws IOException
     */
    @Test
    public void lf() throws IOException {
        RemoteIterator<LocatedFileStatus> statusRemoteIterator =
                fileSystem.listFiles(new Path("/"), true);
        while (statusRemoteIterator.hasNext()) {
            LocatedFileStatus fileStatus = statusRemoteIterator.next();

            System.out.println(fileStatus.getPath());
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (int i = 0; i < blockLocations.length; i++) {
                System.out.println("第" + i + "块");
                String[] hosts = blockLocations[i].getHosts();
                for (String host : hosts) {
                    System.out.print(host + " ");
                }
                System.out.println();
            }

            System.out.println("===================================");

        }
    }

    /**
     * 移动
     */
    @Test
    public void mv() throws IOException {
        fileSystem.rename(new Path("/1.tar.gz"),
                new Path("/logs/2.tar.gz"));
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值