package com.atguigu.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
public class HdfsClientDemo {
private Configuration conf;
private FileSystem fs;
//初始化方法,创建客户端对象
@Before
public void init() throws IOException, InterruptedException {
//1.创建客户端对象 因为是一个文件系统 创建文件系统对象
//参数第一个url
//参数第二个conf
//参数第三个user
//URI uri = new URI("hdfs://hadoop102:8020");
URI uri = URI.create("hdfs://hadoop102:8020");
//创建conf对象
conf = new Configuration();
//conf.set("dfs.replication","2");//修改副本数,用来测试参数优先级
fs = FileSystem.get(uri, conf,"atguigu");
}
//关闭客户端对象
@After
public void close() throws IOException {
fs.close();
}
//创建文件夹
@Test
public void mkdirs() throws IOException {
fs.mkdirs(new Path("/java1"));
}
//上传文件
@Test
public void put() throws IOException {
//参数解读
//1.boolean delSrc 是否删除源文件
//2.boolean overwrite 是否覆盖目标文件
//3.Path src 上传文件路径 本地文件
//4.Path dst 目标路径
fs.copyFromLocalFile(false,true,new Path("D:\\develop\\input\\word.txt"),new Path("/"));
}
//下载文件
//参数解读
//1.boolean delSrc 是否删除源文件
//2.Path src 下载文件路径 本地文件
//3.Path dst 指将文件下载到的路径
//4.useRawLocalFileSystem 是否开启文件校验
@Test
public void downLoad() throws IOException {
fs.copyToLocalFile(false,new Path("/sanguo/shuguo.txt"),new Path("D:\\develop\\input"),false);
}
//文件更名和移动
@Test
public void rename() throws IOException {
fs.rename(new Path("/sanguo/wuguo.txt"),new Path("/weiguo/wu.txt"));
}
//删除文件和目录
@Test
public void delete() throws IOException {
fs.delete(new Path("/java1"),true);
}
//文件详情查看,显示是文件还是文件夹
@Test
public void fileOrDir() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if(fileStatus.isFile()){
System.out.println("文件"+fileStatus.getPath());
}else{
System.out.println("目录"+fileStatus.getPath());
}
}
}
public void fileOrDir(Path path) throws IOException {
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
if(fileStatus.isFile()){
System.out.println("文件"+fileStatus.getPath());
}else{
System.out.println("目录"+fileStatus.getPath());
fileOrDir(fileStatus.getPath());
}
}
}
@Test
public void testFileOrDir() throws IOException {
fileOrDir(new Path("/"));
}
//基于io的上传
public void putByIo() throws IOException {
//创建本地文件系统输入流
FileInputStream fis = new FileInputStream("D:\\develop\\input\\hello.txt");
//创建hdfs文件输出流
FSDataOutputStream fdos = fs.create(new Path("/input/hello.txt"));
//流的对拷
IOUtils.copyBytes(fis,fdos,conf);
//流的关闭
IOUtils.closeStreams(fdos,fis);
}
//基于io的下载
public void getByIo() throws IOException {
//创建hdfs文件输入流
FSDataInputStream fdis = fs.open(new Path("/input/hello.txt"));
//创建本地文件输出流
FileOutputStream fos = new FileOutputStream("D:\\develop\\input\\hello.txt");
//流的对拷
IOUtils.copyBytes(fdis,fos,conf);
//流的关闭
IOUtils.closeStreams(fdis,fos);
}
}
HDFS文件上传(测试参数优先级)
1)编写源代码
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
// 2 上传文件
fs.copyFromLocalFile(new Path("d:/sunwukong.txt"), new Path("/xiyou/huaguoshan"));
// 3 关闭资源
fs.close();
}
2)将hdfs-site.xml拷贝到项目的resources资源目录下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
3)参数优先级
参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的自定义配置(xxx-site.xml) >(4)服务器的默认配置(xxx-default.xml)
本文通过一个测试案例介绍了HDFS文件上传的过程,详细讲解了如何在Java代码中设置配置参数,并探讨了参数优先级:代码中设置的值 > ClassPath下用户自定义配置文件 > 服务器自定义配置 > 服务器默认配置。
416

被折叠的 条评论
为什么被折叠?



