上一篇已经对api做了一个简单的操作,这篇进阶操作一下。 上一篇地址:模块一:Hadoop核心框架(六)—用IDEA创建一个对hadoopAPI简单操作项目
创建hdfsClient2类
为了方便测试使用,我们将Configuration和FileSystem定义了在外面。 用@before和@after来初始化对象和关闭流。
private Configuration configuration;
private FileSystem fileSystem;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
configuration=new Configuration();
fileSystem=FileSystem.get(new URI("hdfs://linux121:9000"),configuration,"root");
}
@After
public void after() throws IOException {
fileSystem.close();
}
测试上传文件
首先hdfs没有文件 我们在resource下创建一个a.txt文件,里面写着hello hdfs。把a.txt文件上传上去,命名为log4j.properties。 代码:
@Test
/**
* @author:yehw
* @date: 2020/7/4:17:41
* @paramType:[]
* @returnType: void
* @description: 测试本地文件复制到服务器
*/
public void testCopyFromLocalFile() throws IOException {
//本地文件路径
Path localPath = new Path("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/log4j.properties");
//上传到服务起的路径
Path serverPath = new Path("/log4j.properties");
//上传
fileSystem.copyFromLocalFile(true,localPath,serverPath);
//上一个方法,默认是自动关闭流,所以可以不需要手动关闭
}
启动测试去网页查看是否上传成功: 可以下载看看,里面内容就hello dfs 注意:这个方法是会把原文件删除
测试下载文件
@Test
/**
* @author:yehw
* @date: 2020/7/4:22:02
* @paramType:[]
* @returnType: void
* @description: 测试从服务在下载文件
*/
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
//本地文件路径
Path localPath = new Path("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/a.txt");
//要下载的服务器的路径
Path serverPath = new Path("/log4j.properties");
//下载
//第一个参数:false;代表下载完后不删除源文件
//第二份参数:服务器要下载的路径
//第三个参数:下载到本地的路径
fileSystem.copyToLocalFile(false,serverPath,localPath);
}
删除文件
@Test
/**
* @author:yehw
* @date: 2020/7/4:22:07
* @paramType:
* @returnType:
* @description: 删除文件
*/
public void testDelete() throws IOException {
//服务器上要删除的文件
Path serverPath = new Path("/log4j.properties");
fileSystem.delete(serverPath,true);
}
去服务器查看,文件是否删除
测试文件信息查看
@Test
/**
* @author:yehw
* @date: 2020/7/4:22:21
* @paramType:[]
* @returnType: void
* @description: 查看文件夹信息
*/
public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
// 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus status = listFiles.next();
// 输出详情
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup());
// 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 获取块存储的主机节点
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------华丽的分割线----------");
}
// 3 关闭资源
fileSystem.close();
}
控制台输出:
a.txt 10 rw-r–r-- supergroup linux123 linux122 linux121 -----------华丽的分割线---------- log4j.properties 442 rw-r–r-- supergroup linux123 linux122 linux121 -----------华丽的分割线----------
测试输入输出流
本地文件上传的到服务器
@Test
/**
* @author:yehw
* @date: 2020/7/4:22:34
* @paramType:
* @returnType:
* @description: 测试输入输出流
*/
public void putFileToHdfs() throws IOException {
//把a.txt文件通过输入流然后输出到服务器根目录下的a_io.txt文件
FileInputStream inputStream=new FileInputStream(new File("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/a.txt"));
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/a_io.txt"));
IOUtils.copyBytes(inputStream,fsDataOutputStream,configuration);
IOUtils.closeStream(inputStream);
IOUtils.closeStream(fsDataOutputStream);
}
服务器查看:
服务器文件下载到本地
本地另去名字是b.txt
@Test
/**
* @author:yehw
* @date: 2020/7/4:22:45
* @paramType:
* @returnType:
* @description: 从服务器通过输入流输入到本地
*/
public void getFiLeFromHdfs() throws IOException {
FSDataInputStream open = fileSystem.open(new Path("/a_io.txt"));
FileOutputStream fileOutputStream = new FileOutputStream(new File("/Users/yehuaiwei/IdeaProjects/client_demo/src/main/resources/b.txt"));
IOUtils.copyBytes(open,fileOutputStream,configuration);
IOUtils.closeStream(open);
IOUtils.closeStream(fileOutputStream);
}
查看文件目录:
seek定位读取
@Test
/**
* @author:yehw
* @date: 2020/7/4:22:52
* @paramType:
* @returnType:
* @description: seek测试
*/
public void seekTest() throws IOException {
FSDataInputStream open = fileSystem.open(new Path("/a_io.txt"));
IOUtils.copyBytes(open, System.out, 4096,false);
//从刚开始再读一遍,这个时候主要要把自动关闭取消
open.seek(0);
IOUtils.copyBytes(open, System.out, 4096, false);
IOUtils.closeStream(open);
}
查看控制台