hadoop学习笔记--5.HDFS的java api接口访问

来源:互联网 发布:李刚疯狂java视频 编辑:程序博客网 时间:2024/05/06 13:24

hadoop学习笔记--5.HDFS的java api接口访问

一:几个常用类介绍

   (1):configuration类:此类封装了客户端或服务器的配置,通过配置文件来读取类路径实现(一般是core-site.xml)。
(2):FileSystem类:一个通用的文件系统api,用该对象的一些方法来对文件进行操作。
FileSystem fs = FileSystem.get(conf);通过FileSystem的静态方法get获得该对象。
(3):FSDataInputStream:HDFS的文件输入流,FileSystem.open()方法返回的即是此类。
(4):FSDataOutputStream:HDFS的文件输入出流,FileSystem.create()方法返回的即是此类。

二:创建文件目录

public static void mkdir(String path) throws IOException { //读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);Path srcPath =  new Path(path);//调用mkdir()创建目录,(可以一次性创建,以及不存在的父目录)boolean flag = fs.mkdirs(srcPath);if(flag) { System.out.println("create dir ok!");}else { System.out.println("create dir failure");}//关闭文件系统fs.close();}

三:删除文件/目录

/*** 删除文件或者文件目录  * @throws IOException **/public static void rmdir(String filePath) throws IOException {//读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);Path path = new Path(filePath);//调用deleteOnExit()boolean flag = fs.deleteOnExit(path);//fs.delete(path);if(flag) { System.out.println("delete ok!");}else { System.out.println("delete failure");}//关闭文件系统fs.close();}

四:创建文件

/**创建文件**/ public static void createFile(String dst , byte[] contents) throws IOException{//读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);//目标路径Path dstPath = new Path(dst);  //打开一个输出流FSDataOutputStream outputStream = fs.create(dstPath);outputStream.write(contents);//关闭文件系统outputStream.close();fs.close();System.out.println("文件创建成功!"); }

五:列出目录下的文件

 /**列出文件**/ public static void listFile(String path) throws IOException{//读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);//获取文件或目录状态FileStatus[] fileStatus = fs.listStatus(new Path(path));//打印文件的路径for (FileStatus file : fileStatus) {System.out.println(file.getPath());} //关闭文件系统fs.close(); }



六:上传本地文件

 /**上传本地文件**/ public static void uploadFile(String src,String dst) throws IOException{//读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);Path srcPath = new Path(src); //原路径Path dstPath = new Path(dst); //目标路径//调用文件系统的文件复制函数,前面参数是指是否删除原文件,true为删除,默认为falsefs.copyFromLocalFile(false,srcPath, dstPath);//打印文件路径System.out.println("Upload to "+conf.get("fs.default.name"));System.out.println("------------list files------------"+"\n");FileStatus [] fileStatus = fs.listStatus(dstPath);for (FileStatus file : fileStatus) {System.out.println(file.getPath());}//关闭文件系统fs.close(); }

七:文件重命名

/**文件重命名**/ public static void renameFile(String oldName,String newName) throws IOException{//读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);Path oldPath = new Path(oldName);Path newPath = new Path(newName);boolean flag = fs.rename(oldPath, newPath);if(flag) { System.out.println("rename ok!");}else { System.out.println("rename failure");}//关闭文件系统fs.close(); }

八:读取文件内容

public static void readFile(String uri) throws IOException {//读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);InputStream in = null;try {in = fs.open(new Path(uri));//复制到标准输出流IOUtils.copyBytes(in, System.out, 4096,false);} catch (Exception e) {e.printStackTrace();}finally{IOUtils.closeStream(in);}}

九:判断目录是否存在

 //判断目录是否存在 public static boolean existDir(String filePath,boolean create) { boolean flag = false; //判断是否存在 if(StringUtils.isEmpty(filePath)) { return flag; } Path path = new Path(filePath);//读取配置文件Configuration conf = new Configuration();try {//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);//或者create为trueif(create) {//如果文件不存在if(!fs.exists(path)) {fs.mkdirs(path);}}//判断是否为目录if(fs.isDirectory(path)) { flag = true;}}catch (Exception e){e.printStackTrace();}return flag;}



十:追加到文件末尾

/**添加到文件的末尾(src为本地地址,dst为hdfs文件地址) * @throws IOException */ public static void appendFile(String src,String dst) throws IOException { //读取配置文件Configuration conf = new Configuration();//获取文件系统FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);Path dstPath = new Path(dst);//创建需要写入的文件流InputStream in = new BufferedInputStream(new FileInputStream(src));//文件输出流写入FSDataOutputStream out = fs.append(dstPath);IOUtils.copyBytes(in, out, 4096,true);fs.close(); }

十一:主函数

public class HdfsJavaApi {public static void main(String[] args) throws IOException {//读取文件内容//readFile(args[0]);//创建文件目录/*String s= "hello";byte[] bytes = s.getBytes();createFile("/liu/h.txt",bytes);*///删除文件/*rmdir("/liu2");*///上传文件/*uploadFile("/home/liu/hello.text", "/liu/hello.text");*///列出文件/*listFile("/liu");*///文科重命名/*renameFile("/liu/hi.txt", "/liu/he1.text");*///查询目录是否存在/*boolean existDir = existDir("/liu2", false);System.out.println(existDir);*///写入文件末尾appendFile("/home/liu/hello.text","/liu1/hello.text");}



十二:注意要点

1: FileSystem的get()方法有两个。
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);  //默认在hdfs上读取文件
FileSystem fs = FileSystem.get(conf);   //默认从本地上读取文件

hdfs://hadoop1:9000需要与core-site.xml配置文件一致。也可以写成URI.create(uri),不过此种写法在文件路径最头处一定要加上hdfs://hadoop1:9000

2:参数从main函数中的args中获得,Eclipse中选择带参运行


1 0
原创粉丝点击