java hadoop_搭建hadoop java开发环境

packagehadoopDemo;importjava.io.IOException;importjava.net.URI;importjava.net.URISyntaxException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.FileSystem;importorg.apache.hadoop.fs.LocatedFileStatus;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.fs.RemoteIterator;public classAa {public static void main(String[] args) throwsException{

Aa a=newAa();

a.mv("e:/c.txt", "/aaa");

a.getAllFile();

a.delete("/aaa");

a.delete("/b.txt");

}public void upload(String src,String dst) throwsIOException, InterruptedException, URISyntaxException{//拿到一个文件系统与客户端一个实例

FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),new Configuration(),"root");

filesys.copyFromLocalFile(new Path(src), newPath(dst));

filesys.close();

}public void download(String src,String dst) throwsIOException, InterruptedException, URISyntaxException{

FileSystem fileSystem= FileSystem.get(new URI("hdfs://linux1:9000"), new Configuration(), "root");

fileSystem.copyToLocalFile(new Path(src), newPath(dst));

fileSystem.close();

}public void getAllFile() throwsIOException, InterruptedException, URISyntaxException{

Configuration cfg=newConfiguration();

FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");

RemoteIterator listFiles=filesys.listFiles(new Path("/"), true);while(listFiles.hasNext()){

LocatedFileStatus next2=listFiles.next();

String name=next2.getPath().getName();

System.out.println(next2.getPath()+" "+name);

}

}public void mkdir(String dir) throwsIOException, InterruptedException, URISyntaxException{

Configuration cfg=newConfiguration();

FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");

filesys.mkdirs(newPath(dir));

filesys.close();

}public void mv(String src,String dst) throwsIOException, InterruptedException, URISyntaxException{

Configuration cfg=newConfiguration();//cfg.set("fs.defaultFs", "hdfs://linux1:9000");

FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");

filesys.moveFromLocalFile(new Path(src), newPath(dst));

filesys.close();

}public void delete(String path) throwsIOException, InterruptedException, URISyntaxException{

Configuration cfg=newConfiguration();

FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");

filesys.delete(new Path(path), true);

filesys.close();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值