發布時間: 2019-08-01 17:36:27
構建maven工程
導入maven類包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
編寫Junit單元測試類
package net.togogo.hdfsproject;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
public class HdfsClientTest {
FileSystem fs = null;
@Before
public void init() throws Exception {
// 構造一個配置參數對象,設置一個參數:我們要訪問的hdfs的URI
// 從而FileSystem.get()方法就知道應該是去構造一個訪問hdfs文件系統的客戶端,以及hdfs的訪問地址
// new Configuration();的時候,它就會去加載jar包中的hdfs-default.xml
// 然后再加載classpath下的hdfs-site.xml
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.75.129:9000");
/**
* 參數優先級: 1、客戶端代碼中設置的值 2、classpath下的用戶自定義配置文件 3、然后是服務器的默認配置
*/
conf.set("dfs.replication", "3");
// 獲取一個hdfs的訪問客戶端,根據參數,這個實例應該是DistributedFileSystem的實例
// fs = FileSystem.get(conf);
// 如果這樣去獲取,那conf里面就可以不要配"fs.defaultFS"參數,而且,這個客戶端的身份標識已經是hadoop用戶
fs = FileSystem.get(new URI("hdfs://192.168.75.129:9000"), conf, "hadoop");
}
@Test
public void testAddFileToHdfs(){
// 要上傳的文件所在的本地路徑
Path src = new Path("D:\\stone\\HCNA-AI\\images\\idcard.jpg");
// 要上傳到hdfs的目標路徑
Path dst = new Path("/togogo/idcard.jpg");
try {
fs.copyFromLocalFile(src,dst);
System.out.println("文件上傳成功...");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testDelFileFromHdfs(){
// 要上傳到hdfs的目標路徑
Path dst = new Path("/togogo/work");
try {
fs.delete(dst,true);
System.out.println("文件刪除成功...");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查看目錄信息,只顯示文件
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {
// 思考:為什么返回迭代器,而不是List之類的容器
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("--------------為angelababy打印的分割線--------------");
}
}
/**
* 查看文件及文件夾信息
*
* @throws IOException
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
String flag = "d-- ";
for (FileStatus fstatus : listStatus) {
if (fstatus.isFile()) flag = "f-- ";
System.out.println(flag + fstatus.getPath().getName());
}
}
}
?
上一篇: CentOS7安裝Nginx