package com.xiaoye.hdfsTest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class DFSClientTest {
//1、创建文件夹
@Test
public void mkdirFile() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://xiaoye11:8020");
//创建对象
Configuration configuration = new Configuration();
String user="root";
//获取客户端对象
FileSystem fs= FileSystem.get(uri, configuration,user);
//创建文件夹
fs.mkdirs(new Path("/xiaoyeText"));
//关闭资源
fs.close();
System.out.println("文件夹创建成功");
}
//2、复制上传文件
@Test
public void copyFromLocalFileTest() throws URISyntaxException, IOException, InterruptedException{
URI uri = new URI("hdfs://xiaoye11:8020");
//创建对象
Configuration configuration = new Configuration();
String user="root";
//获取客户端对象
FileSystem fs= FileSystem.get(uri, configuration,user);
Path src = new Path("F://xiaoye_Test//long.txt");
Path dest = new Path("/xiaoyeText/xx.txt");
fs.copyFromLocalFile(src,dest);
//关闭资源
fs.close();
System.out.println("上传成功");
}
//3、下载文件到本地
@Test
public void copyToLocalTest() throws URISyntaxException, IOException, InterruptedException{
URI uri = new URI("hdfs://xiaoye11:8020");
//创建对象
Configuration configuration = new Configuration();
String user="root";
//获取客户端对象
FileSystem fs= FileSystem.get(uri, configuration,user);
Path src = new Path("F://xiaoye_Test");
Path dest = new Path("/xiaoyeText/xiao111.txt");
fs.copyToLocalFile(false,dest,src,true);
//关闭资源
fs.close();
System.out.println("下载成功");
}
//4、删除hadoop上的文件
@Test
public void deleteFileTest() throws URISyntaxException, IOException, InterruptedException{
URI uri = new URI("hdfs://xiaoye11:8020");
//创建对象
Configuration configuration = new Configuration();
String user="root";
//获取客户端对象
FileSystem fs= FileSystem.get(uri, configuration,user);
Path dest = new Path("/xiaoyeText");
fs.delete(dest,true);
//关闭资源
fs.close();
System.out.println("删除成功");
}
//5、新建文件
@Test
public void createFileTest() throws URISyntaxException, IOException, InterruptedException{
URI uri = new URI("hdfs://xiaoye11:8020");
//创建对象
Configuration configuration = new Configuration();
String user="root";
//获取客户端对象
FileSystem fs= FileSystem.get(uri, configuration,user);
Path dest = new Path("/xiaoxiao.txt");
fs.create(dest);
//关闭资源
fs.close();
System.out.println("新建文件成功");
System.out.println("********************hello,hadoop************************");
}
//6、判断是文件还是文件夹
@Test
public void judgeFileType() throws URISyntaxException, IOException, InterruptedException{
URI uri = new URI("hdfs://xiaoye11:8020");
//创建对象
Configuration configuration = new Configuration();
String user="root";
//获取客户端对象
FileSystem fs= FileSystem.get(uri, configuration,user);
Path dest = new Path("/");
FileStatus[] listStatus=fs.listStatus(dest);
for (FileStatus fileType:listStatus){
if (fileType.isFile()) {
System.out.println("文件:" + fileType.getPath().getName());
}else {
System.out.println("文件夹:" + fileType.getPath().getName());
}
}
//关闭资源
fs.close();
}
}
大数据开发系列(四)----HDFS的增删改查代码
最新推荐文章于 2023-03-23 18:17:31 发布