使用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"));
}
}