HDFS API编程:第一个应用程序的开发:创建一个目录

使用java api操作HDFS文件系统创建一个目录:

  • 1)创建Configuration
  • 2)获取FileSystem
  • 3)一系列HDFS API操作
package com.haglife.bigdata.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

/**
* 使用java api操作HDFS文件系统:
* 1)创建Configuration
* 2)获取FileSystem
* 3)HDFS API操作
*/
public class HDFSApp {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        // 操作HDFS文件系统入口
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://masternode:8020"), configuration, "root");

        Path path = new Path("/hdfsapi/test");
        boolean result = fileSystem.mkdirs(path);
        System.out.println(result);
    }

}

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.nmj.com</groupId>
    <artifactId>HDFS _API编程_第一个应用程序</artifactId>
    <version>1.0</version>

    <properties>
        <!--定义Hadoop版本-->
        <hadoop.version>2.6.0-cdh5.14.2</hadoop.version>
    </properties>

    <repositories>
        <!--引入cdh的仓库-->
        <repository>
            <id>cdh</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        </repository>

    </repositories>

    <dependencies>
        <!--添加hadoop的依赖包-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <!--添加junit的依赖包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
        </dependency>

    </dependencies>

</project>

你可能感兴趣的:(大数据,hdfs,hadoop,大数据,java)