HDFS的java访问接口——FileSystem
写文件 create
读取文件 open
删除文件delete
创建目录 mkdirs
删除文件或目录 delete
列出目录的内容 listStatus
显示文件系统的目录和文件的元数据信息 getFileStatus
java 代码
public class App2 {
static{
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static final String HDFS_PATH = "hdfs://centos-node5:9000";
public static final String PATH_PATH = "/dl100";
public static final String FILE_PATH = "/dl100/f1000";
/**
* hadoop FileSystem 的文件操作
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final FileSystem fileSystem = FileSystem.get(new URL(HDFS_PATH).toURI(),new Configuration());
//创建文件夹
//mkdirDirectroy(fileSystem);
//上传文件
//uploadData(fileSystem);
//下载文件
//downLoadData(fileSystem);
//删除文件(夹)
deleteFile(fileSystem);
}
private static void deleteFile(final FileSystem fileSystem)
throws IOException {
fileSystem.delete(new Path(FILE_PATH), true);
}
private static void downLoadData(final FileSystem fileSystem)
throws IOException {
final FSDataInputStream in = fileSystem.open(new Path(FILE_PATH));
IOUtils.copyBytes(in, System.out, 1024, true);
}
private static void mkdirDirectroy(final FileSystem fileSystem)
throws IOException {
fileSystem.mkdirs(new Path(PATH_PATH));
}
private static void uploadData(final FileSystem fileSystem)
throws IOException {
final FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH));
final FileInputStream in = new FileInputStream("d:/log.txt");
IOUtils.copyBytes(in, out, 1024, true);
}
}