我的博客已迁移至自建服务器:博客传送门,CSDN博客暂时停止,如有机器学习方面的兴趣,欢迎来看一看。
此外目前我在gitHub上准备一些李航的《统计学习方法》的实现算法,目标将书内算法全部手打实现,欢迎参观并打星。GitHib传送门
该部分可网上查阅相关资料
//1.获取文件系统
@Test
public void initHDFS() throws IOException {
// 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(configuration);
//2.打印文件系统到控制台
System.out.println(fs.toString());
fs.close();
}
1.首先需要获取到Hadoop的文件系统,以便对Hadoop进行操作。
2.打印获取到的文件系统
3.控制台有信息输出,说明获取成功
//2.文件上传
@Test
public void CopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
//获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
//上传
fs.copyFromLocalFile(false, new Path("e:/hello.txt"), new Path("/hello1.txt"));
//释放资源
fs.close();
}
//3.文件下载
@Test
public void CopyToLoaclFile() throws IOException, InterruptedException, URISyntaxException{
//获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
//下载文件
fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:\\hello1.txt"), true);
fs.close();
}
//4.创建目录
@Test
public void Mkdirs() throws IOException, InterruptedException, URISyntaxException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
//创建目录
fs.mkdirs(new Path("/test/haha/heihei"));
fs.close();
}
//5.删除文件夹
@Test
public void Delete() throws IOException, InterruptedException, URISyntaxException {
//获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
//删除文件夹
fs.delete(new Path("/test"), true);
//释放资源
fs.close();
}
//6.更改文件名
@Test
public void ReName() throws IOException, InterruptedException, URISyntaxException {
//获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
//更改文件名
fs.rename(new Path("/hello.txt"), new Path("/hello2.txt"));
//释放资源
fs.close();
}
//7.查看文件详情
@Test
public void ListFiles() throws IOException, InterruptedException, URISyntaxException {
//获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
RemoteIterator remoteIterator = fs.listFiles(new Path("/"), true);
while(remoteIterator.hasNext()) {
LocatedFileStatus status = remoteIterator.next();
//文件名
System.out.println(status.getPath().getName());
//文件长度
System.out.println(status.getLen());
//组
System.out.println(status.getGroup());
//副本数
System.out.println(status.getReplication());
//获取块位置信息
BlockLocation[] blockLocations = status.getBlockLocations();
for(BlockLocation bl: blockLocations) {
//偏移量
System.out.println(bl.getOffset());
//副本所在主机
System.out.println(bl.getHosts());
System.out.println(bl.getNames());
}
System.out.println("-------------------------------------------");
}
fs.close();
}
//8.判断是否文件
@Test
public void IsFile() throws IOException, InterruptedException, URISyntaxException {
//获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status : listStatus) {
if(status.isFile()) {
System.out.println("f:" + status.getPath().getName());
}
else {
System.out.println("d:" + status.getPath().getName());
}
}
fs.close();
}
}