HDFS的API简单操作源码

1.基础环境搭建与要求

1.Hadoop连接HDFS进行API操作
2.hadoop集群已启动完全分布式
3.有手就行

2.代码

1.创建文件:

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * 创建文件
 * 
 * 
 * **/
public class demo1 {
 
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  //初始化配置
  Configuration conf = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root"); //url写完整,root是用户
  String filepath = "/test1/demo6"; //定义文件路径
  String text="sdawfaw"; //定义文本内容
  FSDataOutputStream fd = fs.create(new Path(text)); //调用create方法
  System.out.println(filepath);
  fs.close();
  fd.close();
  
 }
}

2.创建文件夹

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * 创建文件夹
 * @author ASUS
 * **/
 public class demo2 {
  
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  
  Configuration conf = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
  String dirpath =  "/test1/demo2";
  boolean b = fs.mkdirs(new Path(dirpath));
  System.out.println(String.format(dirpath, b));
  fs.close();
  
  
 }
 
}

3.文件更名

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class demo5 {
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  Configuration conf = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
  String filepath = "/test1/demo2/a.txt"; //改名前文件
  String newName = "/test1/demo2/b.txt"; //改名后文件
  boolean res = fs.rename(new Path(filepath), new Path(newName));
  System.out.println(String.format(filepath, newName,res));
  	}
  }

4.查看文件是否存在

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class demo4 {
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  Configuration conf = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
  String dirpath = "/test1/demo2w/";
  String filepath = "/test1/demo22/a.txt";
  String none ="/none";
  boolean dirEx = fs.exists(new Path(dirpath));
  boolean fileEx = fs.exists(new Path(filepath));
  boolean noEx = fs.exists(new Path(none));
  System.out.println(String.format(dirpath,dirEx));
  System.out.println(String.format(filepath, fileEx));
  System.out.println(String.format(none, noEx));
  fs.close();
 }
}

5.附加文件

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * 附加文件
 * @author ASUS
 * 
 * **/
public class demo3 {
 //附加文件
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  Configuration conf = new Configuration();
//  conf.set("dfs.client.block,write.replace-datanode-on-failure.policy", "NEVER");
//  conf.set("dfs.client.block,write.replace-datanode-on-failure.enable", "true");
  FileSystem fs =FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
  String filepath = "/test1/demo2/a.txt";//定义路径 
  System.out.println("旧文本:");
  printFile(conf,fs,filepath); //打印文件内容 
  FSDataOutputStream w = fs.append(new Path(filepath)); //附加文件*
  String context = "wocaonimagehanhan\n"+"\n"; //附加内容
  w.write(context.getBytes());//写入内容
  w.close(); 
  System.out.println("新文本:");
  printFile(conf, fs, filepath);
 }
 private static void printFile(Configuration conf, FileSystem fs, String filepath) throws IllegalArgumentException, IOException {
  // TODO Auto-generated method stub
  FSDataInputStream read = fs.open(new Path(filepath));
  byte[] datas = new byte[128];
  int res;
  while ((res = read.read(datas))!=-1) {
   System.out.write(datas,0,res);
  }
  //关闭资源
  read.close();
 }
}

6.读文件

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * 读文件
 * @author ASUS
 * 
 * */
public class demo6 {
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  Configuration conf = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
  String filepath = "/test1/demo2/b.txt";
  FSDataInputStream reader = fs.open(new Path(filepath));
  byte[] datas = new byte[128];
  int res;
  while ((res=reader.read(datas))!=-1) {
   System.out.write(datas,0,res);   
  }
  reader.close();
 }
}

7.上传文件

package test1_javaApi;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 * 上传文件
 * @author ASUS
 * 
 * */
public class demo7 {
 public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
  Configuration conf = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
  fs.copyFromLocalFile(new Path("G:\\douban.txt"),new Path("/douban.txt"));
  System.out.println("上传成功");
  fs.close();
 }
}

8.判断是文件还是目录

package test1_javaApi;
/*
 * 判断是文件还是目录
 * 
 * **/
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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 demo8 {
  public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
   Configuration conf = new Configuration();
   FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
   String bashpath = "/test1/demo2/b.txt";
   FileStatus[] filestatus = fs.listStatus(new Path(bashpath));//判断文件状态
   for(FileStatus file:filestatus) {
    if(file.isDirectory()) {
     System.out.println("目录");
    }else{
     System.out.println("文件");
    }
   }
  }
}

3.提示

这些都是简单的操作,需要注意要导入正确的包,不然会有意想不到的bug。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值