在windows下的hdfs客戶端編寫
新建一個工程,右鍵 properties -> java build path -> libraries 和之前一樣的操作,這次 new 一個 user libiary 把整個 hadoop3.0.0 的jar包全部導入
upload()運行成功,運行download()的時候出現報錯
HADOOP_HOME and hadoop.home.dir are unset.
解決方案:
- 從linux上把解壓的hadoop-3.0.0文件夾下載到本地
- 下載hadoop3.0.0的 winutils,替換掉bin目錄
- 將hadoop.dll 拷貝至C:\windows\system32\
- 配置環境變量
HADOOP_HOME
,並且在系統變量的path中附加%HADOOP_HOME%/bin
成功運行代碼:
package cn.thousfeet.hadoop.hdfs;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
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 HdfsUtils {
FileSystem fs = null;
@Before
public void init() throws Exception {
// 讀取classpath下的xxx-site.xml配置文件,解析內容封裝到conf對象中
Configuration conf = new Configuration();
// 可以手動設置conf的配置信息(覆蓋原配置文件中的值)
conf.set("fs.defaultFS", "hdfs://node01:9000/");
// 獲取文件系統的客戶端操作實例對象。設置username為之前上傳文件的linux用戶信息,以防沒有讀寫權限
fs = FileSystem.get(new URI("hdfs://node01:9000/"), conf, "thousfeet");
}
/**
* 上傳文件
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void upload() throws IllegalArgumentException, Exception {
fs.copyFromLocalFile(new Path("D:/eclipse-workspace/test.txt"), new Path("hdfs://node01:9000/a/b/c/test.txt"));
}
/**
* 下載文件
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void download() throws IllegalArgumentException, Exception {
fs.copyToLocalFile(new Path("hdfs://node01:9000/aa/test.txt"), new Path("D:/eclipse-workspace/test2.txt"));
}
/**
* 查看文件信息
* @throws Exception
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void listFiles() throws FileNotFoundException, IllegalArgumentException, Exception {
// listFile() 列出的是文件信息,並且提供遞歸遍歷(參數為true時 能遞歸查看文件夾內的文件)
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.txt
* core-site.xml
* hadoop-core-1.2.1.jar
* jdk-8u161-linux-x64.tar.gz
*/
System.out.println("-----------------------");
// listStatus() 列出文件和文件夾的信息,但是不提供遞歸遍歷(需要自行去做遞歸)
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status : listStatus)
{
String name = status.getPath().getName();
System.out.println(name + (status.isDirectory() ? " - is a dir": " - is a file"));
}
/**
* 打印結果
* a - is a dir
* core-site.xml - is a file
* input - is a dir
* jdk-8u161-linux-x64.tar.gz - is a file
*/
}
/**
* 創建文件夾
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void mkdir() throws IllegalArgumentException, Exception {
fs.mkdirs(new Path("/a/b/c"));
}
/**
* 刪除文件或文件夾
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void deleteFiles() throws IllegalArgumentException, Exception {
fs.delete(new Path("hdfs://node01:9000/aa"), true);
}
}
所以說還是windows的eclipse用的舒服啊!(
關於win下調試hadoop的參考資料:
- https://blog.csdn.net/nightwizard2030/article/details/53142069
- https://blog.csdn.net/darkdragonking/article/details/72636917
附:eclipse使用的一些小技巧快捷鍵
Alt + /
提示補全信息(設置全字符補全:https://www.cnblogs.com/firstcsharp/p/4025689.html)
Ctrl + 2 松手 L
new一個對象,前面接收的對象信息快速補全