HDFS API詳解
Hadoop中關於文件操作類基本上全部是在"org.apache.hadoop.fs"包中,這些API能夠支持的操作包含:打開文件,讀寫文件,刪除文件等。
Hadoop類庫中最終面向用戶提供的接口類是FileSystem,該類是個抽象類,只能通過來類的get方法得到具體類。get方法存在幾個重載版本,常用的是這個:
static FileSystem get(Configuration conf);
該類封裝了幾乎所有的文件操作,例如mkdir,delete等。綜上基本上可以得出操作文件的程序庫框架:
operator()
{
得到Configuration對象
得到FileSystem對象
進行文件操作
}
6.1 上傳本地文件
通過"FileSystem.copyFromLocalFile(Path src,Patch dst)"可將本地文件上傳到HDFS的制定位置上,其中src和dst均為文件的完整路徑。具體事例如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CopyFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
//本地文件
Path src =new Path("D:\\HebutWinOS");
//HDFS為止
Path dst =new Path("/");
hdfs.copyFromLocalFile(src, dst);
System.out.println("Upload to"+conf.get("fs.default.name"));
FileStatus files[]=hdfs.listStatus(dst);
for(FileStatus file:files){
System.out.println(file.getPath());
}
}
}
運行結果可以通過控制台、項目瀏覽器和SecureCRT查看,如圖6-1-1、圖6-1-2、圖6-1-3所示。
1)控制台結果
圖6-1-1 運行結果(1)
2)項目瀏覽器
圖6-1-2 運行結果(2)
3)SecureCRT結果
圖6-1-3 運行結果(3)
6.2 創建HDFS文件
通過"FileSystem.create(Path f)"可在HDFS上創建文件,其中f為文件的完整路徑。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CreateFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
byte[] buff="hello hadoop world!\n".getBytes();
Path dfs=new Path("/test");
FSDataOutputStream outputStream=hdfs.create(dfs);
outputStream.write(buff,0,buff.length);
}
}
運行結果如圖6-2-1和圖6-2-2所示。
1)項目瀏覽器
圖6-2-1 運行結果(1)
2)SecureCRT結果
圖6-2-2 運行結果(2)
6.3 創建HDFS目錄
通過"FileSystem.mkdirs(Path f)"可在HDFS上創建文件夾,其中f為文件夾的完整路徑。具體實現如下:
package com.hebut.dir;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CreateDir {
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path dfs=new Path("/TestDir");
hdfs.mkdirs(dfs);
}
}
運行結果如圖6-3-1和圖6-3-2所示。
1)項目瀏覽器
圖6-3-1 運行結果(1)
2)SecureCRT結果
圖6-3-2 運行結果(2)
6.4 重命名HDFS文件
通過"FileSystem.rename(Path src,Path dst)"可為指定的HDFS文件重命名,其中src和dst均為文件的完整路徑。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Rename{
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path frpaht=new Path("/test"); //舊的文件名
Path topath=new Path("/test1"); //新的文件名
boolean isRename=hdfs.rename(frpaht, topath);
String result=isRename?"成功":"失敗";
System.out.println("文件重命名結果為:"+result);
}
}
運行結果如圖6-4-1和圖6-4-2所示。
1)項目瀏覽器
圖6-4-1 運行結果(1)
2)SecureCRT結果
圖6-4-2 運行結果(2)
6.5 刪除HDFS上的文件
通過"FileSystem.delete(Path f,Boolean recursive)"可刪除指定的HDFS文件,其中f為需要刪除文件的完整路徑,recuresive用來確定是否進行遞歸刪除。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DeleteFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path delef=new Path("/test1");
boolean isDeleted=hdfs.delete(delef,false);
//遞歸刪除
//boolean isDeleted=hdfs.delete(delef,true);
System.out.println("Delete?"+isDeleted);
}
}
運行結果如圖6-5-1和圖6-5-2所示。
1)控制台結果
圖6-5-1 運行結果(1)
2)項目瀏覽器
圖6-5-2 運行結果(2)
6.6 刪除HDFS上的目錄
同刪除文件代碼一樣,只是換成刪除目錄路徑即可,如果目錄下有文件,要進行遞歸刪除。
6.7 查看某個HDFS文件是否存在
通過"FileSystem.exists(Path f)"可查看指定HDFS文件是否存在,其中f為文件的完整路徑。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CheckFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path findf=new Path("/test1");
boolean isExists=hdfs.exists(findf);
System.out.println("Exist?"+isExists);
}
}
運行結果如圖6-7-1和圖6-7-2所示。
1)控制台結果
圖6-7-1 運行結果(1)
2)項目瀏覽器
圖6-7-2 運行結果(2)
6.8 查看HDFS文件的最后修改時間
通過"FileSystem.getModificationTime()"可查看指定HDFS文件的修改時間。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class GetLTime {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path fpath =new Path("/user/hadoop/test/file1.txt");
FileStatus fileStatus=hdfs.getFileStatus(fpath);
long modiTime=fileStatus.getModificationTime();
System.out.println("file1.txt的修改時間是"+modiTime);
}
}
運行結果如圖6-8-1所示。
圖6-8-1 控制台結果
6.9 讀取HDFS某個目錄下的所有文件
通過"FileStatus.getPath()"可查看指定HDFS中某個目錄下所有文件。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class ListAllFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path listf =new Path("/user/hadoop/test");
FileStatus stats[]=hdfs.listStatus(listf);
for(int i = 0; i < stats.length; ++i)
{
System.out.println(stats[i].getPath().toString());
}
hdfs.close();
}
}
運行結果如圖6-9-1和圖6-9-2所示。
1)控制台結果
圖6-9-1 運行結果(1)
2)項目瀏覽器
圖6-9-2 運行結果(2)
6.10 查找某個文件在HDFS集群的位置
通過"FileSystem.getFileBlockLocation(FileStatus file,long start,long len)"可查找指定文件在HDFS集群上的位置,其中file為文件的完整路徑,start和len來標識查找文件的路徑。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class FileLoc {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
Path fpath=new Path("/user/hadoop/cygwin");
FileStatus filestatus = hdfs.getFileStatus(fpath);
BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen());
int blockLen = blkLocations.length;
for(int i=0;i<blockLen;i++){
String[] hosts = blkLocations[i].getHosts();
System.out.println("block_"+i+"_location:"+hosts[0]);
}
}
}
運行結果如圖6-10-1和6.10.2所示。
1)控制台結果
圖6-10-1 運行結果(1)
2)項目瀏覽器
圖6-10-2 運行結果(2)
6.11 獲取HDFS集群上所有節點名稱信息
通過"DatanodeInfo.getHostName()"可獲取HDFS集群上的所有節點名稱。具體實現如下:
package com.hebut.file;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
public class GetList {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(conf);
DistributedFileSystem hdfs = (DistributedFileSystem)fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
for(int i=0;i<dataNodeStats.length;i++){
System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName());
}
}
}
運行結果如圖6-11-1所示。
圖6-11-1 控制台結果