如何用java程式把本地檔案拷貝到hdfs上並顯示進度

來源:互聯網
上載者:User

把程式打成jar包放到Linux上

轉到目錄下執行命令 hadoop jar mapreducer.jar /home/clq/export/java/count.jar  hdfs://ubuntu:9000/out06/count/

上面一個是本地檔案,一個是上傳hdfs位置

成功後出現:列印出來,你所要列印的字元。

package com.clq.hdfs;         import java.io.BufferedInputStream;  import java.io.FileInputStream;  import java.io.IOException;  import java.io.InputStream;  import java.net.URI;  import org.apache.hadoop.conf.Configuration;  import org.apache.hadoop.fs.FSDataOutputStream;  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 FileCopyWithProgress {  //********************************  //把本地的一個檔案拷貝到hdfs上  //********************************      public static void main(String[] args) throws IOException {          String localSrc = args[0];          String dst = args[1];          InputStream in = new BufferedInputStream(new FileInputStream(localSrc));          Configuration conf = new Configuration();          FileSystem fs = FileSystem.get(URI.create(dst), conf);          FSDataOutputStream out = fs.create(new Path(dst), new Progressable() {              @Override            public void progress() {                  System.out.print(".");              }          });          IOUtils.copyBytes(in, out, conf, true);      }         }

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

可能出現異常:

Exception in thread "main" org.apache.hadoop.ipc.RemoteException: java.io.IOException: Cannot create /out06; already exists as a directory

at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFileInternal(FSNamesystem.java:1569)

at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFile(FSNamesystem.java:1527)

at org.apache.hadoop.hdfs.server.namenode.NameNode.create(NameNode.java:710)

at org.apache.hadoop.hdfs.server.namenode.NameNode.create(NameNode.java:689)

at sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:587)

at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1432)

at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1428)

at java.security.AccessController.doPrivileged(Native Method)

at javax.security.auth.Subject.doAs(Subject.java:415)

說明你這個路徑在hdfs上已經存在,換一個即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.