FileSystem类,Hadoop文件API的起点,是一个一个与文件系统交互的抽象类,其对HDFS的操作由不同的具体实现子类来实现。
通过下面的方法来获取一个具体的FileSystem实例:
获取用于HDFS系统的实例:public static FileSystem get(Configuration conf) throws IOException
获取用于本地文件系统的实例:public static LocalFileSystem getLocal(Configuration conf) throws IOException
FileStatus类:可以获取元数据的信息,包括文件的长度,权限,修改时间等等
public class FileStatus
extends
Object
implements
Writable,
Comparable
Interface that represents the client side information for a file.
例子:
- import java.io.IOException;
- 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 FileSystemDemo01 {
- public static void main(String[] args) throws IOException {
- Configuration conf = new Configuration();
- FileSystem hdfs = FileSystem.get(conf);
-
-
-
- Path inputDir = new Path(args[0]);
-
- FileStatus[] inputFiles = hdfs.listStatus(inputDir);
-
- System.out.println(inputFiles.length);
- System.out.println(inputFiles[0].toString());
- System.out.println(inputFiles[0].getGroup());
- System.out.println(inputFiles[0].getModificationTime());
- }
- }
-
-
-
-
-
-
-
一个综合例子:把本地D:\test下的文件复制到HDFS文件/usr/test2下
![]()
- import java.io.IOException;
-
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FSDataOutputStream;
- import org.apache.hadoop.fs.FileStatus;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
-
-
- public class PutMerge {
- public static void main(String[] args) throws IOException {
- Configuration conf = new Configuration();
- FileSystem hdfs = FileSystem.get(conf);
- FileSystem local = FileSystem.getLocal(conf);
-
- Path localDir = new Path(args[1]);
- Path hdfsDir = new Path(args[0]);
-
- FileStatus[] inputFile = local.listStatus(localDir);
- FSDataOutputStream out = hdfs.create(hdfsDir);
-
- for(int i=0;i<inputFile.length;i++){
- System.out.println(inputFile[i].getPath().getName());
- FSDataInputStream in = local.open(inputFile[i].getPath());
- byte[] buffer = new byte[255];
- int byteRead = 0;
- while((byteRead=in.read(buffer))>0){
- out.write(buffer,0,byteRead);
- }
- out.close();
- }
- }
- }
-
-
-
-
-
-
-
-
http://blog.csdn.net/jtlyuan/article/details/7535983