亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java結合HADOOP集群文件上傳下載

 更新時間:2015年03月25日 14:45:54   投稿:hebedich  
這篇文章主要介紹了java結合HADOOP集群文件上傳下載的方法和示例,非常的實用,這里推薦給大家,希望大家能夠喜歡。

對HDFS上的文件進行上傳和下載是對集群的基本操作,在《HADOOP權威指南》一書中,對文件的上傳和下載都有代碼的實例,但是對如何配置HADOOP客戶端卻是沒有講得很清楚,經(jīng)過長時間的搜索和調試,總結了一下,如何配置使用集群的方法,以及自己測試可用的對集群上的文件進行操作的程序。首先,需要配置對應的環(huán)境變量:

復制代碼 代碼如下:

hadoop_HOME="/home/work/tools/java/hadoop-client/hadoop"
for f in $hadoop_HOME/hadoop-*.jar; do
        hadoop_CLASSPATH=${hadoop_CLASSPATH}:$f
done
for f in $hadoop_HOME/lib/*.jar; do
        hadoop_CLASSPATH=${hadoop_CLASSPATH}:$f
done
hadoopvfs_HOME="/home/work/tools/java/hadoop-client/hadoop-vfs"
for f in $hadoopvfs_HOME/lib/*.jar; do
        hadoop_CLASSPATH=${hadoop_CLASSPATH}:$f
done
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/work/tools/java/hadoop-client/hadoop/lib/native/Linux-amd64-64/

其中LD_LIBRARY_PATH是在調用時需要用到的庫的路徑,hadoop_CLASSPATH則是我們hadoop客戶端里各種jar包
有一點需要注意的是最好不要使用HADOOP_HOME這個變量,這個是一個系統(tǒng)使用的環(huán)境變量,最好不要和它沖突
編譯類的方法:

復制代碼 代碼如下:

javac -classpath $CLASSPATH:$hadoop_CLASSPATH HDFSUtil.java

運行的方法:

復制代碼 代碼如下:

java -classpath $CLASSPATH:$hadoop_CLASSPATH HDFSUtil

但是在實際的使用過程中,會報No Permission之類的錯誤,或者你能保證代碼沒有問題的情況下,在運行的時候也會報一些奇奇怪怪的錯誤
那么問題來了,這是什么鬼?
答案:這是因為沒有配置對應集群的配置文件
因為在《HADOOP權威指南》一書中,弱化了配置的東西,所以在具體使用集群的時候就會出現(xiàn)問題,如何解決呢,這樣子:

復制代碼 代碼如下:

this.conf = new Configuration(false);
conf.addResource("./hadoop-site.xml");
conf.addResource("./hadoop-default.xml");
conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());

為什么會這樣,書上只是很簡單的:

this.conf = new Configuration();
那是因為默認你的集群在本地,所以不需要做配置,但是在實際使用的過程中,各個集群的配置是不同的,所以我們要引入集群的配置
這是非常重要的一點,因為實際使用的過程中我們都是使用的HADOOP的客戶端,而且是已經(jīng)搭好環(huán)境的集群,所以我們需要做好本地的配置
hadoop-site.xml和hadoop-default.xml這兩個文件在所使用的客戶端的conf目錄下,在addResource的時候指定好目錄就行了

將以上所提到的配置,全部配完之后,這個程序才能真正運行起來,所以配置是非常重要的一環(huán)。

以下是對應的工具的代碼,有興趣的看一下吧,使用的是文件流的方式來搞的,這樣子也可以打通FTP和HDFS之間文件的互傳:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.io.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

public class HDFSUtil {
  private String hdfs_node = "";
  private String hdfs_path = "";
  private String file_path = "";
  private String hadoop_site = "";
  private String hadoop_default = "";
  private Configuration conf = null;

  public HDFSUtil(String hdfs_node) {
    this.hdfs_node = hdfs_node;
  }

  public String getHdfsNode() {
    return this.hdfs_node;
  }

  public void setHdfsPath(String hdfs_path){
    this.hdfs_path = hdfs_path;
  }

  public String getHdfsPath(){
    return this.hdfs_path;
  }

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

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

  public void setHadoopSite(String hadoop_site){
    this.hadoop_site = hadoop_site;
  }

  public String getHadoopSite(){
    return this.hadoop_site;
  }

  public void setHadoopDefault(String hadoop_default){
    this.hadoop_default = hadoop_default;
  }

  public String getHadoopDefault(){
    return this.hadoop_default;
  }

  public int setConfigure(boolean flag) {
    if (flag == false){
      if (this.getHadoopSite() == "" || this.getHadoopDefault() == ""){
        return -1;
      }
      else {
        this.conf = new Configuration(false);
        conf.addResource(this.getHadoopDefault());
        conf.addResource(this.getHadoopSite());
        conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
        conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
        return 0;
      }
    }
    this.conf = new Configuration();
    return 0;
  }

  public Configuration getConfigure() {
    return this.conf;
  }

  public int upLoad(String localName, String remoteName) throws FileNotFoundException, IOException {
    InputStream inStream = null;
    FileSystem fs = null;
    try{
      inStream = new BufferedInputStream(new FileInputStream(localName));
      fs = FileSystem.get(URI.create(this.hdfs_node), this.conf);
      OutputStream outStream = fs.create(new Path(remoteName) ,new Progressable() {
        public void progress(){
          System.out.print('.');
        }
      });

      IOUtils.copyBytes(inStream, outStream, 4096, true);
      inStream.close();
      return 0;
    } catch (IOException e){
      inStream.close();
      e.printStackTrace();
      return -1;
    }
  }

  public int upLoad(InputStream inStream, String remoteName) throws FileNotFoundException, IOException {
    FileSystem fs = null;
    try{
      fs = FileSystem.get(URI.create(this.hdfs_node), this.conf);
      OutputStream outStream = fs.create(new Path(remoteName) ,new Progressable() {
        public void progress(){
          System.out.print('.');
        }
      });

      IOUtils.copyBytes(inStream, outStream, 4096, true);
      inStream.close();
      return 0;
    } catch (IOException e){
      inStream.close();
      e.printStackTrace();
      return -1;
    }
  }

  public int donwLoad(String remoteName, String localName, int lines) throws FileNotFoundException, IOException {
    FileOutputStream fos = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    String str = null;
    OutputStreamWriter osw = null;
    BufferedWriter buffw = null;
    PrintWriter pw = null;
    FileSystem fs = null;
    InputStream inStream = null;
    try {
      fs = FileSystem.get(URI.create(this.hdfs_node + remoteName), this.conf);
      inStream = fs.open(new Path(this.hdfs_node + remoteName));
      fos = new FileOutputStream(localName);
      osw = new OutputStreamWriter(fos, "UTF-8");
      buffw = new BufferedWriter(osw);
      pw = new PrintWriter(buffw);
      isr = new InputStreamReader(inStream, "UTF-8");
      br = new BufferedReader(isr);
      while((str = br.readLine()) != null && lines > 0){
        lines--;
        pw.println(str);
      }
    } catch (IOException e){
      throw new IOException("Couldn't write.", e);
    } finally {
      pw.close();
      buffw.close();
      osw.close();
      fos.close();
      inStream.close()
    }
    return 0;
  }

  //main to test
  public static void main(String[] args){
    String hdfspath = null;
    String localname = null;
    String hdfsnode = null;
    int lines = 0;

    if (args.length == 4){
      hdfsnode = args[0];
      hdfspath = args[1];
      localname = args[2];
      lines = Integer.parseInt(args[3]);
    }
    else{
      hdfsnode = "hdfs://nj01-nanling-hdfs.dmop.baidu.com:54310";
      hdfspath = "/app/ps/spider/wdmqa/wangweilong/test/HDFSUtil.java";
      localname = "/home/work/workspace/project/dhc2-0/dhc/base/ftp/papapa";
      lines = 5;
    }
    HDFSUtil hdfsutil = new HDFSUtil(hdfsnode);
    hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfspath);
    hdfsutil.setHadoopSite("./hadoop-site.xml");
    hdfsutil.setHadoopDefault("./hadoop-default.xml");
    hdfsutil.setConfigure(false);
    try {
      hdfsutil.donwLoad(hdfspath, localname, lines);
    } catch (IOException e){
      e.printStackTrace();
    }
  }

如果想要了解FTP上文件的下載,請參考這篇文章:

ftp下載工具

如果想要打通FTP和HDFS文件互傳,只要創(chuàng)建一個類,調用這兩篇文章中的工具的接口就可以搞定,自己寫的代碼,實測有效。

以上就是本文的全部內(nèi)容了,希望能夠對大家熟練掌握java有所幫助。

請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支持!

相關文章

  • idea構建web項目的超級詳細教程

    idea構建web項目的超級詳細教程

    好多朋友在使用IDEA創(chuàng)建項目時,總會碰到一些小問題,下面這篇文章主要給大家介紹了關于idea構建web項目的超級詳細教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • 淺談SSH框架中spring的原理

    淺談SSH框架中spring的原理

    下面小編就為大家?guī)硪黄獪\談SSH框架中spring的原理。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java優(yōu)化for循環(huán)嵌套的高效率方法

    Java優(yōu)化for循環(huán)嵌套的高效率方法

    這篇文章主要介紹了Java優(yōu)化for循環(huán)嵌套的高效率方法,幫助大家更好的提升java程序性能,感興趣的朋友可以了解下
    2020-09-09
  • springmvc開啟異步請求報錯Java code using the Servlet API or

    springmvc開啟異步請求報錯Java code using the Ser

    這篇文章主要為大家介紹了springmvc開啟異步請求報錯Java code using the Servlet API or解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-02-02
  • Servlet連接數(shù)據(jù)庫實現(xiàn)用戶登錄的實現(xiàn)示例

    Servlet連接數(shù)據(jù)庫實現(xiàn)用戶登錄的實現(xiàn)示例

    本文主要介紹了Servlet連接數(shù)據(jù)庫實現(xiàn)用戶登錄的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • Java項目常見工具類詳解

    Java項目常見工具類詳解

    這篇文章主要為大家總結了平時在Java項目中使用的工具類:JWT工具類、MD5工具類、視頻點播工具類、公共常量工具類、日期操作工具類、Http客戶端工具類和獲取IP工具類。需要的可以參考一下
    2021-12-12
  • SpringBoot靜態(tài)資源CSS等修改后再運行無效的解決

    SpringBoot靜態(tài)資源CSS等修改后再運行無效的解決

    這篇文章主要介紹了SpringBoot靜態(tài)資源CSS等修改后再運行無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 淺談Java 三種方式實現(xiàn)接口校驗

    淺談Java 三種方式實現(xiàn)接口校驗

    這篇文章主要介紹了淺談Java 三種方式實現(xiàn)接口校驗,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 關于IDEA 2020使用 mybatis-log-plugin插件的問題

    關于IDEA 2020使用 mybatis-log-plugin插件的問題

    這篇文章主要介紹了關于IDEA 2020使用 mybatis-log-plugin插件的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • jenkins配置詳細指南(附jdk多個版本配置)

    jenkins配置詳細指南(附jdk多個版本配置)

    Jenkins是一款CICD(持續(xù)集成與持續(xù)交付)工具,Jenkins可以幫你在寫完代碼后,一鍵完成開發(fā)過程中的一系列自動化部署的工作,這篇文章主要給大家介紹了關于jenkins配置的相關資料,文中還附jdk多個版本配置指南,需要的朋友可以參考下
    2024-02-02

最新評論