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);/*操作HDFS文件系统的类*/
// FileSystem local = FileSystem.getLocal(conf);/*操作本地文件系统的类*/
Path inputDir = new Path(args[0]);/*获取文件的路径*/
/*FileStatue类*/
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());
}
}
/*输出结果:
1
org.apache.hadoop.fs.FileStatus@a82e7892
supergroup
1336053927670
*/
一个综合例子:把本地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();
}
}
}
/*输出结果
12/05/04 13:37:24 WARN conf.Configuration: DEPRECATED: hadoop-site.xml found in the classpath. Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, mapred-site.xml and hdfs-site.xml to override properties of core-default.xml, mapred-default.xml and hdfs-default.xml respectively
4d aa.txt
aa.txt
bbaa.txt
ccaa.txt
*/