9.定位读取HDFS文件

1.文件从hdfs下载

下面来回顾回顾,出现的尴尬问题:

这个问题,出现的原因就是从hdfs下载到本地,不能后面直接跟一个目录,必须记得写一个名。

@Test
    public void getFileHdfs1() throws URISyntaxException, IOException, InterruptedException {

    Configuration conf = new Configuration();

    FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root");

    FSDataInputStream fis = fs.open(new Path("/123"));

    FileOutputStream fos = new FileOutputStream(new File("D:\\input\\test\\aa.txt"));

    IOUtils.copyBytes(fis,fos,1024*5,true);


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

}
 

2.文件从本地上传到HDFS

@Test
    public void putFileHdfs1() throws URISyntaxException, IOException, InterruptedException {

    Configuration conf = new Configuration();

    FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root");

    FileInputStream fis=new FileInputStream(new File("D:\\input\\test\\student.txt"));

    Path path=new Path("/student.txt");

    FSDataOutputStream fos=fs.create(path);

    IOUtils.copyBytes(fis,fos,4*1024,false);

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

    fs.close();


}

3.文件的定位读取三次

/**
 * hdfs文件的定位读取第一个block块到windows中
 *
 * */
@Test
    public void readFileHdfs1() throws URISyntaxException, IOException, InterruptedException {

    Configuration conf = new Configuration();

    FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root");

    //从hdfs创建输入流
    FSDataInputStream fis=fs.open(new Path("/Python素材.rar"));

    //创建输出流
    FileOutputStream fos=new FileOutputStream(new File("D:\\input\\test\\ss1.txt"));

    //对接流
    byte[] buff =  new byte[1024];
    //1kb,然后是循环1024*128=128M
    for (int i=0;i<128*1024;i++){
        fis.read(buff);
        fos.write(buff);
    }
    IOUtils.closeStream(fis);
    IOUtils.closeStream(fos);




}
/**
* hdfs文件的定位读取第二个block块到windows中
*/
    @Test
    public void readFileHdfs2() throws URISyntaxException, IOException, InterruptedException {

        Configuration conf = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root");

        //从hdfs创建输入流
        FSDataInputStream fis=fs.open(new Path("/Python素材.rar"));

        //创建输出流
        FileOutputStream fos=new FileOutputStream(new File("D:\\input\\test\\ss2.txt"));

        //对接流
        byte[] buff =  new byte[1024];

        //定义偏移量
        fis.seek(128*1024*1024);

        //1kb,然后是循环1024*128=128M
        for (int i=0;i<128*1024;i++){
            fis.read(buff);
            fos.write(buff);
        }
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
        fis.close();
    }

/**
        * hdfs文件的定位读取第三个块到windows中
 *
         * */
    @Test
    public void readFileHdfs3() throws URISyntaxException, IOException, InterruptedException {

        Configuration conf = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata111:9000"),conf,"root");

        //从hdfs创建输入流
        FSDataInputStream fis=fs.open(new Path("/Python素材.rar"));

        //创建输出流
        FileOutputStream fos=new FileOutputStream(new File("D:\\input\\test\\ss3.txt"));


        //对接流
        byte[] buff =  new byte[1024];
       //定义偏移量
        fis.seek(128*1024*1024*2);

        IOUtils.copyBytes(fis, fos, 1024);

        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);

         fs.close();


    }

 结果:

 在cmd中文件合并:

最终改成压缩包后,在解压,会变成原来的模样。

                                                                                                                                                                     保持学习,保持饥饿

                                                                                                                                                       ————Jackson_MVP 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SuperBigData~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值