HDFS文件系统介绍
Hadoop提供的操作HDFS的api接口是以FileSystem为基础的,在该类中提供一系列操作文件的方法,比如:文件上传copyFromLocalFile方法,创建文件create方法,删除文件delete方法等。该类的全称为org.apache.hadoop.fs.FileSystem。主要的子类有:DistributedFileSystem, WebHdfsFileSystem等。 通过FileSystem访问远程集群一般情况下需要给定配置信息,Hadoop通过自定义的Configuration类来给定hadoop相关的连接信息。Configuration采用延迟加载的模式来加载配置信息,加载顺序是按照代码顺序加载,但是如果在代码中强制指定的话,那么会覆盖文件中的加载。
FileUtil工具类 ,先贴一下
public class HDFSUtil {
public static Configuration getConfiguration(){
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node-47:9000");
return configuration;
}
public static FileSystem getFileSystem() throws Exception{
return getFileSystem(getConfiguration());
}
public static FileSystem getFileSystem(Configuration configuration) throws IOException {
return FileSystem.get(configuration);
}
}
HDFS写文件
方法:append 参数: f:指定要写出文件的路径,可以为相对路径。 bufferSize: 缓冲区大小 返回值:如果创建成功获得FSDataOutputStream输出流,否则出现异常信息。
/**
* test append
* @throws Exception
*/
public static void testAppend()throws Exception{
FileSystem fileSystem = HDFSUtil.getFileSystem();
FSDataOutputStream append = fileSystem.append(new Path("/part-r-00000"));
append.write("yes yes !!".getBytes());
append.close();
fileSystem.close();
}
HDFS创建文件并输出文件内容
方法:create 参数: f:指定要创建文件的路径,可以为相对路径。 permission:指定文件权限,默认为644(rw-r--r--)。 overwrite: 是否覆盖,默认覆盖。 bufferSize: 进行写过程中缓存区大小,默认4096。 replication: 备份个数,默认3。 blockSize: 块大小,默认128MB。 progress: 进程通知对象,默认为空。 返回值:如果创建成功,返回FSDataOutputStream对象;否则出现异常信息。
/**
* test create new file or
* @throws Exception
*/
//create new file /testcreatenewfile true
///part-r-00000: masked=rw-r--r--
//[root@node-47 hadoop-2.8.3]# hdfs dfs -cat /part*
//hello hadoop fs
//you are smart
public static void testCreateNewFile()throws Exception{
FileSystem fileSystem = HDFSUtil.getFileSystem();
//如果只是只是创建一个新的文件,直接调用createNewFile 就行了,因为createNewFile 里面也是调用的create方法。
boolean createNewFileStatus = fileSystem.createNewFile(new Path("/testcreatenewfile"));
System.out.println("create new file /testcreatenewfile " +createNewFileStatus);
//该方法返回的是一个流对象,你可以对该文件进行写入操作
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/part-r-00000"), true);//覆盖一个原本存在的文件
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream));
bufferedWriter.write("hello hadoop fs");
bufferedWriter.newLine();
bufferedWriter.write("you are smart");
bufferedWriter.close();
fsDataOutputStream.close();
fileSystem.close();
}
HDFS读文件内容
方法:open 参数: f:指定要读取的文件路径,可以为相对路径。 bufferSize: 缓冲区大小。 返回值:如果创建成功获得FSDataInputStream输出流,否则出现异常信息。
/**
* test read file
* @throws Exception
*/
//hello hadoop fs
//you are smart
public static void testReadFile() throws Exception {
FileSystem fileSystem = HDFSUtil.getFileSystem();
FSDataInputStream open = fileSystem.open(new Path("/part-r-00000"));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open));
String line=null;
while ((line = bufferedReader.readLine())!=null){
System.out.println(line);
}
bufferedReader.close();
open.close();
fileSystem.close();
}
HDFS创建文件夹
方法:mkdirs 参数: f:指定要创建的文件夹路径,可以为相对路径。 permission: 指定创建文件的权限,默认755。 返回值:如果创建成功则返回true;否则返回false。
/**
* test mk dir
* @throws Exception
*/
//test mkdir true
//FileStatus{path=hdfs://node-47:9000/testmkdir; isDirectory=true; modification_time=1538289780249; access_time=0; owner=zhang; group=supergroup; permission=rwxr-xr-x; isSymlink=false}
public static void testMkdirs() throws Exception {
FileSystem fileSystem = HDFSUtil.getFileSystem();
boolean mkdirsstatus = fileSystem.mkdirs(new Path("/testmkdir"));
System.out.println("test mkdir "+mkdirsstatus);
System.out.println(fileSystem.getFileStatus(new Path("/testmkdir")));
fileSystem.close();
}
HDFS上传文件
方法:copyFromLocal 参数: delSrc:是否删除本地文件,默认true。 overwrite:当目标文件存在的时候,是否覆盖,默认true。 srcs/src:本地文件,可以指定为数组或者单个文件。 dst:集群存储文件。 返回值:无,如果操作失败,会产生异常信息。 其他类似方法: moveFromLocal从本地移动文件到集群上。 copyToLocal从集群上复制文件到本地。 moveToLocal从集群上移动文件到本地。
/**
* test copy to local
* @throws Exception
*/
public static void testCopyToLocal() throws Exception {
FileSystem fileSystem = HDFSUtil.getFileSystem();
fileSystem.copyToLocalFile(new Path("/part-r-00000"),new Path("C:\\Users\\zhang\\part.txt"));
fileSystem.close();
}
/**
* test copy from local
* @throws Exception
*/
public static void testCopyFromLocal() throws Exception{
FileSystem fileSystem = HDFSUtil.getFileSystem();
fileSystem.copyFromLocalFile(new Path("C:\\Users\\zhang\\part.txt"),new Path("/part-r-00000-r"));
fileSystem.close();
}
HDFS删除文件
方法:delete 参数: f:要删除的文件路径,可以为绝对路径。 recursive:是否进行递归删除,默认为true。 返回值:如果文件不存在,则返回false。如果指定recursive为false,而且要删除的文件夹不为空,那么抛出异常,如果删除成功返回true。 其他删除方法: deleteOnExit: 如果存在则返回true,并标记删除,如果不存在,则返回false。
/**
* test delete file or directory
* @throws Exception
*/
//delete file /output/output201809281415 true
//delete file /output/output201809301328 true
public static void testDelete() throws Exception{
FileSystem fileSystem = HDFSUtil.getFileSystem();
boolean deletestatus = fileSystem.delete(new Path("/output/output201809281415"), true);
System.out.println("delete file /output/output201809281415 " +deletestatus);
boolean deletestatus2 = fileSystem.deleteOnExit(new Path("/output/output201809301328/part-r-00000"));
System.out.println("delete file /output/output201809301328 " +deletestatus2);
fileSystem.close();
}
HDFS查看文件属性
方法:getFileStatus 参数: f:要获取状态属性指定的文件路径,可以为绝对路径。 返回值:如果获取文件属性成功,则返回FileStatus对象。否则发生异常信息。 其他类似方法: listStatus: 递归的获取文件属性信息。
/**
* test get file status
* @throws Exception
*/
//fileStatus : FileStatus{path=hdfs://node-47:9000/output; isDirectory=true; modification_time=1538285329273; access_time=0; owner=root; group=supergroup; permission=rwxr-xr-x; isSymlink=false}
//hdfs://node-47:9000/output/_SUCCESS_________hdfs://node-47:9000/output
//hdfs://node-47:9000/output/output201809281415/_SUCCESS_________hdfs://node-47:9000/output/output201809281415
//hdfs://node-47:9000/output/output201809281415/part-r-00000_________hdfs://node-47:9000/output/output201809281415
//hdfs://node-47:9000/output/output201809281905/_SUCCESS_________hdfs://node-47:9000/output/output201809281905
//hdfs://node-47:9000/output/output201809281905/part-r-00000_________hdfs://node-47:9000/output/output201809281905
//hdfs://node-47:9000/output/output201809301328/_SUCCESS_________hdfs://node-47:9000/output/output201809301328
//hdfs://node-47:9000/output/output201809301328/part-r-00000_________hdfs://node-47:9000/output/output201809301328
//hdfs://node-47:9000/output/part-r-00000_________hdfs://node-47:9000/output
public static void testGetFileStatus() throws Exception{
FileSystem fileSystem = HDFSUtil.getFileSystem();
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/output"));
System.out.println("fileStatus : "+fileStatus);
RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/output"), true);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~```");
while (iterator.hasNext()){
LocatedFileStatus file = iterator.next();
System.out.println(file.getPath() +"_________"+file.getPath().getParent());
}
fileSystem.close();
}
HDFS 合并多个文件到一个文件中
方法:FileUtil 工具类的copyMerge方法,参数为src文件系统,src文件路径,dst文件系统,dst文件路径,是否删除源文件,haddoop配置文件,addstring 可以设置文件编码方式
返回值为是否合并成功
/**
* test megre
* @throws Exception
*/
public static void testCopyMerge()throws Exception{
FileSystem fileSystem = HDFSUtil.getFileSystem();
Configuration configuration = HDFSUtil.getConfiguration();
Path srcPath = new Path("/testmkdir");
Path dstPath = new Path("/output/megredres3");
boolean b = FileUtil.copyMerge(fileSystem, srcPath, fileSystem, dstPath, false, configuration, null);
System.out.println("merge result is "+b);
fileSystem.close();
}
其他API接口
rename: 修改文件名称。 exists: 指定文件是否存在。 setReplication: 重新设置文件的备份个数。 isDirectory: 判断是否是文件夹。 setOwner: 设置文件所属者信息。 ......... api参考http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.5.0-cdh5.3.6/api/index.html