省流:
低 => 高
hdfs-default.xml => hdfs-site.xml => 项目资源目录下的hdfs-site.xml => 代码里的配置
package com.bigdata.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* 客户端代码常用套路
* 1、获取客户端操作对象
* 2、执行相关的操作命令
* 3、关闭资源
* HDFS ZooKeeper
*/
public class HdfsClient {
private FileSystem fileSystem;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
// 连接的集群和地址,客户端和namenode打交道,执行相关的操作
URI uri = new URI("hdfs://hadoop102:8020");
// 创建一个配置文件
Configuration configuration = new Configuration();
configuration.set("dfs.replication","2");
// 用户
String user = "mingyu";
// 获取配置文件
fileSystem = FileSystem.get(uri, configuration,user);
}
@After
public void close() throws IOException {
// 关闭资源
fileSystem.close();
}
@Test
public void testClient() throws URISyntaxException, IOException, InterruptedException {
// 创建一个文件夹
fileSystem.mkdirs(new Path("/xiyou/huaguoshan/xiaohouzi"));
}
/**
* 参数优先级
* hdfs-default.xml => hdfs-site.xml => 项目资源目录下的hdfs-site.xml => 代码里的配置
*
* @throws IOException
*/
@Test
public void testPut() throws IOException {
// 上传文件
// para1:是否删除源文件,para2:是否覆盖写入,para3:源路径,para4:目标路径
fileSystem.copyFromLocalFile(false,true,new Path("E:\\sunwukong.txt"),new Path("hdfs://hadoop102/xiyou/huaguoshan/"));
}
}