Java操作HDFS(Linux) 學習篇(二)
阿新 • • 發佈:2018-11-05
java操作Linux系統上的HDFS檔案系統
一、首先:在Linux上搭建HDFS偽分散式環境,啟動命令 ------>sh start-all.sh,顯示啟動成功介面如下:
二 、win 上Java開發環境使用的是IDEA ,Java遠端操作HDFS,在pom中用到的依賴如下(因為hadoop用的是2.7.1的版本所用這裡我也用的是2.7.1):
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency>
三、新建一個hdfstest類
實現檔案上傳,程式碼如下:
public class Hdfs { static FileSystem fs =null; public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://lxp:9000/"); try { fs = FileSystem.get(new URI("hdfs://lxp:9000/"),conf,"root"); } catch (InterruptedException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } try { upload();//檔案上傳 /* download();//檔案下載*/ /* mkdir();//建立資料夾*/ /* rm();//刪除資料夾或者檔案*/ /* listFiles();//檢視檔案資訊*/ } catch (Exception e) { e.printStackTrace(); } } /*上傳檔案*/ public static void upload() throws Exception{ fs.copyFromLocalFile(new Path("D:/lxp.txt"), new Path("hdfs://lxp:9000/ll/xx/pp/lxppppp.txt")); } }
解析:
1. Configuration獲取Hadoop配置檔案的資訊。
2. conf.set("fs.defaultFS","hdfs://lxp:9000/")可以對hadoop的配置檔案("core-site.xml")資訊手動修改,會覆蓋掉從配置檔案中讀取的資訊;
3. FileSystem.get(new URI("hdfs://lxp:9000/"),conf,"root"); root使用者為登入Linux系統的登入使用者;
實現檔案下載,程式碼如下:
/*下載檔案*/ public static void download() throws IOException { fs.copyToLocalFile(new Path("hdfs://lxp:9000/aa/lxppppp.txt"),new Path("D://lxp2.txt")); }
實現檔案檢視,程式碼如下:
/*檢視檔案資訊*/
public static void listFiles() throws IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status : listStatus){
String fileName= status.getPath().getName();
System.out.println(fileName);
}
}
解析:
1. listStatus可以列出檔案和資料夾的資訊
2. listFiles只可以獲取檔案
實現資料夾建立,程式碼如下:
/*建立資料夾*/
public static void mkdir() throws IOException {
fs.mkdirs(new Path("/ll/xx/pp"));
}
解析:
1. fs.mkdirs(new Path("/ll/xx/pp")); 如果父目錄不存在,也會一起建立。
實現檔案或者資料夾的刪除,程式碼如下:
/*刪除檔案或者資料夾*/
public static void rm() throws IOException {
fs.delete(new Path("/aa"),true);
}
注意:
一、 fs = FileSystem.get(new URI("hdfs://lxp:9000/"),conf,"root"); 要與hadoop配置檔案(“”core-site.xml
“”)中的主機名相同(使用的ssh遠端連線工具,沒敲命令直接打開了,偷懶了。。。。)
二、如下錯誤(不認識主機名):
解決辦法:
1.進入到C盤路徑:C:\Windows\System32\drivers\etc
2.在hosts檔案末尾新增如下資訊:
192.168.1.220 master
192.168.1.225 no1
192.168.1.226 no2
192.168.1.227 no3
三、在這個問題上浪費的時間比較長,所以需要特別注意!!!關閉Linux的防火牆!不然會一直報連線超時。。。