6.HDFS文件上传和下载API

                                 HDFS文件上传和下载API

package hdfsAPI;

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

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 李庆
 * 2019-8-25
 * HDFS的上传与下载
 */
public class Hdfs1API {
    //实例化hadoop配置文件
    Configuration conf = new Configuration();
    //获取文件系统
    //Shift+F2 快速找到错误提
    FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"), conf, "root");

    public Hdfs1API() throws InterruptedException, IOException, URISyntaxException {
    }

    @Test
    public void initHdfs() throws IOException {

        //Ctrl+Alt+v 后推
        //Ctrl+Alt+O 去除无用的包
        //Ctrl+Alt+空格 提示

        //打印文件系统
        System.out.println(fs.toString());

        //关闭
        fs.close();


    }

    @Test
    /**
     * 从本地文件上传到
     */
    public void putFileHdfs() throws URISyntaxException, IOException, InterruptedException {

        //定义文件的输入路径
        Path src = new Path("D:\\input\\test\\aa.txt");
        //定义文件的输出路径
        Path out = new Path("hdfs://bigdata111:9000/teacher.txt");
        //上传
        fs.copyFromLocalFile(src, out);
        //关闭
        fs.close();
              System.out.println("teacher.txt上传成功");

    }

    @Test
    /**
     * 从HDFS文件下载到本地
     */
    public void getFileHdfs() throws IOException {
        //定义文件Hdfs下载的路径
        Path output = new Path("/teacher.txt");

        //定义下载到win中文件路径
        Path input = new Path("D:\\input\\test\\student.txt");

        //下载
        fs.copyToLocalFile(false, output, input, true);

        //关闭
        fs.close();

        System.out.println("plus下载成功");
    }

    @Test
    /**
     * 创建目录
     */
    //上传目录
    public void mkdirHdfs() throws IOException {
        //上传目录
        Path path = new Path("/0825");
        //创建目录
        fs.mkdirs(path);
        //关闭
        fs.close();

        System.out.println("创建目录0825成功");

    }

    @Test
    /**
     * 删除目录
     */
    public void deldirHdfs() throws IOException {
        //找出删除目录
        Path path = new Path("/0825");
        //删除目录
        fs.delete(path);
        //关闭
        fs.close();
        System.out.println("删除目录0825成功");

    }

    @Test
    /**
     * 重命名
     */
    public void renameHdfs() throws IOException {

        //重命名文件
        fs.rename(new Path("/0825"), new Path("/0826"));
        //关闭
        fs.close();
        System.out.println("重命名成功");
    }

    @Test
    /**
     *查看文件详情
     */
    public void readFileHdfs() throws IOException {
        //获取到目录里面的所有的文件迭代器
        //文件目录可以形成迭代器,里面有许多文件
        RemoteIterator<LocatedFileStatus> listfiles = fs.listFiles(new Path("/"), true);

        while (listfiles.hasNext()) {
            //拿到每个文件的对象,listFiles.next
            LocatedFileStatus file = listfiles.next();
            //
            System.out.println("文件名字" + file.getPath().getName());
            System.out.println("文件的块大小" + file.getBlockSize());
            System.out.println("文件的长度" + file.getLen());
            System.out.println("=================分隔符");

            BlockLocation[] blockLocations = file.getBlockLocations();
            for (BlockLocation block : blockLocations) {
                //getOffset()偏移量
                System.out.println("块的偏移量" + block.getOffset());
                //每个块放在哪个机器上
                String[] hosts = block.getHosts();
                for (String host : hosts) {
                    System.out.println(host);

                }
            }
        }
    }

    @Test
    /**
     * 判断是否是文件
     */
    public void isFile() throws IOException {

        //获取目录所有的路径的对象
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus status : fileStatuses) {
            if (status.isFile()) {
                System.out.println(status.getPath().getName() + "是文件");
            } else {
                System.out.println(status.getPath().getName() + "是文件夹");
            }
        }
        fs.close();
    }

    @Test
    /**
     * HDFS文件下载到Win中
     */
    public void putFileHdfsIO() throws IOException {

        //创建输入流
        FSDataInputStream fis = fs.open(new Path("/teacher.txt"));
        //创建输出流
        FileOutputStream fos = new FileOutputStream(new File("D:\\input\\test\\teacher1.txt"));
        //对接流
        IOUtils.copyBytes(fis, fos, 5 * 1024, true);
        fs.close();
        System.out.println("下载成功");
    }

    @Test
    /**
     * win文件上传到HDFS中
     */

    public void inFileHDFS() throws IOException {

        //创建输入流
        FileInputStream fis = new FileInputStream("D:\\input\\test\\bbbbbbb.txt");
        //创建输出流
        Path writePath = new Path("hdfs://bigdata111:9000/plus/bbbbbbb.txt");
        FSDataOutputStream fos = fs.create(writePath);
        //对接流
        try {
            IOUtils.copyBytes(fis, fos, 5 * 1024, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fs.close();
            System.out.println("上传成功");
        }
    }

    @Test
    /**
     * 定位读取,文件第一个快
     * 定位读取,也是下载
     */
    public void readFileblock1() throws IOException {

        //创建输入流
        FSDataInputStream fis = fs.open(new Path("/hadoop-2.8.4.tar.gz"));
        //创建输出流
        FileOutputStream fos = new FileOutputStream(new File("D:\\input\\test\\1"));

        //对接流
        //设buff第一个块1kb
        byte[] buff = new byte[1024];
        for (int i = 0; i < 128 * 1024; i++) {
            fis.read(buff);
            fos.write(buff);

        }
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
        fs.close();

    }

    @Test
    /**
     * 定位读取,文件第二个快
     * 定位读取,也是下载
     */
    public void readFileblock2() throws IOException {
          //创建输入流
        FSDataInputStream fis = fs.open(new Path("/hadoop-2.8.4.tar.gz"));
        //创建输出流
        FileOutputStream fos = new FileOutputStream(new File("D:\\input\\test\\2"));
        //定位偏移量
        fis.seek(128 * 1024*1024);
        //对接流,用copyBytes
        //buffSize:1024是缓存区
        //close:true是关流
        IOUtils.copyBytes(fis,fos,1024,true);
            fis.close();


    }
}

从HDFS中下载到win中的文件可以合并到一起(俩个block的可以)

在win中用追加的方式:type 2 >> 1,然后就可以合并一起了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperBigData~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值