激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - java實(shí)現(xiàn)將ftp和http的文件直接傳送到hdfs

java實(shí)現(xiàn)將ftp和http的文件直接傳送到hdfs

2019-12-13 10:51hebedich JAVA教程

前面幾篇文章,我們已經(jīng)做了很好的鋪墊了,幾個(gè)要用到的工具我們都做了出來(lái),本文就是將他們集合起來(lái),說(shuō)下具體的用法,小伙伴們可以參考下。

之前實(shí)現(xiàn)了使用流來(lái)講http和ftp的文件下載到本地,也實(shí)現(xiàn)了將本地文件上傳到hdfs上,那現(xiàn)在就可以做到將
ftp和http的文件轉(zhuǎn)移到hdfs上了,而不用先將ftp和http的文件拷貝到本地再上傳到hdfs上了。其實(shí)這個(gè)東西的原理
很簡(jiǎn)單,就是使用流,將ftp或http的文件讀入到流中,然后將流中的內(nèi)容傳送到hdfs上,這樣子就不用讓數(shù)據(jù)存到
本地的硬盤(pán)上了,只是讓內(nèi)存來(lái)完成這個(gè)轉(zhuǎn)移的過(guò)程,希望這個(gè)工具,能夠幫到有這樣需求的同學(xué)~
這里先附上之前的幾個(gè)工具的鏈接:

http工具
ftp工具
鏈接描述

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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];         //遠(yuǎn)端服務(wù)器類(lèi)型,http還是ftp
      hostname = args[1];       //遠(yuǎn)端服務(wù)器hostname
      filepath = args[2];       //遠(yuǎn)端文件路徑
      hdfsnode = args[3];       //hdfs的機(jī)器名,不帶hdfs開(kāi)頭
      hdfsfilepath = args[4];     //hdfs的文件路徑
      localpath = args[5];       //如果需要在本地保存一份的話(huà),輸入本地的路徑,不保存,傳入空格或者samplelines傳入0
      samplelines = Integer.parseInt(args[6]); //保存在本地的話(huà),保存前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!");
  }
 
}

編譯有問(wèn)題的話(huà),在hadoop工具的那篇文章中有提到,可以參考
注:最好將其他三個(gè)工具的文件放在同一個(gè)目錄下,如果不放在一起,那么請(qǐng)自行引用

這個(gè)工具既可以將ftp或者h(yuǎn)ttp轉(zhuǎn)移到hdfs,也能將前N行保存到本地,進(jìn)行分析

以上就是本文所述的全部?jī)?nèi)容了,希望能夠?qū)Υ蠹覍W(xué)習(xí)java有所幫助。

請(qǐng)您花一點(diǎn)時(shí)間將文章分享給您的朋友或者留下評(píng)論。我們將會(huì)由衷感謝您的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中国女人内谢69xxxx天美 | 伊人yinren22综合网色 | 午夜视频在线看 | 手机视频在线播放 | 国产精品久久久久网站 | 黄色av网站免费 | 激情大乳女做爰办公室韩国 | 久久精品日产高清版的功能介绍 | 91短视频在线视频 | 午夜视频在线看 | 欧美zoofilia杂交videos | 国产成人精品区一区二区不卡 | 操碰网 | 国产在线播放91 | 亚洲欧美在线视频免费 | 黄色av电影在线 | 欧美国产日韩在线观看成人 | 毛片大全在线观看 | 午夜视频国产 | 久久福利小视频 | 亚洲欧美日韩精品久久亚洲区 | 午夜视频观看 | 国产精品一区二区三区在线 | 国产精品久久久久久久不卡 | 日本不卡一二三区 | 精品无码一区在线观看 | 少妇一级淫片免费放播放 | 久久蜜桃香蕉精品一区二区三区 | 亚洲精品一区二区三区大胸 | 免费黄色欧美视频 | 五月j香国内婷婷 | 国内精品免费一区二区2001 | 欧美黄一区 | 亚洲3p激情在线观看 | 久草视频中文 | asian裸体佳人pics | 久草在线视频免费播放 | 羞羞视频免费网站日本动漫 | 国产亚洲精品久久午夜玫瑰园 | 黄片毛片一级 | 久久国产成人精品国产成人亚洲 |