項目使用了Maven Project 快速進行HDFS 用戶端程式測試
用戶端作業系統:win10 64位
JDK: 1.7.0_79
開發工具 :Eclipse Luna
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alixx</groupId>
<artifactId>hdfsz</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>hdfsz</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<!-- hadoop 分布式檔案系統類庫 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
<!-- hadoop 公共類庫 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
</project>
測試類HDFSUtil
package com.bonc.hdfsz;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HdfsUtil {
/*
FileSystem是檔案系統的抽象,HDFS是分布式檔案系統對FileSystem的實作,如此即可解耦合。
不論底層檔案系統的具體實作是什麼樣的,檔案系統FileSystem統一提供了通路接口。
*/
FileSystem fs = null;
@Before
public void init() throws IOException, InterruptedException, URISyntaxException{
//通路HDFS檔案系統兩種方式
Configuration conf = new Configuration();
/*
方式1:設定預設檔案系統、設定run Configuration的參數 -DHADOOP_USER_NAME=dream361
預設讀取classpath下的xxx.site.xml配置檔案,并解析其内容,封裝到conf對象中。
conf.set("fs.defaultFS", "hdfs://master:9000/");
*/
fs = FileSystem.get(conf);
/*
方式2:在此方法的參數中設定預設檔案系統、使用者名
根據配置資訊,去擷取一個具體檔案系統的用戶端操作執行個體對象
*/
fs = FileSystem.get(new URI("hdfs://master:9000/"), conf, "dream361");
}
// 上傳方式1:更底層的
@Test
public void upload1() throws IOException{
Path dst = new Path("hdfs://master:9000/c.txt");
FSDataOutputStream os = fs.create(dst);
FileInputStream in = new FileInputStream("D:/c.txt");
IOUtils.copy(in, os);
}
// 上傳方式2: 封裝好的
@Test
public void upload2() throws IllegalArgumentException, IOException{
fs.copyFromLocalFile(new Path("D:/c2.txt"), new Path("hdfs://master:9000/c3.txt"));
}
// 下載下傳檔案
@Test
public void download() throws Exception, IOException{
fs.copyToLocalFile(new Path(""), new Path(""));
}
//疊代列出檔案
@Test
public void listFiles1() throws FileNotFoundException, IllegalArgumentException, IOException{
//listFiles列出的是檔案資訊,而且提供遞歸周遊 ,第二個參數為false則不提供遞歸
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
while(files.hasNext()){
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
}
//疊代列出目錄及檔案
@Test
public void listFiles2() throws FileNotFoundException, IllegalArgumentException, IOException{
//listStatus可以列出檔案和目錄資訊,但是不提供自帶的遞歸周遊
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status:listStatus){
String name = status.getPath().getName();
//判斷是目錄還是檔案,然後列印name+和判斷結果
System.out.println(name+(status.isDirectory()?" is dir":" is file"));
}
}
//建立目錄
@Test
public void mkdir() throws IllegalArgumentException, IOException{
fs.mkdirs(new Path("/aa/bb/cc"));
}
//删除檔案或目錄
@Test
public void rmFile() throws IllegalArgumentException, IOException{
fs.delete(new Path("/aa"),true);
}
//移動檔案
@Test
public void mvFile() throws IllegalArgumentException, IOException{
fs.rename(new Path("/aa/a.txt"), new Path("/bb/b.txt"));
}
//在程式入口測試
public static void main(String[] args) throws IOException {
//配置檔案資訊
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path src = new Path("hdfs://master:9000/jdk.tar.gz");
FSDataInputStream in = fs.open(src);
FileOutputStream os = new FileOutputStream("D:/jdk.tar.gz");
IOUtils.copy(in, os);
}
}