HDFS Java 客户端使用(Windows开发环境)
生活随笔
收集整理的這篇文章主要介紹了
HDFS Java 客户端使用(Windows开发环境)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.加入依賴
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.6.4</version>
</dependency>
2.文件上傳到HDFS
/*** @TODO 上傳文件到HDFS*/@Testpublic void testAddFileToHdfs() throws Exception{// 要上傳的文件所在的本地路徑Path src= new Path("F:\\staday-video.avi");// 要上傳到hdfs的目標路徑Path dst= new Path("/test_dir/");if(!fs.exists(dst))fs.mkdirs(dst);try {fs.copyFromLocalFile(src, dst);fs.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }
3.從HDFS中下載文件到本地
/*** @TODO 從HDFS中下載文件到本地*/@Testpublic void testDownloadFileToLocal() throws Exception{fs.copyToLocalFile(false,//是否刪除原文件new Path("/test_dir/staday-video.avi"),//源路徑new Path("e:/"),//目標路徑true //目標路徑是否本地文件系統);fs.close();}
4.創建目錄、刪除目錄/文件、重命名 目錄/文件
@Testpublic void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException {// 創建目錄fs.mkdirs(new Path("/test_new/a1/b1"));// 刪除文件夾 ,如果是非空文件夾,參數2必須給值truefs.delete(new Path("/aaa"), true);// 重命名文件或文件夾fs.rename(new Path("/test_new"), new Path("/test_n"));}
5.查看目錄信息,只顯示文件
/*** 查看目錄信息,只顯示文件* * @throws IOException* @throws IllegalArgumentException* @throws FileNotFoundException*/@Testpublic void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);while (listFiles.hasNext()) {LocatedFileStatus fileStatus = listFiles.next();System.out.println(fileStatus.getPath().getName());System.out.println(fileStatus.getBlockSize());System.out.println(fileStatus.getPermission());System.out.println(fileStatus.getLen());BlockLocation[] blockLocations = fileStatus.getBlockLocations();for (BlockLocation bl : blockLocations) {System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());String[] hosts = bl.getHosts();for (String host : hosts) {System.out.println(host);}}System.out.println("--------------打印的分割線--------------");}}
6.上述例子的demo工程下載
??hadoop-hdfs-test-master
?
?
?
?
?
總結
以上是生活随笔為你收集整理的HDFS Java 客户端使用(Windows开发环境)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个日文个性签名!
- 下一篇: 鲅鱼多少钱啊?