HDFS常用方法 JAVA代码

获取指定路径所有文件

public  static void  listStatus()  throws Exception  { 
    //该类的对象封转了客户端或者服务器的配置
    Configuration conf=new Configuration();
    //该类的对象是一个文件系统对象
    FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
    //获取某一目录下的所有文件
    FileStatus stats[]=hdfs.listStatus(new Path("/"));
    //遍历输出
    for(int i = 0; i < stats.length; ++i)
        System.out.println(stats[i].getPath().toString());
        hdfs.close();
}

重命名

public  static void  rename()  throws Exception  {
    //该类的对象封转了客户端或者服务器的配置
    Configuration conf=new Configuration();
    //该类的对象是一个文件系统对象
    FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
    //旧文件名
    Path frpaht=new Path("/aaa");
    //旧新文件名
    Path topath=new Path("/aaaaaaa");
    //修改文件名
   boolean isRename=hdfs.rename(frpaht, topath);
   String result=isRename?"修改成功!":"修改失败!";
   System.out.println(result);
}

获取文件日期

public  static void  GetTime()  throws Exception  {  
     //该类的对象封转了客户端或者服务器的配置
     Configuration conf=new Configuration();
     //该类的对象是一个文件系统对象
     FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
     //获取文件信息对象
     FileStatus fileStatus=hdfs.getFileStatus(new Path("/yarn-daemons.txt"));
     //获取指定文件日期
     long modiTime=fileStatus.getModificationTime();
    System.out.println(modiTime);
 }

创建文件夹

public  static void  mkdir ()  throws Exception  {
    //该类的对象封转了客户端或者服务器的配置
    Configuration conf=new Configuration();
    //该类的对象是一个文件系统对象
    FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
    //创建文件夹
    boolean bool2=hdfs.mkdirs(new Path("/user/new"));
    if (bool2){
       System.out.println("创建成功!!");
   }else{
      System.out.println("创建失败!!");
    }
}

删除文件

public  static void  deletefile()  throws Exception  {
    //该类的对象封转了客户端或者服务器的配置
    Configuration conf=new Configuration();
    //该类的对象是一个文件系统对象
    FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
    //删除文件
    boolean isDeleted=hdfs.delete(new Path("/user/new"),true);
    System.out.println("Delete?"+isDeleted);
}

创建数据

public  static void  AddFile()  throws Exception  {
    //该类的对象封转了客户端或者服务器的配置
    Configuration conf=new Configuration();
    //该类的对象是一个文件系统对象
    FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
    //创建数据
    byte[] buff="hello hadoop world!\r\n hadoop ".getBytes();
    //获取文件输出流
    FSDataOutputStream outputStream=hdfs.create(new Path("/tmp/file.txt"));
   //写文件
   outputStream.write(buff,0,buff.length);
   //关流
   outputStream.close();
}

上传数据

public  static void  put()  throws Exception  {
    //该类的对象封转了客户端或者服务器的配置
    Configuration conf=new Configuration();
    //该类的对象是一个文件系统对象
    FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
    //上传的数据
    Path src =new Path("C:/123.py");
    //上传的目标路径
    Path dst =new Path("/");
    hdfs.copyFromLocalFile(src, dst);
}

检查目录是否存在

public  static void  check()  throws Exception  {
   //该类的对象封转了客户端或者服务器的配置
   Configuration conf=new Configuration();
   //该类的对象是一个文件系统对象
   FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
   //检查的目录
   Path findf=new Path("/abc");
   //判断目录是否存在
   boolean isExists=hdfs.exists(findf);
  System.out.println("Exist?"+isExists);
}
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值