目錄文章來源地址http://www.zghlxwxcb.cn/news/detail-519234.html
//通過工具類來操作hdfs ? hdfs dfs -put d:user_info.txt ?/user_info.txt?
// 將文件放入到hdfs中
@Test
public void test1() throws IOException { // 構(gòu)建方法的時(shí)候,不要加 static 加了之后@Test就不能用了
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs hdfs dfs -put d:user_info.txt /user_info.txt
// 將文件放入到hdfs中
fileSystem.copyFromLocalFile(new Path("d:\\user_info.txt"),new Path("/"));
// Permission denied: 看到這個(gè)就一定要想到權(quán)限問題
// hdfs dfs -chmod -R 777 /
/*
運(yùn)行結(jié)果中有如下的信息,可以忽略
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
*/
//關(guān)閉hdfs的連接工具
if (fileSystem != null) {
fileSystem.close();
}
?2.通過工具類來操作hdfs ? hdfs dfs -get hdfs路徑 ? 本地路經(jīng)? 將文件放入到本地Windows中
@Test
public void test2() throws IOException {
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs hdfs dfs -get hdfs路徑 本地路徑
// 將文件放入到本地Windows中(或者說,你的代碼運(yùn)行在哪里,你的本地就是哪里)
fileSystem.copyToLocalFile(new Path("/user_info.txt"),new Path("D:\\BD2209太湖"));
// 關(guān)閉操作工具類
fileSystem.close();
}
3.通過工具類來操作hdfs ? hdfs dfs -mkdir -p ?hdfs路徑
@Test
public void test3() throws IOException {
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs hdfs dfs -mkdir -p hdfs路徑
boolean mkdir_flag = fileSystem.mkdirs(new Path("/dir_from_java/dir1/dir2"));
System.out.println(mkdir_flag);
// 關(guān)閉操作工具類
fileSystem.close();
}
4.通過工具類來操作hdfs ?查看一個(gè)文件是否存在
@Test
public void test4() throws IOException {
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs 查看一個(gè)文件是否存在
boolean exists = fileSystem.exists(new Path("/suibian"));
System.out.println(exists);
boolean exists1 = fileSystem.exists(new Path("/test1"));
System.out.println(exists1);
// 關(guān)閉操作工具類
fileSystem.close();
}
5.刪除一個(gè)hdfs中的文件
@Test
public void test5() throws IOException {
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs hdfs dfs -rm hdfs絕對(duì)路徑
// 刪除一個(gè)hdfs中的文件
// delete 方法自帶 -r 參數(shù),也就是直接調(diào)用的時(shí)候,就會(huì)遞歸刪除
// 如果想要避免直接遞歸刪除,可以使用第二個(gè)參數(shù), false 來進(jìn)行控制
boolean delete = fileSystem.delete(new Path("/test"),false);
System.out.println(delete);
// 關(guān)閉操作工具類
fileSystem.close();
}
6.查看某一個(gè)路徑下的所有文件以及文件夾
@Test
public void test6() throws IOException {
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs hdfs dfs -ls hdfs絕對(duì)路徑
// 查看某一個(gè)路徑下的所有文件以及文件夾
// listStatus 返回值是一個(gè)對(duì)象數(shù)組,那么為什么要返回一個(gè)對(duì)象數(shù)組呢?直接給一個(gè)String 數(shù)組不就行了嗎?
// 對(duì)象中可以擁有屬性和方法,一個(gè)對(duì)象里面可以存儲(chǔ)大量的信息
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
System.out.println(fileStatus.getPath());
System.out.println("是不是一個(gè)文件夾:"+fileStatus.isDirectory());
System.out.println("文件大小是:"+fileStatus.getLen());
System.out.println("副本數(shù):"+fileStatus.getReplication());
System.out.println("------------------");
}
// 關(guān)閉操作工具類
fileSystem.close();
}
7.通過工具類來操作hdfs ?查看關(guān)于數(shù)據(jù)塊的信息
@Test
public void test7() throws IOException {
//操作hdfs
//1. 初始化配置對(duì)象 需要new出來
Configuration conf = new Configuration();
//2. 添加配置 (其實(shí)就是提供一個(gè)key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 構(gòu)建操作hdfs的具體工具類
FileSystem fileSystem = FileSystem.get(conf);
//通過工具類來操作hdfs 查看關(guān)于數(shù)據(jù)塊的信息
// 文件本身對(duì)于 hdfs來說,是一個(gè)邏輯上的概念
// listFiles 方法可以返回一個(gè)存放對(duì)象的迭代器,這個(gè)對(duì)象中有 數(shù)據(jù)塊的信息
// 查看一個(gè)迭代器中的內(nèi)容時(shí),使用while循環(huán),while循環(huán)的剎車踏板:hasNext()
// hasNext()返回的值是false的時(shí)候,就算是讀完了迭代器,返回true的時(shí)候,就是還有下一個(gè)元素需要讀取
RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
System.out.println(fileStatus.getLen());
System.out.println(fileStatus.getPath());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
// 這個(gè)循環(huán)會(huì)循環(huán)幾次,是根據(jù)一個(gè)文件被拆成了多少個(gè)塊決定的
for (BlockLocation blockLocation : blockLocations) {
String[] hosts = blockLocation.getHosts();
String strHosts = Arrays.toString(hosts);
String names = Arrays.toString(blockLocation.getNames());
long offset = blockLocation.getOffset();
long length = blockLocation.getLength();
System.out.println("所在的DataNode是:"+names+" 所在主機(jī):"+strHosts+" 偏移量是:"+offset+" 塊的大小是:"+length);
}
System.out.println("--------------");
}
// 關(guān)閉操作工具類
fileSystem.close();
}
8.查看所有文件
@Test
public void test3() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hadoop10:8020");
FileSystem fileSystem = FileSystem.get(conf);
//2:調(diào)用方法listFiles 獲取 /目錄下所有的文件信息,,參數(shù)true代表遞歸遍歷
RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
//3:遍歷迭代器
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
//getPath()方法就是獲取絕對(duì)路徑
System.out.println(fileStatus.getPath() + "----" +fileStatus.getPath().getName());
//文件的block信息
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
String[] hosts = blockLocation.getHosts();
String strHosts = Arrays.toString(hosts);
String names = Arrays.toString(blockLocation.getNames());
long offset = blockLocation.getOffset();
long length = blockLocation.getLength();
System.out.println("所在的DataNode是:"+names+" 所在主機(jī):"+strHosts+" 塊的大小是:"+length);
}
}
// 關(guān)閉操作工具類
fileSystem.close();
}
文章來源:http://www.zghlxwxcb.cn/news/detail-519234.html
到了這里,關(guān)于使用javaAPI對(duì)HDFS進(jìn)行文件上傳,下載,新建文件及文件夾刪除,遍歷所有文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!