Hadoop的基本使用(2)——通过代码操作HDFS

1、导入相关jar包

hadoop-common 相关jar包
hadoop-hdfs 相关jar包

2、指定hadoop入口获得fileSystem对象

public void before() throws IOException{
    Configuration conf=new Configuration();
    conf.set("fs.defaultFS", "hdfs://192.168.11.128:9999");
    conf.setInt("dfs.replication", 1);
    fileSystem=FileSystem.get(conf);

}

3、通过fileSystem对象对hdfs进行操作

//创建文件夹
@Test
public void mkdir() throws IOException{
    Path path=new Path("/demo/test2");
    boolean mkdirs = fileSystem.mkdirs(path);
    System.out.println(mkdirs);
}
//判断路径是否存在
@Test
public void exist() throws IOException{
    Path path=new Path("/gjhg");
    boolean exists = fileSystem.exists(path);
    System.out.println(exists);
}
//判断所给路径是否为文件夹或文件
@Test
public void isFile() throws IOException{
    Path src=new Path("/demo/test/helloworld.txt");
    boolean dir = fileSystem.isDirectory(src);
    boolean file = fileSystem.isFile(src);
    System.out.println(dir);
    System.out.println(file);
}
//上传文件:1、路径 2、流
@Test
public void copyFromLocalFile() throws IOException{
    Path src=new Path("H:\\hadoopTest\\text.txt");
    Path dst=new Path("/demo/test2/text");
    fileSystem.copyFromLocalFile(src, dst);
}
@Test
public void copyFormLocalStream() throws IOException{
    Path dst=new Path("/demo/test/hahah.txt");
    FSDataOutputStream fos=fileSystem.create(dst);
    FileInputStream fis=new FileInputStream("H:\\hadoopTest\\howareyou.txt");
    IOUtils.copyBytes(fis, fos, 1024);
    IOUtils.closeStream(fis);
    IOUtils.closeStream(fos);
}
//下载文件:1、路径 2、流
@Test
public void copyToLocalFile() throws IOException{
    Path src=new Path("/demo/test/helloworld.txt");
    Path dst=new Path("H:/hadoopTest/hi.txt");
    fileSystem.copyToLocalFile(false, src, dst, true);
}
@Test
public void copyToLocalStream() throws IOException{
    Path dst=new Path("/demo/test/hahah.txt");
    InputStream fis=fileSystem.open(dst);
    FileOutputStream fos=new FileOutputStream("H:\\hadoopTest\\hah.txt");
    IOUtils.copyBytes(fis, fos, 1024);
    IOUtils.closeStream(fis);
    IOUtils.closeStream(fos);
}
//输出文件信息
@Test
public void fileList() throws FileNotFoundException, IOException{
    Path dst=new Path("/");
    RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(dst, true);
    while(listFiles.hasNext()){
    LocatedFileStatus next=listFiles.next();
    System.out.println(next);
}

4、关闭fileSystem

@After
public void after() throws IOException{
    fileSystem.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值