遠程Java用戶端上傳檔案到HDFS__Java

來源:互聯網
上載者:User

Hadoop叢集環境:

三台機器:namenode0, datanode1, datanode2 作業系統:Ubuntu 11.04 Server version Haddop版本: hadoop-0.20.2-cdh3u1 HBase版本:hbase-0.90.4-cdh3u2 Java版本:jdk-6u29-linux-x64

用戶端機器: 注意點: conf需添加core-site.xml設定檔,且其內容為正確。 許可權的問題:叢集裡的機器是以hadoop賬戶啟動並執行,我本地機器是以lotus賬戶啟動並執行,如果上傳到hdfs://namenode0:54310/usr/目錄下會失敗,拋出Permission denied的異常。因為/usr目錄的owner是hadoop,而上傳到hdfs://namenode0:54310/usr/lotus/目錄就成功,是因為lotus目錄的owner被設定為lotus賬戶了。

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!-- Put site-specific property overrides in this file. --><configuration><property><name>fs.default.name</name><value>hdfs://namenode0:54310</value></property></configuration>

代碼
import java.io.File;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HDFSUpLoad {//HDFSUpLoad srcFilePath, dstFilePathpublic static void main(String[] args) throws Exception{if(args.length < 2){System.out.print("command line: HDFSUpLoad srcFilePath dstFilePath");return;}String srcFilePath = args[0];String dstFilePath = args[1];Configuration conf = new Configuration();conf.addResource(new Path("/home/lotus/work/hadoop-0.20.2-cdh3u1/conf/core-site.xml"));FileSystem hdfs = FileSystem.get(conf);Path src = new Path(srcFilePath);Path dst = new Path(dstFilePath);System.out.print("start upload");long startTime = System.currentTimeMillis();hdfs.copyFromLocalFile(src, dst);long endTime = System.currentTimeMillis();System.out.print("finish upload, cost time: " + String.valueOf(endTime - startTime));}}



效能測試: HDFSUpload /media/PC-Book/DD/win2000.dd hdfs://namenode0:54310/usr/lotus/win2000.dd 上傳一個3.9G的dd檔案,花了428秒,第二次上傳花了377秒,效能問題以後研究。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.