一、先看集群上的配置,这里设置了文件块副本数为 3
上传一个文件试试
public class ConfigPriority {
private Configuration conf;
private FileSystem fs; @Before
public void init() throws Exception {
// 设置 HADOOP_HOME 环境变量
System.setProperty("hadoop.home.dir", "D:/DevelopTools/hadoop-2.9.2/");
// 日志初始化
BasicConfigurator.configure(); conf = new Configuration();
// 获取 hdfs 客户端对象,指定用户名,避免无权限
fs = FileSystem.get(new URI("hdfs://192.168.8.136:9000"), conf, "root");
} @After
public void close() throws IOException {
fs.close();
} // 文件上传
@Test
public void testCopyFromLocalFile() throws Exception{
fs.copyFromLocalFile(new Path("D://MyFile/Downloads/Writage-1.12.msi"), new Path("/Priority/AAA.msi"));
}
}
二、在资源目录添加 hdfs-site.xml 配置后再上传
<?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>
代码中的上传名字做下改变
三、在代码中指定下配置参数
public class ConfigPriority {
private Configuration conf;
private FileSystem fs; @Before
public void init() throws Exception {
// 设置 HADOOP_HOME 环境变量
System.setProperty("hadoop.home.dir", "D:/DevelopTools/hadoop-2.9.2/");
// 日志初始化
BasicConfigurator.configure(); conf = new Configuration();
conf.set("dfs.replication","2");
// 获取 hdfs 客户端对象,指定用户名,避免无权限
fs = FileSystem.get(new URI("hdfs://192.168.8.136:9000"), conf, "root");
} @After
public void close() throws IOException {
fs.close();
} // 文件上传
@Test
public void testCopyFromLocalFile() throws Exception{
fs.copyFromLocalFile(new Path("D://MyFile/Downloads/Writage-1.12.msi"), new Path("/Priority/CCC.msi"));
}
}