1. 程式人生 > >一臉懵逼學習hadoop之HDFS的java客戶端編寫

一臉懵逼學習hadoop之HDFS的java客戶端編寫

txt 維護 刪除文件 trac 實例 for nod delete reat

1:eclipse創建一個項目,然後導入對應的jar包:

鼠標右擊項目,點擊properties或者alt+enter快捷鍵--->java build path--->libraries--->add library--->user library--->next--->user libraries--->new--->hdfsLib(根據自己的需要填寫)---》add external jars(添加自己的需求包):

2:開始添加自己的需求包,路徑如

  hadoop-2.4.1\share\hadoop\hdfs的hadoop-hdfs-2.4.1.jar

和hadoop-2.4.1\share\hadoop\hdfs\lib下面的全部包;

  hadoop-2.4.1\share\hadoop\common的hadoop-common-2.4.1.jar和hadoop-2.4.1\share\hadoop\common\lib下面的全部包;

  1 package com.master01;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.IOException;
  5 import java.net.URISyntaxException;
  6 
  7
import org.apache.commons.io.IOUtils; 8 import org.apache.hadoop.conf.Configuration; 9 import org.apache.hadoop.fs.FSDataOutputStream; 10 import org.apache.hadoop.fs.FileStatus; 11 import org.apache.hadoop.fs.FileSystem; 12 import org.apache.hadoop.fs.LocatedFileStatus; 13 import org.apache.hadoop.fs.Path;
14 import org.apache.hadoop.fs.RemoteIterator; 15 16 public class HdfsTest { 17 18 19 //public FileSystem fs = null; 20 /* 21 @Before 22 public void init() throws IOException, InterruptedException, URISyntaxException{ 23 //讀配置文件 24 Configuration conf = new Configuration(); 25 //這裏直接拷貝配置或者直接設置值 26 conf.set("fs.defaultFS", "hdfs://master:9000/"); 27 28 //獲取配置文件裏面的內容 29 fs = FileSystem.get(conf); 30 //fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root"); 31 } 32 */ 33 34 35 /** 36 * 上傳文件 37 * @throws IOException 38 */ 39 public static void upload() throws IOException{ 40 //讀配置文件 41 //讀取classpath下的core-site.xml配置文件,並且解析其的內容,封裝到conf的對象中; 42 Configuration conf = new Configuration(); 43 //這裏直接拷貝配置或者直接設置值 44 //也可以在代碼中對conf的配置信息進行手動設置,會覆蓋配置文件中的配置信息 45 conf.set("fs.defaultFS", "hdfs://master:9000"); 46 47 //獲取配置文件裏面的內容 48 //根據配置信息,去獲取一個具體文件系統的客戶端操作實例對象 49 FileSystem fs = FileSystem.get(conf); 50 //本地文件是輸入流,hdfs是輸出流 51 52 //先搞出路徑 53 Path src = new Path("hdfs://master:9000/aa/test.txt"); 54 //搞出輸出流,即向hdfs上面寫內容 55 FSDataOutputStream create = fs.create(src); 56 57 //輸入流就是讀,本地文件,輸入流 58 FileInputStream fileInputStream = new FileInputStream("d:/test.txt"); 59 60 //將文件fileInputStream到create即完成上傳到hdfs 61 IOUtils.copy(fileInputStream, create); 62 } 63 64 65 //最快的上傳文件的方法 66 public void upload02() throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException{ 67 //讀配置文件 68 Configuration conf = new Configuration(); 69 //這裏直接拷貝配置或者直接設置值 70 conf.set("fs.defaultFS", "hdfs://master:9000"); 71 72 //獲取配置文件裏面的內容 73 FileSystem fs = FileSystem.get(conf); 74 //FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root"); 75 fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("hdfs://master:9000/aa/test.txt")); 76 } 77 78 79 /** 80 * 下載文件 81 * @throws IOException 82 * @throws IllegalArgumentException 83 */ 84 public void download02() throws IllegalArgumentException, IOException{ 85 //去配置文件 86 Configuration conf = new Configuration(); 87 conf.set("fs.defaultFS", "hdfs://master:9000"); 88 89 //獲取配置文件裏面的內容 90 FileSystem fs = FileSystem.get(conf); 91 fs.copyToLocalFile(new Path("hdfs://master:9000/aa/test.txt"), new Path("d:/test2.txt")); 92 93 } 94 95 /*** 96 * 創建文件夾的方法 97 * @throws IOException 98 */ 99 public void mkdir02() throws IOException{ 100 //主配置文件 101 Configuration conf = new Configuration(); 102 //設置配置文件的值 103 conf.set("fs.defaultFS", "hdfs://master:9000"); 104 //獲取配置文件裏面的內容 105 FileSystem fs = FileSystem.get(conf); 106 107 //文件夾的創建 108 fs.mkdirs(new Path("hdfs://master:9000/aaa/bbb/ccc")); 109 } 110 111 112 /** 113 * 刪除文件 114 * @throws IOException 115 */ 116 public void remove02() throws IOException{ 117 //主配置文件 118 Configuration conf = new Configuration(); 119 //設置值 120 conf.set("fs.defaultFS", "hdfs://master:9000"); 121 //獲取配置文件裏面的內容 122 FileSystem fs = FileSystem.get(conf); 123 124 //執行刪除操作 125 fs.delete(new Path("hdfs://master:9000/aaa/bbb/ccc"), true); 126 } 127 128 /** 129 * 文件的移動 130 * @throws IOException 131 */ 132 public void move() throws IOException{ 133 //主配置文件 134 Configuration conf = new Configuration(); 135 //設置值 136 conf.set("fs.defaultFS", "hdfs://master:9000"); 137 //獲取配置文件裏面的內容 138 FileSystem fs = FileSystem.get(conf); 139 140 //移動操作 141 fs.rename(new Path("hdfs://master:9000/aa/test.txt"), new Path("hdfs://master:9000/aaa/bbb")); 142 } 143 144 /*** 145 * 查看文件的信息 146 * @throws IOException 147 */ 148 public void listFiles() throws IOException{ 149 //主配置文件 150 Configuration conf = new Configuration(); 151 //設置值 152 conf.set("fs.defaultFS", "hdfs://master:9000"); 153 //獲取配置文件裏面的內容 154 FileSystem fs = FileSystem.get(conf); 155 156 //查看的是文件,不是文件夾 157 //listFiles列出的是文件信息,而且提供遞歸遍歷 158 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://master:9000/"), true); 159 //叠代輸出信息 160 while(listFiles.hasNext()){ 161 LocatedFileStatus file = listFiles.next(); 162 //文件路徑 163 Path path = file.getPath(); 164 System.out.println(path.getName()); 165 } 166 167 System.out.println("============================================="); 168 //listStatus列出文件和文件夾的信息,但是不提供自帶的遞歸遍歷 169 FileStatus[] listStatus = fs.listStatus(new Path("hdfs://master:9000/")); 170 /*for(int i = 0 ; i<listStatus.length; i++){ 171 System.out.println(listStatus[i]); 172 }*/ 173 for(FileStatus fileStatus : listStatus){ 174 //根據獲取的路徑獲取文件夾的名稱 175 Path path = fileStatus.getPath(); 176 System.out.println(path.getName()); 177 } 178 179 } 180 181 public static void main(String[] args) { 182 HdfsTest hdfsTest = new HdfsTest(); 183 try { 184 //上傳文件的調用 185 //hdfsTest.upload02(); 186 187 //下載文件的調用 188 //hdfsTest.download02(); 189 190 //文件夾的創建 191 //hdfsTest.mkdir02(); 192 193 //刪除操作 194 //hdfsTest.remove02(); 195 196 //移動文件的操作 197 //hdfsTest.move(); 198 199 //查看文件信息 200 hdfsTest.listFiles(); 201 } catch (Exception e) { 202 e.printStackTrace(); 203 } 204 } 205 206 }

3:NameNode的職責

(1):維護元數據的信息;

(2):維護hdfs的目錄樹;

(3):響應客戶端的請求;

一臉懵逼學習hadoop之HDFS的java客戶端編寫