发布于 2016-07-12 08:58:07 | 432 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Hadoop教程,程序狗速度看过来!

Hadoop分布式系统

一个分布式系统基础架构,由Apache基金会所开发。 用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力高速运算和存储。


这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上、从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务