hdfs中的基础java脚本

17 篇文章 0 订阅

********************************************************************************************
 
上传文件   vi CopyFile.java
********************************************************************************************************************
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 CopyFile {
    public static void main(String[] args) throws Exception {
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        Path src =new Path("/home/hadoop/bbb/b1");
        Path dst =new Path("hdfs://h101:9000/user/hadoop");
        hdfs.copyFromLocalFile(src, dst);
        System.out.println("Upload to"+conf.get("fs.default.name"));
        FileStatus files[]=hdfs.listStatus(dst);
        for(FileStatus file:files){
            System.out.println(file.getPath());
        }
    }
}
***************************************************************************************************************************
 

HDFS创建文件  vi CreateFile.java
**************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
public class CreateFile {
    public static void main(String[] args) throws Exception {
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        byte[] buff="hello hadoop world!\n".getBytes();
 
 Path dst =new Path("hdfs://h151:9000/user/hadoop");
        Path dfs=new Path("hdfs://h151:9000/user/hadoop/hellow.txt");
        FSDataOutputStream outputStream=hdfs.create(dfs);
        outputStream.write(buff,0,buff.length);
      
    FileStatus files[]=hdfs.listStatus(dst);
        for(FileStatus file:files){
            System.out.println(file.getPath());
}
}
}
**************************************************************************************************************************

创建目录   vi CreateDir.java
**************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CreateDir {
    public static void main(String[] args) throws Exception{
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        Path dfs=new Path("hdfs://h101:9000/user/hadoop/TestDir");
        hdfs.mkdirs(dfs);
    }
}

***************************************************************************************************************************
重命名文件  vi Rename.java
***************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Rename{
    public static void main(String[] args) throws Exception {
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        Path frpaht=new Path("hdfs://h101:9000/user/hadoop/b1");    //旧的文件名
        Path topath=new Path("hdfs://h101:9000/user/hadoop/bb111");    //新的文件名
        boolean isRename=hdfs.rename(frpaht, topath);
        String result=isRename?"成功":"失败";
        System.out.println("文件重命名结果为:"+result);
    }
}
**********************************************************************************************************************************
  
删除hdfs 上的文件 vi delete.java

************************************************************************************************************************************

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DeleteFile {
    public static void main(String[] args) throws Exception {
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        Path delef=new Path("hdfs://h101:9000/user/hadoop/bb111");
       
        boolean isDeleted=hdfs.delete(delef,false);
        //递归删除
        //boolean isDeleted=hdfs.delete(delef,true);
        System.out.println("Delete?"+isDeleted);
    }
}

***********************************************************************************************************************************************
查看文件是否存在  vi CheckFile.java
**********************************************************************************************************************************************
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; 
public class CheckFile {
    public static void main(String[] args) throws Exception {
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        Path findf=new Path("hdfs://h101:9000/user/hadoop/hellow.txt");
        boolean isExists=hdfs.exists(findf);
       
        String result=isRename?"文件存在":"文件不存在";
        System.out.println("检查结果为:"+result);
    }
}

****************************************************************************************************************************************************
查看HDFS文件最后修改时间   vi GetLTime.java
****************************************************************************************************************************************************
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 GetLTime {

    public static void main(String[] args) throws Exception {
        Configuration conf=new Configuration();
        FileSystem hdfs=FileSystem.get(conf);
        Path fpath =new Path("hdfs://h101:9000/user/hadoop/hellow.txt");
        FileStatus fileStatus=hdfs.getFileStatus(fpath);
        long modiTime=fileStatus.getModificationTime();
        System.out.println("file1.txt的修改时间是"+modiTime);
    }
}

*************************************************************************************************************************************************************
查看 hdfs文件  vi URLcat.java

****************************************************************************************************************************************************************
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;
public class URLcat{
        static {
                URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        }
        public static void main(String[] args) throws Exception {
                InputStream in = null;
                try {
                        in = new URL(args[0]).openStream();
                        IOUtils.copyBytes(in, System.out, 4086, false);
                } finally {
                        IOUtils.closeStream(in);
                }
}
}
************************************************************************************************************************************
判断文件是否存在 vi nimei.java

************************************************************************************************
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataOutputStream;
public class nimei{
    public static void main(String[] args) throws Exception {
         Configuration conf=new Configuration();
         FileSystem hdfs=FileSystem.get(conf);
         Path find=new Path("hdfs://h151:9000/user/hadoop/hellow.txt");
         boolean isExists=hdfs.exists(find);
      
   if(isExists==false){
            FSDataOutputStream outputStream=hdfs.create(find);
            FileStatus files[]=hdfs.listStatus(find);
           
           for(FileStatus file:files){
            System.out.println(file.getPath()); 
                        }
                        }
    else{
        System.out.println("您要创建的文件已经存在");
                 }
}
}
**********************************************************************************************************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值