使用Eclipse来操作HDFS的文件

一.常用类

  1.Configuration

Hadoop配置文件的管理类,该类的对象封装了客户端或者服务器的配置(配置集群时,所有的xml文件根节点都是configuration)

创建一个Configuration对象时,其构造方法会默认加载hadoop中的两个配置文件,分别是hdfs-site.xml以及core-site.xml,这两个文件中会有访问hdfs所需的参数值,主要是fs.default.name,指定了hdfs的地址,有了这个地址客户端就可以通过这个地址访问hdfs了。即可理解为configuration就是hadoop中的配置信息。

  2.FileSystem

该类的对象是一个文件系统对象,对HDFS中的文件进行的一系列操作,如创建等

  3.FileStatus

获取文件或者文件夹的元信息!比如:文件路径,文件大小,文件所有者,所在的块大小,文件修改时间,备份数量,权限等!

  4.FSDataInputStream

输入流对象!可以HDFS中的文件或者文件夹读取到本地!

  5.FSDataOutputStream

输出流对象! 可以将本地的文件或者文件夹上传到HDFS中!

二.实际应用

  1.

static FileSystem fileSystem=null;
static{
     //创建连接
     String uri="hdfs://192.168.100.2:9000";      
     //加载hadoop配置文件
     Configuration con=new Configuration();
     //创建一个可以操作HDFS的对象
     try{
          fileSystem=FileSystem.get(URI.create(uri),con)          
      }catch(IOException e){
           e.printStackTrace();
      }
}

  2.对文件的操作

1.获取单个文件
public static void catHDFS(String path)throws IOException{
        //获取文件路径
        FileStatus fileStatus=fileSystem.getFileStatus(new Path(path));
        System.out.println(fileStatus);
    }
2.查询文件内容
public static void readFile(String fileName)throws IOException{
      //获取指定文件路径
      FSDateInputStream input=fileSystem.open(new Path(fileName));
      //将文件内容装载到BufferedReader对象当中      
      BufferedReader br=new BufferedReader(new InputStreamReader(input));
      String line="";
      //循环读取数据
      while((line=reader.readLine())!=null){
           System.out.println(line);
      }      
      //关闭资源
      reader.close();
      input.close();
      fileSystem.close();  
}
3.创建一个文件,并向其中写入内容
public static void createFile(String fileName)throws IOException{
        if(fileSystem.exists(new Path(fileName))) {
            System.out.println("文件已经存在");
        }else {
            System.out.println("可以创建");
            FSDataOutputStream create=fileSystem.create(new Path(fileName));
            String str="wsjxzzgdfq";
            create.write(str.getBytes());
            create.flush();
            create.close();
        }
        fileSystem.close();
        
    }
4.创建一个空文件夹
public static void mkdir(String fileName)throws IOException{
        boolean mkdir=fileSystem.mkdirs(new Path(fileName));
        if(mkdir) {
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        fileSystem.close();
    }
5.重命名文件或文件夹
public static void rename(String oldName,String newName)throws IOException{
        boolean rename=fileSystem.rename(new Path(oldName), new Path(newName));
        if(rename) {
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        fileSystem.close();
    }
6.重命名文件或文件夹
public static void remove(String fileName)throws IOException{
        @SuppressWarnings("deprecation")
        boolean delete = fileSystem.delete(new Path(fileName));
        if(delete) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        
        fileSystem.close();
    }
7.从本地(本机)上传文件到HDFS
public static void upfile(String localName,String hdfsName)throws IOException{
        //调用上传文件的方法
        fileSystem.copyFromLocalFile(new Path(localName), new Path(hdfsName));
        fileSystem.close();
    }
8.下载文件
public static void downfile(String localName,String hdfsName)throws IOException{
        //调用下载文件的方法
        fileSystem.copyToLocalFile(new Path(hdfsName), new Path(localName));
        fileSystem.close();
    }

  3.执行上述方法(把想执行的方法去掉注释就好了)

9.执行方法
public static void main(String[]args) throws Exception{
        /*catHDFS("/input/file1.txt");*/
        /*readFile("/input/file3.txt");*/
        /*createFile("wd.txt");*/
        /*mkdir("/wdj");*/
        /*rename("/input/file3.txt","/input/file4.txt");*/
        /*remove("/wdj");*/
        /*upfile("C:\Users\wd\Desktop\wwww.txt","/input");*/
        /*downfile("C:\Users\wd\Desktop","/input/file1.txt");*/
    }
原文地址:https://www.cnblogs.com/wang2386033566/p/10123000.html