资讯专栏INFORMATION COLUMN

用java实现将ftp和http的文件直接传送到hdfs

sixgo / 1771人阅读

摘要:之前实现了使用流来讲和的文件下载到本地,也实现了将本地文件上传到上,那现在就可以做到将和的文件转移到上了,而不用先将和的文件拷贝到本地再上传到上了。

之前实现了使用流来讲http和ftp的文件下载到本地,也实现了将本地文件上传到hdfs上,那现在就可以做到将
ftp和http的文件转移到hdfs上了,而不用先将ftp和http的文件拷贝到本地再上传到hdfs上了。其实这个东西的原理
很简单,就是使用流,将ftp或http的文件读入到流中,然后将流中的内容传送到hdfs上,这样子就不用让数据存到
本地的硬盘上了,只是让内存来完成这个转移的过程,希望这个工具,能够帮到有这样需求的同学~
这里先附上之前的几个工具的链接:

http工具
ftp工具
链接描述

代码如下:

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;


public class FileTrans {
    private String head = "";
    private String hostname = "";
    private String FilePath = "";
    private String hdfsFilePath = "";
    private HDFSUtil hdfsutil = null;
    private FtpClient ftp;
    private HttpUtil http;

    public void setFilePath(String FilePath){
        this.FilePath = FilePath;
    }

    public String getFilePath(String FilePath){
        return this.FilePath;
    }

    public void sethdfsFilePath(String hdfsFilePath){
        this.hdfsFilePath = hdfsFilePath;
    }

    public String gethdfsFilePath(String hdfsFilePath){
        return this.hdfsFilePath;
    }

    public void setHostName(String hostname){
        this.hostname = hostname;
    }

    public String getHostName(){
        return this.hostname;
    }

    public void setHead(String head){
        this.head = head;
    }

    public String getHead(){
        return this.head;
    }

    public FileTrans(String head, String hostname, String filepath, String hdfsnode,String hdfsFilepath){
        this.head = head;
        this.hostname = hostname;
        this.FilePath = filepath;
        this.hdfsFilePath = hdfsFilepath;
        if (head.equals("ftp") && hostname != ""){
            this.ftp = new FtpClient(this.hostname);
        }
        if ((head.equals("http") || head .equals("https")) && hostname != ""){
            String httpurl = head + "://" + hostname + "/" + filepath;
            this.http = new HttpUtil(httpurl);
        }
        if (hdfsnode != ""){
            this.hdfsutil = new HDFSUtil(hdfsnode);
        }
        this.hdfsutil.setHdfsPath(this.hdfsFilePath);
        this.hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfsutil.getHdfsPath());
        this.hdfsutil.setHadoopSite("./hadoop-site.xml");
        this.hdfsutil.setHadoopDefault("./hadoop-default.xml");
        this.hdfsutil.setConfigure(false);
    }

    public static void main(String[] args) throws IOException{
        String head = "";
        String hostname = "";
        String filepath = "";
        String hdfsfilepath = "";
        String hdfsnode = "";
        String localpath = "";
        InputStream inStream = null;
        int samplelines = 0;
        try{
            head = args[0];                  //远端服务器类型,http还是ftp
            hostname = args[1];              //远端服务器hostname
            filepath = args[2];              //远端文件路径
            hdfsnode = args[3];              //hdfs的机器名,不带hdfs开头
            hdfsfilepath = args[4];          //hdfs的文件路径
            localpath = args[5];             //如果需要在本地保存一份的话,输入本地的路径,不保存,传入空格或者samplelines传入0
            samplelines = Integer.parseInt(args[6]);  //保存在本地的话,保存前N行,如果不保存,填0
        }catch (Exception e){
            System.out.println("[FileTrans]:input args error!");
            e.printStackTrace();
        }
        FileTrans filetrans = new FileTrans(head, hostname, filepath, hdfsnode,hdfsfilepath);
        if (filetrans == null){
            System.out.println("filetrans null");
            return;
        }
        if (filetrans.ftp == null && head.equals("ftp")){
            System.out.println("filetrans ftp null");
            return;
        }
        if (filetrans.http == null && (head.equals("http") || head.equals("https"))){
            System.out.println("filetrans ftp null");
            return;
        }
        try{
            if (head.equals("ftp")){
                inStream = filetrans.ftp.getStream(filepath);
                if (samplelines > 0){
                    filetrans.ftp.writeStream(inStream, localpath, samplelines);
                }
            }
            else{
                inStream = filetrans.http.getStream(head + "://" + hostname + "/" + filepath);
                if (samplelines > 0){
                    filetrans.http.downLoad(head + "://" + hostname + "/" + filepath, localpath, samplelines);
                }
            }
            filetrans.hdfsutil.upLoad(inStream, filetrans.hdfsutil.getFilePath()); 
            if (head == "ftp"){
                filetrans.ftp.disconnect();
            }
        }catch (IOException e){
            System.out.println("[FileTrans]: file trans failed!");
            e.printStackTrace();
        }
        System.out.println("[FileTrans]: file trans success!");
    }

}

编译有问题的话,在hadoop工具的那篇文章中有提到,可以参考
注:最好将其他三个工具的文件放在同一个目录下,如果不放在一起,那么请自行引用

这个工具既可以将ftp或者http转移到hdfs,也能将前N行保存到本地,进行分析

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64285.html

相关文章

  • HADOOP集群文件上传下载

    摘要:对上的文件进行上传和下载是对集群的基本操作,在权威指南一书中,对文件的上传和下载都有代码的实例,但是对如何配置客户端却是没有讲得很清楚,经过长时间的搜索和调试,总结了一下,如何配置使用集群的方法,以及自己测试可用的对集群上的文件进行操作的程 对HDFS上的文件进行上传和下载是对集群的基本操作,在《HADOOP权威指南》一书中,对文件的上传和下载都有代码的实例,但是对如何配置HADOOP...

    nevermind 评论0 收藏0
  • 什么是大数据

    一、什么是大数据进入本世纪以来,尤其是2010年之后,随着互联网特别是移动互联网的发展,数据的增长呈爆炸趋势,已经很难估计全世界的电子设备中存储的数据到底有多少,描述数据系统的数据量的计量单位从MB(1MB大约等于一百万字节)、GB(1024MB)、TB(1024GB),一直向上攀升,目前,PB(等于1024TB)级的数据系统已经很常见,随着移动个人数据、社交网站、科学计算、证券交易、网站日志、传...

    learn_shifeng 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<