HDFS编程基础

1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件;-put覆盖
-appendToFile追加

2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;

if $(hadoop fs -test -e /home/fenghao);

then $(hadoop fs -copyToLocal text.txt ./text.txt);

else $(hadoop fs -copyToLocal text.txt ./text2.txt);

fi

3)将HDFS中指定文件的内容输出到终端中;cat

4)显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;ls

5)给定HDFS中某一个目录,递归输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息;lsr

6)提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;mkdir -p

if $(hadoop dfs -test -d “dirpath”);then $(hadoop dfs -touch “filepath”); else $(hadoop dfs -mkdir -p “dirpath” && hadoop dfs -touch “filepath”); fi

7)提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在,则自动创建相应目录;删除目录时,当该目录为空时删除,当该目录不为空时不删除该目录;mkdir -p

if $(hadoop dfs -test -z “dirpath”);then $(hadoop dfs -rmdir “dirpath”); else $(); fi

8)向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;appendToFile -localpath -hdfspath 结尾

1.copyToLocal
2.cat file1>>file2 3.copyFromLocal -f

9)删除HDFS中指定的文件; rm

10)在HDFS中,将文件从源路径移动到目的路径。mv

public class HDFS {
   
 public static FileSystem fs;
  public static void init(){
   
   Configuration conf = new Configuration();
   conf.set("fs.defaultFS", "hdfs://localhost:9000");
   //conf.set("dfs.replication","1");
   try {
   
     fs = FileSystem.get(conf);
   } catch (IOException e) {
   
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  public static void uploads(String localFile,String remoteFile) throws IOException{
   
         //假设  /user/hadoop/input路径已存在 如没存在 在命令行输入 hadoop fs -mkdir -p /user/hadoop/input
         init();
         //String localFileName = "/home/hadoop/myfile.txt";
         //String remoteFileName = "/user/hadoop/input/myfile.txt";
         Path localFilePath = new Path(localFile);
         Path remoteFilePath = new Path(remoteFile);
         if(!fs.exists(remoteFilePath)) {
      远程文件不存在
         System.out.println("remote don't have this file");
           fs.copyFromLocalFile(localFilePath, remoteFilePath);
           System.out.println("success upload");
         }else {
      //远程文件已存在
           System.out.println("already exists:1 cover ;2  append ");
           int choice = 0;
           Scanner input  = new Scanner(System.in);
           choice = input.nextInt();
           switch(choice) {
   
           case 1:fs.copyFromLocalFile(false, true, localFilePath,remoteFilePath);
              System.out.println("successful cover");break;
           case 2:FSDataOutputStream out = fs.append(remoteFilePath);
               FileInputStream in = new FileInputStream(localFile); //字节流
             byte buff[] = new byte[1024];
             int read = -1;
             while((read =in.read(buff))>0) {
     //in.read()返回读取数据的长度
              out.write(buff, 0, read);
             }
             out.close()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值