hadoop通过API接口读写文件篇

背景

为了转岗,最近在学习大数据,特此记录一下学习过程与写代码时的一些简单思考。

代码

package com.lineqi.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.yarn.api.records.URL;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsApi{
public static void main(String[] args) {
HdfsApi ha =new HdfsApi();
try {
//ha.downloadFile();
//ha.streamDownloadFile();
//ha.mergeUploadBigFile();
ha.mergeDownloadFile();
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException ie){
ie.printStackTrace();
}finally {
System.out.println(“aaa”);
}
}
1、创建文件夹

public void mkdirs() throws IOException,InterruptedException{
    FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.0.104:8020"),new Configuration(),"root");
    boolean bl = fs.mkdirs(new Path("/aaa/bbb/ccc"));
    System.out.println(bl);
    fs.close();
}

2、上传文件


    public void upLoadFile() throws IOException,InterruptedException {
        FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.0.104:8020" ),new Configuration(),"root");
        fs.copyFromLocalFile(new Path("D:\\pgtable.txt"),new Path("/aaa/bbb/ccc"));
        fs.close();
    }

3、下载文件

public void downloadFile() throws InterruptedException,IOException {
    FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.0.104:8020"),new Configuration(),"root");
    fs.copyToLocalFile(false,new Path("/aaa/bbb/ccc/pgtable.txt"),new Path("E:\\tt.txt"),true);
    fs.close();
}

4、以流方式下载文件

public void streamDownloadFile() throws IOException,InterruptedException {
    FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.0.104:8020"),new Configuration(),"root");
    FSDataInputStream fsdi = fs.open(new Path("/test/hadoop-2.10.1.tar.gz"),1024);
    FileOutputStream fsdo = new FileOutputStream("E:\\hadoop.tar.gz");
    IOUtils.copyBytes(fsdi,fsdo,1024);
    IOUtils.closeStream(fsdi);
    IOUtils.closeStream(fsdo);
    fs.close();
}

5、合并多个小文件上传

public void mergeUploadBigFile() throws IOException,InterruptedException {
    // 1、获取FileSystem
    FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.0.104:8020"),new Configuration(),"root");
    // 2、获取HDFS输出流
    FSDataOutputStream fsos = fs.create(new Path("/test/bigfile.txt"));
    // 3、获取本地文件系统
    LocalFileSystem lfs = FileSystem.getLocal(new Configuration());
    // 4、获取本地文件夹下所以文件
    FileStatus[] filestatus = lfs.listStatus(new Path("d:\\test"));

    // 5、遍历每个文件,获取每个文件的输入流
    for(FileStatus files : filestatus){
        FSDataInputStream fsdi = lfs.open(files.getPath());
        // 6、将每个文件内容复制到大文件中
        IOUtils.copyBytes(fsdi,fsos,1024);
        IOUtils.closeStream(fsdi);

    }
    // 7、关闭流
    IOUtils.closeStream(fsos);
    lfs.close();
    fs.close();
}

6、将多个小文件合并下载

  public void mergeDownloadFile() throws IOException, InterruptedException {
        //1、获取FileSystem文件系统
        FileSystem fs = FileSystem.get(URI.create("hdfs://192.168.0.104:8020"),new Configuration(),"root");
        //2、获取FileSystem文件目录
        RemoteIterator<LocatedFileStatus> remoteIterator= fs.listFiles(new Path("/aaa/bbb/ccc/"),true);
        //3、创建文件输出流
        FileOutputStream outputstream = new FileOutputStream("e:\\test\\ddd.txt");

        while(remoteIterator.hasNext()){
            //4、获取每个文件并打开文件
            LocatedFileStatus filestatus=remoteIterator.next();
            String filename="/aaa/bbb/ccc/"+filestatus.getPath().getName();

            FSDataInputStream inputstream = fs.open(new Path(filename));
            //5、执行文件内容拷贝
            IOUtils.copyBytes(inputstream,
                    outputstream,
                    1024);
            IOUtils.closeStream(inputstream);

        }
        //6、关闭输出流
        IOUtils.closeStream(outputstream);
        //7、关闭文件
        fs.close();

    }

}

思考方式

1、大文件下载时,中断了如何能从中断时的那个点开始接着下载呢?(即是否支持断点续传功能)
2、只处理一个目录下的部分文件,比如以mysql开头或以mysql结尾,如何实现呢(昨天晚上试一下没有成功)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值