java操作HDFS (API) 常用操作亲测代码

package com.yxc.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.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;


/**
 * 测试HDFSAPI
 *
 * @author yxc
 * @create 2018-05-03 18:21
 **/
public class TestHDFS04 {
    private Configuration cf = null;
    private FileSystem fs = null;

    /**
     * 获取配置信息,以及获取文件系统
     */
    @Before
    public void beforeRun() throws URISyntaxException, IOException, InterruptedException {
        //获取配置信息
        cf = new Configuration();

        //获取文件系统
        fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), cf, "yxc");
    }


    /**
     * 创建文件目录
     */
    @Test
    public void mkdir () throws IOException {
        boolean mkdirs = fs.mkdirs(new Path("/input/"));
        if (mkdirs) {
            System.out.println("文件创建成功");
        } else {
            System.out.println("文件创建失败");
        }
    }

    /**
     * 判断目录是否存在
     */
    @Test
    public void isDirectory() throws IOException {
        boolean directory = fs.isDirectory(new Path("/input/"));
        if (directory) {
            System.out.println("存在input 的这个目录");
        } else {
            System.out.println("不存在input的这个目录");
        }
    }

    /**
     * 删除文件或文件目录
     */
    @Test
    public void delete() throws IOException {
        boolean delete = fs.delete(new Path("/input/"), true);
        if (delete) {
            System.out.println("成功删除");
        } else {
            System.out.println("删除失败");
        }
    }

    /**
     * 获取文件下的资源
     */
    @Test
    public void getFile() throws IOException {
        RemoteIterator listFiles = fs.listFiles(new Path("/"), true);
        if (listFiles == null) {
            System.out.println("文件下没有资源");
        } else {
            System.out.println("文件下有资源");
        }
        while (listFiles.hasNext()) {
            LocatedFileStatus file = listFiles.next();
            if (file.isDirectory()) {
                System.out.println("是文件夹");
            } else {
                System.out.println("是文件");
            }
            System.out.println("文件名:"+file.getPath().getName());
        }

    }

    /**
     * 上传文件
     */
    @Test
    public void upFile() throws IOException {
        //配置信息

        //获取文件系统

        //获取输入流
        FileInputStream inputStream = new FileInputStream(new File("D:/yue5.txt"));

        //获取输出流
        FSDataOutputStream outputStream = fs.create(new Path("/input/yue5.txt"), true);

        //对接流数据
        IOUtils.copyBytes(inputStream, outputStream, cf, true);

        System.out.println("上传完成");

        //关闭资源
        IOUtils.closeStream(inputStream);
        IOUtils.closeStream(outputStream);
    }


    /**
     * 下载文件
     */
    @Test
    public void downFile() throws IOException {
        //获取配置信息
        //获取文件系统
        //创建输入流
        FSDataInputStream inputStream = fs.open(new Path("/input/yue4.txt"));

        //创建输出流
        FileOutputStream outputStream = new FileOutputStream(new File("D:/yue5.txt"));

        //流数据对接
        IOUtils.copyBytes(inputStream, outputStream, cf, true);

        System.out.println("下载完成");
        //关闭资源
        IOUtils.closeStream(inputStream);
        IOUtils.closeStream(outputStream);
    }

    /**
     * 获取节点信息
     */
    @Test
    public void getNodeInfo() throws IOException {
        RemoteIterator listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println(fileStatus.getPath().getName());
            if (fileStatus.isDirectory()) {
                System.out.println("副本数"+fileStatus.getReplication());
                System.out.println("块大小"+fileStatus.getBlockSize());
                System.out.println("文件夹");
            } else {
                System.out.println("副本数"+fileStatus.getReplication());
                System.out.println("块大小"+fileStatus.getBlockSize());
                System.out.println("不是文件夹");
                BlockLocation[] blockLocations = fileStatus.getBlockLocations();
                for (int i = 0; i < blockLocations.length; i++) {
                    String[] hosts = blockLocations[i].getHosts();
                    for (String host : hosts) {
                        System.out.println(host);
                    }
                    System.out.println("--------------每个块分解线--------------");
                }
            }


        }

    }

    /**
     * 文件重命名
     */
    @Test
    public void reName() throws IOException {
        boolean rename = fs.rename(new Path("/input/yue.txt"), new Path("/input/yuenew.txt"));
        if (rename) {
            System.out.println("成功重新命名");
        } else {
            System.out.println("命名失败");
        }
    }

    /**
     * 查看HDFS文件的最后修改时间
     */

    @Test
    public void getLastTime() throws IOException {
        FileStatus fileStatus = fs.getFileStatus(new Path("/input/yuenew.txt"));
        long modificationTime = fileStatus.getModificationTime();
        System.out.println(modificationTime);
        System.out.println(new Date(modificationTime));
    }

    /**
     * 关闭资源
     * @throws IOException
     */
    @After
    public void afterRun () throws IOException {
        fs.close();
    }

}

你可能感兴趣的:(大数据报错及解决方案,大数据开发)