欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hadoop的hdfs文件操作实现上传文件到hdfs

程序员文章站 2024-03-31 12:03:52
hdfs文件操作操作示例,包括上传文件到hdfs上、从hdfs上下载文件和删除hdfs上的文件,大家参考使用吧复制代码 代码如下:import org.apache.had...

hdfs文件操作操作示例,包括上传文件到hdfs上、从hdfs上下载文件和删除hdfs上的文件,大家参考使用吧

复制代码 代码如下:

import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.fs.*;

import java.io.file;
import java.io.ioexception;
public class hadoopfile {
    private configuration conf =null;

    public hadoopfile(){
        conf =new configuration();
        conf.addresource(new path("/hadoop/etc/hadoop/core-site.xml"));
    }

    public hadoopfile(configuration conf){
        this.conf =conf;
    }

    public boolean sendfile(string path,string localfile){
        file file=new file(localfile);
        if (!file.isfile()) {
            system.out.println(file.getname());
            return false;
        }
        try {
            filesystem localfs =filesystem.getlocal(conf);
            filesystem hadoopfs =filesystem.get(conf);
            path hadpath=new path(path);

            fsdataoutputstream fsout=hadoopfs.create(new path(path+"/"+file.getname()));
            fsdatainputstream fsin=localfs.open(new path(localfile));
            byte[] buf =new byte[1024];
            int readbytes=0;
            while ((readbytes=fsin.read(buf))>0){
                fsout.write(buf,0,readbytes);
            }
            fsin.close();
            fsout.close();

            filestatus[] hadfiles= hadoopfs.liststatus(hadpath);
            for(filestatus fs :hadfiles){
                system.out.println(fs.tostring());
            }
            return true;
        } catch (ioexception e) {
            e.printstacktrace();
        }
        return false;
    }

    public boolean delfile(string hadfile){
        try {

            filesystem hadoopfs =filesystem.get(conf);
            path hadpath=new path(hadfile);
            path p=hadpath.getparent();
            boolean rtnval= hadoopfs.delete(hadpath, true);

            filestatus[] hadfiles= hadoopfs.liststatus(p);
            for(filestatus fs :hadfiles){
                system.out.println(fs.tostring());
            }
            return rtnval;
        } catch (ioexception e) {
            e.printstacktrace();
        }
        return false;
    }


    public boolean downloadfile(string hadfile,string localpath){

        try {
            filesystem localfs =filesystem.getlocal(conf);
            filesystem hadoopfs =filesystem.get(conf);
            path hadpath=new path(hadfile);

            fsdataoutputstream fsout=localfs.create(new path(localpath+"/"+hadpath.getname()));
            fsdatainputstream fsin=hadoopfs.open(hadpath);
            byte[] buf =new byte[1024];
            int readbytes=0;
            while ((readbytes=fsin.read(buf))>0){
                fsout.write(buf,0,readbytes);
            }
            fsin.close();
            fsout.close();

            return true;
        } catch (ioexception e) {
            e.printstacktrace();
        }
        return false;
    }
}