HDFS API 操作之目录创建、文件上传、配置优先级

目录创建

@Test
public void testMkdir() throws Exception {

    //1 获取文件系统
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "root");

    //2 上传文件
    fs.mkdirs(new Path("/demo/test"));

    //3 关闭资源
    fs.close();

    System.out.println("~ok~");

}

文件上传

@Test
public void testCopyFromLocalFile() throws Exception {

    //1 获取文件系统
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "root");

    //2 上传文件
    fs.mkdirs(new Path("/demo/test"));

    //3 关闭资源
    fs.close();

    System.out.println("~ok~");

}

-w862

-w1192

参数优先级

在resource中添加配置文件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>

执行文件上传test

-w1208

代码中添加配置

configuration.set("dfs.replication", "2");
@Test
public void testCopyFromLocalFile() throws Exception {

    //1 获取文件系统
    Configuration conf = new Configuration();
    conf.set("dfs.replication", "2");

    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "root");

    //2 上传文件
    fs.copyFromLocalFile(new Path("/Users/ylj/demo/readme.md"),new Path("/demo/test/readme-3.md"));

    //3 关闭资源
    fs.close();

    System.out.println("~ok~");

}

执行文件上传test

-w1185

结论

参数优先级排序:

  1. 客户端代码中设置的值
  2. classpath下的用户自定义配置文件
  3. 服务器的默认配置

猜你喜欢

转载自blog.csdn.net/yljphp/article/details/88958550