HDFS, Java操作API

使用API之前需要在window部署hadoop编译之后的依赖环境,增加环境变量,然后重启

这种操作性的框架API是非常简单的,都大体上就是:

  • 拿到对象
  • 进行操作
  • 关闭对象

此处给出源码,应该非常好理解,junit的@before和@after非常好用,是一种类似代理的实现,他们会在@test之前和之后分别得到运行,可以用来做对象初始化以及关闭对象。


publicclassHdfsClient{

privateConfigurationcfg;

privateFileSystemfs;



@Before

publicvoidbefore()throwsURISyntaxException,IOException,InterruptedException{

cfg=newConfiguration();

cfg.set("dfs.replication","2");

cfg.set("dfs.blocksize","67108864");

fs=FileSystem.get(newURI("hdfs://hadoop101:8020"),cfg,"kdlin");

}



@After

publicvoidafter()throwsIOException{

fs.close();

}



@Test

publicvoidtestMkdirs()throwsIOException,InterruptedException,URISyntaxException{

//fs.mkdirs(newPath("/1108/daxian/banzhang"));

fs.copyFromLocalFile(newPath("E:\\BaiduNetdiskDownload\\04,Hadoop\\Day02\\1_出了问题看日志.mp4"),newPath("/home/kdlin/tmp/xyz.mp4"));



}



@Test

publicvoidtestListFiles()throwsIOException{

RemoteIterator<LocatedFileStatus>listFile=fs.listFiles(newPath("/"),true);

while(listFile.hasNext()){

LocatedFileStatusstatus=listFile.next();

System.out.print(status.getPath()+"");

System.out.print(status.getLen()+"");

System.out.print(status.getPermission()+"");

System.out.print(status.getGroup()+"");

System.out.println();

}

}



@Test

publicvoidtestMkdir()throwsIOException{

fs.mkdirs(newPath("/home/kdlin/tmp"));

}



}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值