覺得有幫助的,請多多支援部落客,點贊關注哦~
文章目錄
- HDFS用戶端操作總結
-
- 一、基于IDEA程式設計的準備
-
- 1、環境變量的配置
-
- 1.1、配置HADOOP_HOME
- 1.2、配置PATH
- 2、基于IDEA建立maven 的jar工程
- 3、導入hadoop/hdfs相關依賴
- 4、配置項目的日志(log4j的簡單配置)
- 二、相關類及常用API了解
-
- 1、個人學習方法
- 2、Configuration
- 3、FileSystem
- 4、IO操作
- 三、程式設計代碼實作操作
-
- 1、HDFS編寫一個工具類友善測試
- 2、HDFS實作 -ls 清單檢視
- 3、HDFS判斷是檔案還是目錄
- 4、HDFS實作-mkdir建立目錄
- 5、HDFS删除檔案夾
- 6、HDFS實作本地檔案上傳到叢集
-
- 6.1、基于shell cli指令
- 6.2、基于windows的FS的API
- 6.3、基于Linux測試
- 6.4、基于IO操作
- 6.5、使用提取工具類整體操作
- 7、HDFS檢測Configuration的配置參數的優先級
-
- 7.1、代碼權限高于叢集配置測試
- 7.2、用戶端資源檔案權限高于叢集配置測試
- 7.3、代碼高于用戶端資源檔案高于叢集配置測試
- 8、HDFS實作 -cp 複制
- 9、HDFS實作下載下傳叢集檔案到本地
-
- 9.1、基于shell cli指令
- 9.2、基于FSAPI
- 9.3、基于IO操作
- 9.4、使用提取工具類整合
- 10、HDFS檔案名修改 -mv
- 11、HDFS檢視檔案詳細資訊
HDFS用戶端操作總結
一、基于IDEA程式設計的準備
1、環境變量的配置
1.1、配置HADOOP_HOME
根據自己電腦的作業系統拷貝對應的編譯後的 hadoop jar 包到非中文路徑
1.2、配置PATH
2、基于IDEA建立maven 的jar工程
不多說了,随便建立一下吧。。
這裡多說一點,因為idea跟eclipse不同,不支援一個工作空間,同時操作多個項目,是以可以先建立一個項目project,然後删掉工程下的src目錄,這樣就相當于建立了一個父工程。然後,再在該project也就是父工程下建立Module,也就是新的工作子產品。同一個項目下可以建立多個項目子產品,這樣就實作了同一個工作界面下,存放多個子項目。
這是一點比較實用的經驗。
有些小白剛開始實用idea的時候,項目目錄就能把自己搞暈。。
3、導入hadoop/hdfs相關依賴
<!--引入資源包依賴-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--引入hdfs基本依賴-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.7</version>
</dependency>
<!--引入日志的依賴-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>
4、配置項目的日志(log4j的簡單配置)
網上有好多關于log4j的詳細配置,我這裡簡單的配置一下,目的是執行的時候不出現warn警告。
log4j.rootLogger=INFO,console,file
# 配置輸出到控制台
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
# 配置輸出日志到日志檔案
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=target/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n
二、相關類及常用API了解
1、個人學習方法
首先先說一下學習方法。
個人覺得,這一塊的用戶端操作,與之前的java程式設計學習一樣。
當你學習到了一個新的類包的時候,首先要了解一下大概有哪些對象,主要是幹什麼的,就像下邊我總結的幾個常用的一樣,然後管他三七二十一,先建立對象點一下再說,當你不知道這個類這個對象怎麼用的時候,你
.
點一下對象,方法就自然而然出來,大部分都是可以看得懂的,比如這裡的方法就跟之前的HDFS Shell有關,結合起來就了解的快了。
當你有能力的時候,推薦你直接點進包裡,檢視一下源碼,其實源碼的邏輯,認真看也是可以看得懂的。比如FileSystem裡邊就有很多常用方法API。
我覺得多點一點,多點進源碼看看,對個人的提升非常大。
2、Configuration
Configuration:該類的對象封裝了用戶端或者伺服器的配置
Configuration conf=new Configuration();
建立一個Configuration對象時,其構造方法會預設加載hadoop中的兩個配置檔案,分别是hdfs-site.xml以及core-site.xml,這兩個檔案中會有通路hdfs所需的參數值,主要是fs.default.name,指定了hdfs的位址,有了這個位址用戶端就可以通過這個位址通路hdfs了。即可了解為configuration就是hadoop中的配置資訊。
使用Configuration類的一般過程是:
- 構造Configuration對象,并通過類的addResource()方法添加需要加載的資源;
- 然後就可以使用get方法和set方法通路/設定配置項,資源會在第一次使用的時候自動加載到對象中。
3、FileSystem
Java抽象類org.apache.hadoop.fs.FileSystem定義了hadoop的一個檔案系統接口。
Hadoop中關于檔案操作類基本上全部是在"org.apache.hadoop.fs"包中,
這些API能夠支援的操作包含:打開檔案,讀寫檔案,删除檔案等。
Hadoop類庫中最終面向使用者提供的接口類是FileSystem,該類是個抽象類,隻能通過來類的get方法得到具體類。
FileSystem:該類的對象是一個檔案系統對象,可以用該對象的一些方法來對檔案進行操作,通過FileSystem 的靜态方法 get 獲得該對象。
get 方法從 conf 中的一個參數 fs.defaultFS 的配置值判斷具體是什麼類型的檔案系統
4、IO操作
IO操作用到的比較多的有FSDataInputStream、FSDataOutputStream和IOUtils。
其實也比較見名知意了,就相當于hdfs分布式叢集的IO操作,以及工具類包。
看了下邊案例就明白了。
三、程式設計代碼實作操作
1、HDFS編寫一個工具類友善測試
HdfsTools.java
package com.biubiubiu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import java.net.URI;
/**
* hdfs的 fs常用操作
*/
public class HdfsTools {
public static final String ROOT_URL="hdfs://192.168.153.231:9000";
public static final String ROOT_USER = "hadoop01";
//得到檔案系統
public static FileSystem getFS(){
try{
Configuration conf = new Configuration();
FileSystem fs =FileSystem.get(URI.create(ROOT_URL),conf,ROOT_USER);
return fs;
}catch(Exception ex){
System.out.println("得到檔案系統失敗:"+ex.getMessage());
return null;
}
}
//關閉檔案系統
public static void closeFS(FileSystem fs){
try{
if(fs!=null)
fs.close();
}catch(Exception ex){
System.out.println("關閉檔案系統出錯:"+ex.getMessage());
}
}
}
2、HDFS實作 -ls 清單檢視
【cli指令】
【java代碼】
package com.biubiubiu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 測試 hdfs dfs -ls
* @author biubiubiu
*/
public class Demo01_ls {
public static void main(String[] args) throws Exception{
//1)建立配置對象
Configuration conf = new Configuration();
//配置叢集的namenode的通路路徑
conf.set(FileSystem.FS_DEFAULT_NAME_KEY,"hdfs://192.168.153.231:9000");
//2)得到檔案系統
FileSystem fs =FileSystem.get(conf);
//3)執行操作
FileStatus[] fileStatuses=fs.listStatus(new Path("/"));
for(FileStatus status :fileStatuses){
System.out.println(status);
System.out.println("路徑:"+status.getPath());
System.out.println("擁有者:"+status.getOwner());
System.out.println("權限:"+status.getPermission().toString());
System.out.println();
}
}
}
3、HDFS判斷是檔案還是目錄
【java代碼】
package com.biubiubiu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 判斷是檔案還是目錄
* @author biubiubiu
*/
public class Demo02_fileOrDir {
public static void main(String[] args) throws Exception{
//1.建立配置對象
Configuration conf = new Configuration();
conf.set(FileSystem.FS_DEFAULT_NAME_KEY,"hdfs://192.168.153.231:9000");
//2.得到檔案系統
FileSystem fs = FileSystem.get(conf);
//3.執行查詢
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if(fileStatus.isFile()){
System.out.println(fileStatus.getPath().getName() + "是檔案!");
}else if (fileStatus.isDirectory()){
System.out.println(fileStatus.getPath().getName() + "是目錄!");
}
}
}
}
4、HDFS實作-mkdir建立目錄
【cli指令】
【java代碼】
package com.biubiubiu;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 判斷是檔案還是目錄
* @author biubiubiu
*/
public class Demo02_fileOrDir {
public static void main(String[] args) throws Exception{
//1.建立配置對象
Configuration conf = new Configuration();
conf.set(FileSystem.FS_DEFAULT_NAME_KEY,"hdfs://192.168.153.231:9000");
//2.得到檔案系統
FileSystem fs = FileSystem.get(conf);
//3.執行查詢
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if(fileStatus.isFile()){
System.out.println(fileStatus.getPath().getName() + "是檔案!");
}else if (fileStatus.isDirectory()){
System.out.println(fileStatus.getPath().getName() + "是目錄!");
}
}
}
}
使用提取的工具類
package com.biubiubiu;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 建立目錄
* @author biubiubiu
*/
public class Test03_mkdir {
public static void main(String[] args) {
FileSystem fs = null;
try {
fs = HdfsUtils.getFS();
fs.mkdirs(new Path("/input/test"));
System.out.println("建立成功");
} catch (Exception ex){
System.out.println("建立失敗" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
}
}
}
5、HDFS删除檔案夾
@Test
public voidt estDelete() throws Exception {
// 1 擷取檔案系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://biubiubiu01:9000"), configuration,
"hadoop01");
// 2 執行删除,true表示遞歸删除所有,預設false不遞歸,若有檔案存在則報異常
fs.delete(new Path("/input/1"), true);
// 3 關閉資源
fs.close();
}
6、HDFS實作本地檔案上傳到叢集
6.1、基于shell cli指令
#【put指令】
hdfs dfs -put /opt/model/test_data/hi.txt /input/1
#【copyFromLocal指令】
hdfs dfs -copyFromLocal /opt/model/test_data/hi.txt /input/1
6.2、基于windows的FS的API
/**
* 基于windows
*
* @param source
* @param target
*/
public static void putOfWin(String source, String target) {
FileSystem fs = null;
try {
//1)建立配置對象
Configuration conf = new Configuration();
//2)得到檔案系統
fs = FileSystem.get(URI.create("hdfs://192.168.153.231:9000"), conf, "hadoop01");
//3) 上傳
fs.copyFromLocalFile(new Path(source), new Path(target));
System.out.println("檔案上傳成功!");
} catch (Exception ex) {
System.out.println("執行檔案上傳出錯:" + ex.getMessage());
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.3、基于Linux測試
/**
* 基于Linux
* 需要打jar包在Linux執行
* @param args
*/
public static void putOfLinux(String[] args){
//檢查參數的合理性
if(args == null || args.length<2){
System.out.println("參數傳遞出錯,傳遞兩個參數:源路徑 目标路徑");
System.exit(0);
}
//1)建立配置對象
Configuration conf = new Configuration();
//2)得到檔案系統
FileSystem fs = null;
try {
fs = FileSystem.get(URI.create("192.168.153.231:9000"),conf,"hadoop01");
//3)進行上傳操作
fs.copyFromLocalFile(new Path(args[0]),new Path(args[1]));
System.out.println("上傳成功!");
}catch (Exception ex){
System.out.println("上傳失敗!" + ex.getMessage());
}finally {
if (fs!=null){
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.4、基于IO操作
/**
* 基于IO
* @param source
* @param target
*/
public static void putOfIO(String source, String target) {
//檔案系統
FileSystem fs = null;
//本地讀取流
FileInputStream in = null;
//hdfs分布式寫入流
FSDataOutputStream out = null;
try {
//1)建立配置對象
Configuration conf = new Configuration();
//2)得到檔案系統
fs = FileSystem.get(URI.create("hdfs://192.168.153.231:9000"), conf, "hadoop01");
//3) 上傳
// 3.1 建立輸入流
in = new FileInputStream(source);
// 3.2 擷取輸出流
out = fs.create(new Path(target));
// 3.3 流複制
IOUtils.copyBytes(in, out, conf);
} catch (Exception ex) {
System.out.println("執行檔案上傳出錯:" + ex.getMessage());
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null)
IOUtils.closeStream(in);
if (out != null)
IOUtils.closeStream(out);
}
System.out.println("檔案上傳成功!");
}
}
6.5、使用提取工具類整體操作
package com.biubiubiu;
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 java.io.FileInputStream;
/**
* 實作檔案上傳到叢集
* @author biubiubiu
*/
public class Test04_upload {
public static void main(String[] args) {
// upload1("E:\\JAVA\\BigData\\Hadoop\\TestData\\hi.txt", "/input/test");
upload2(args);
// upload3("E:\\JAVA\\BigData\\Hadoop\\TestData\\hi.txt", "/input/test");
}
/**
* 基于windows
* @param fromLocal
* @param toHdfs
*/
public static void upload1(String fromLocal, String toHdfs) {
FileSystem fs = null;
try {
fs = HdfsUtils.getFS();
fs.copyFromLocalFile(new Path(fromLocal), new Path(toHdfs));
System.out.println("ok");
} catch (Exception ex) {
System.out.println("error" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
}
}
/**
* 基于Linux
* @param args
*/
public static void upload2(String[] args) {
//檢查參數是否合法
if(args == null || args.length<2){
System.out.println("參數不合法");
System.exit(0);
}
FileSystem fs = null;
try{
fs = HdfsUtils.getFS();
fs.copyFromLocalFile(new Path(args[0]), new Path(args[1]));
System.out.println("上傳成功");
} catch (Exception ex) {
System.out.println("上傳失敗" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
}
}
/**
* 基于IO流操作
* @param fromLocal
* @param toHdfs
*/
public static void upload3(String fromLocal, String toHdfs) {
FileSystem fs = null;
//本地讀取流
FileInputStream in = null;
//HDFS分布式叢集寫入流
FSDataOutputStream out = null;
try {
//擷取檔案系統
fs = HdfsUtils.getFS();
//建立輸入流
in = new FileInputStream(fromLocal);
//擷取輸出流
out = fs.create(new Path(toHdfs));
//進行流複制
IOUtils.copyBytes(in, out, 1024*1024*10);
System.out.println("上傳成功");
} catch (Exception ex) {
System.out.println("上傳失敗" + ex.getMessage());
} finally {
IOUtils.closeStream(out);
IOUtils.closeStream(in);
HdfsUtils.closeFS(fs);
}
}
}
7、HDFS檢測Configuration的配置參數的優先級
- 檢測Configuration的配置參數的優先級.
- 結論:【關于配置參數:按照就近原則】
- 代碼參數>用戶端資源檔案>叢集配置
7.1、代碼權限高于叢集配置測試
/**
* 代碼權限高于叢集配置
* 實作上傳
* 【說明】
* 1)叢集配置副本為3
* 2)代碼中配置副本為1
*/
public static void testPut() throws Exception {
FileSystem fs = null;
try {
//1)建立配置對象
Configuration conf = new Configuration();
//程式中設定配置,優先于伺服器設定 *************************
conf.set("dfs.replication", "1");
//2)得到檔案系統
fs = FileSystem.get(URI.create("hdfs://192.168.153.231:9000"), conf, "hadoop01");
//3) 上傳
fs.copyFromLocalFile(new Path("E:\\JAVA\\BigData\\Hadoop\\TestData\\hi.txt"), new Path("/input/1"));
} catch (Exception ex) {
System.out.println("執行檔案上傳出錯:" + ex.getMessage());
} finally {
if (fs != null)
fs.close();
}
System.out.println("檔案上傳成功!");
}
7.2、用戶端資源檔案權限高于叢集配置測試
/**
* 用戶端資源檔案權限高于叢集配置
* 實作上傳
* 【說明】
* 1)叢集配置副本為3
* 2)用戶端資源檔案中配置服務為2
*/
public static void testPut2() throws Exception {
FileSystem fs = null;
try {
//1)建立配置對象
Configuration conf = new Configuration();
//程式中設定配置,優先于伺服器設定 *************************
// conf.set("dfs.replication", "1");
conf.addResource("hdfs-site.xml");
//2)得到檔案系統
fs = FileSystem.get(URI.create("hdfs://192.168.153.231:9000"), conf, "hadoop01");
//3) 上傳
fs.copyFromLocalFile(new Path("E:\\JAVA\\BigData\\Hadoop\\TestData\\hello.txt"), new Path("/input/1"));
} catch (Exception ex) {
System.out.println("執行檔案上傳出錯:" + ex.getMessage());
} finally {
if (fs != null)
fs.close();
}
System.out.println("檔案上傳成功!");
}
7.3、代碼高于用戶端資源檔案高于叢集配置測試
基于FSAPI
/**
* 代碼高于用戶端資源檔案高于叢集配置
* 實作上傳
* 【說明】
* 1)叢集配置副本為3
* 2)用戶端資源檔案中配置服務為2
* 3)代碼配置副本為1
*/
public static void testPut3() throws Exception {
FileSystem fs = null;
try {
//1)建立配置對象
Configuration conf = new Configuration();
//程式中設定配置,優先于伺服器設定1
conf.set("dfs.replication", "1");
//資源檔案中配置2
conf.addResource("hdfs-site.xml");
//叢集中預設配置為3
//2)得到檔案系統
fs = FileSystem.get(URI.create("hdfs://192.168.153.231:9000"), conf, "hadoop01");
//3) 上傳
fs.copyFromLocalFile(new Path("E:\\JAVA\\BigData\\Hadoop\\TestData\\aaa.txt"), new Path("/input/1"));
} catch (Exception ex) {
System.out.println("執行檔案上傳出錯:" + ex.getMessage());
} finally {
if (fs != null)
fs.close();
}
System.out.println("檔案上傳成功!");
}
基于IO
public static void testPut4(String from, String to) throws Exception{
FileSystem fs =null;//檔案系統
FileInputStream in =null;//本地讀取流
FSDataOutputStream out = null;//hdfs的分布式寫入流
try {
//1.建立configuration
Configuration conf = new Configuration();
conf.set("dfs.replication", "1");
//資源檔案中配置2
conf.addResource("hdfs-site.xml");
//叢集中預設配置為3
//2.得到檔案系統
fs = FileSystem.get(URI.create("hdfs://192.168.153.231:9000"), conf, "hadoop01");
//3.封裝in、out對象
in = new FileInputStream(from);
out = fs.create(new Path(to), true);
//4.執行寫入
IOUtils.copyBytes(in, out, conf);
System.out.println("上傳成功");
}catch (Exception ex){
System.out.println("上傳失敗" + ex.getMessage());
}finally {
if (fs != null) {
fs.close();
}
if (in != null)
IOUtils.closeStream(in);
if (out != null)
IOUtils.closeStream(out);
}
}
8、HDFS實作 -cp 複制
【cli指令】
hdfs dfs -cp /input/1/hello.txt /input/3/hello.txt
【java 代碼】
package com.biubiubiu;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
/**
* 基于叢集的 -cp 複制
* @author biubiubiu
*/
public class Test06_cp {
public static void main(String[] args) {
FileSystem fs = null;
FSDataInputStream in = null;
FSDataOutputStream out = null;
try {
fs = HdfsUtils.getFS();
//資料讀取流
in = fs.open(new Path("/input/hi.txt"));
//資料輸出流
out = fs.create(new Path("/hi.txt"));
//複制
//true 使用後關閉資料流
IOUtils.copyBytes(in, out, 1024*1024*10, true);
System.out.println("複制成功");
} catch (Exception ex) {
System.out.println("複制失敗" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
//IOUtils預設為false,需要手動關閉資料流,如果寫為true,則不需要
}
}
}
9、HDFS實作下載下傳叢集檔案到本地
9.1、基于shell cli指令
【cli指令】
hdfs dfs -get /input/3/hello.txt
hdfs dfs -copyToLocal /input/3/hello.txt
9.2、基于FSAPI
/**
* java代碼基于FSAPI
*/
public static void download1() {
FileSystem fs=null;
try{
//1)建立配置對象
Configuration conf = new Configuration();
//2)得到檔案系統
fs =FileSystem.get(URI.create("hdfs://192.168.153.231:9000"),conf,"hadoop01");
//3) 檔案下載下傳
fs.copyToLocalFile(new Path("/input/1/hello33.txt"),new Path("D:\\"));
System.out.println("檔案下載下傳成功!");
}catch(Exception ex){
System.out.println("執行檔案下載出錯:"+ex.getMessage());
}finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
9.3、基于IO操作
/**
* 基于I/O流實作下載下傳
*/
public static void download2() {
FileSystem fs=null;
FSDataInputStream in=null;
FileOutputStream out=null;
try{
//1)建立配置對象
Configuration conf = new Configuration();
//2)得到檔案系統
fs =FileSystem.get(URI.create("hdfs://192.168.153.231:9000"),conf,"hadoop01");
//3) 檔案下載下傳
//3.1)得到輸入流
in= fs.open(new Path("/input/1/hi.txt"));
//3.2)得到輸出流
out= new FileOutputStream(new File("D:\\hi.txt"));
//3.3)執行下載下傳
IOUtils.copyBytes(in, out, conf);
System.out.println("檔案下載下傳成功!");
}catch(Exception ex){
System.out.println("執行檔案下載出錯:"+ex.getMessage());
}finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
;
}
}
9.4、使用提取工具類整合
package com.biubiubiu;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.FileOutputStream;
/**
* 下載下傳叢集檔案到本地
* @author biubiubiu
*/
public class Test07_download {
public static void main(String[] args) {
// download1("/input/hi.txt", "D:\\");
download2("/input/hi.txt", "D:\\hi.txt");
}
/**
* 基于hdfsapi
* @param fromHdfs
* @param toLocal
*/
public static void download1(String fromHdfs, String toLocal) {
FileSystem fs = null;
try {
fs = HdfsUtils.getFS();
fs.copyToLocalFile(new Path(fromHdfs), new Path(toLocal));
System.out.println("下載下傳成功");
} catch (Exception ex) {
System.out.println("下載下傳失敗" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
}
}
/**
* 基于IO流操作
* @param fromHdfs
* @param toLocal
*/
public static void download2(String fromHdfs, String toLocal) {
FileSystem fs = null;
FSDataInputStream in = null;
FileOutputStream out = null;
try {
fs = HdfsUtils.getFS();
in = fs.open(new Path(fromHdfs));
out = new FileOutputStream(toLocal);
IOUtils.copyBytes(in, out, 1024*1024*10, true);
System.out.println("下載下傳成功");
} catch (Exception ex) {
System.out.println("下載下傳失敗" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
}
}
}
10、HDFS檔案名修改 -mv
【cli指令】
hdfs dfs -mv /input/1/hello2.txt /input/1/hello2_new.txt
【java代碼】
package com.biubiubiu;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
*檔案名修改 -mv
* @author biubiubiu
*/
public class Test08_mv {
public static void main(String[] args) {
FileSystem fs = null;
try {
fs = HdfsUtils.getFS();
fs.rename(new Path("/input/hi.txt"), new Path("/input/hello.txt"));
System.out.println("改名成功");
} catch (Exception ex) {
System.out.println("失敗" + ex.getMessage());
} finally {
HdfsUtils.closeFS(fs);
}
}
}
11、HDFS檢視檔案詳細資訊
【cli指令】
hdfs fsck /input/1/user_friends.csv -files -blocks -locations -racks
【java代碼】
// 檢視詳細資訊
static void testFileDetails(String url){
FileSystem fs =null;
try{
//1)得到檔案系統
fs =HdfsTools.getFS();
//2)執行操作
if(fs!=null){
//2.1)得到該路徑檔案清單
RemoteIterator<LocatedFileStatus> fileStatuss= fs.listFiles(new Path(url),false);
//2.2)循環讀取
while(fileStatuss.hasNext()){ //判斷有沒有目前檔案
//2.3 取得目前的檔案
LocatedFileStatus file =fileStatuss.next();
//2.4 展示目前檔案的資訊
String fileName=file.getPath().getName();
long length=file.getLen();
String permission =file.getPermission().toString();
String owner =file.getOwner();
System.out.println("檔案名:"+fileName+",長度:"+length+",權限:"+permission+",擁有者:"+owner);
//2.5 讀取該檔案的塊的位置資訊
BlockLocation [] blockLocations=file.getBlockLocations();
System.out.println("檔案的塊數:"+blockLocations.length);
//2.5.1 周遊塊清單
int i=0;
for(BlockLocation block:blockLocations){
//得到目前塊所在的節點的位置:
String [] hosts=block.getHosts();
System.out.println("塊【"+i+"】所在的節點位置是:"+ Arrays.toString(hosts));
i++;
}
System.out.println();
System.out.println("------------------------------------");
}
}else{
System.out.println("檔案系統連結失敗!!");
}
}catch(Exception ex){
System.out.println("讀取檔案失敗:"+ex.getMessage());
}finally {
//3)關閉連結
HdfsTools.closeFS(fs);
}
}
覺得有幫助的,請多多支援部落客,點贊關注哦~