当前位置: 移动技术网 > IT编程>开发语言>Java > hadoop的hdfs文件操作实现上传文件到hdfs

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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;
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网