Creating a Maven-Based Java Project

Create a Maven-based Java project to write a MapReduce application.

  1. Create a Maven project. For example, create a projected named wordcountjava:
    mvn archetype:generate -DgroupId=org.apache.hadoop.examples -DartifactId=wordcountjava -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    

    This command creates a directory with the name specified by the artifactID parameter (wordcountjava in this example). This directory contains the following items:

    • pom.xml: The Project Object Model (POM) that contains information and configuration details used to build the project.
    • src\main\java\org\apache\hadoop\examples: Contains the application code.
    • src\test\java\org\apache\hadoop\examples: Contains test code.
  2. Update pom.xml with required dependencies:
    1. To add dependencies, add the following text in the <dependencies> section of pom.xml:
      <dependency>
      
      
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-mapreduce-client-common</artifactId>
          <version>3.3.3</version>
          <scope>provided</scope>
      </dependency>
      <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-common</artifactId>
          <version>3.3.3</version>
          <scope>provided</scope>
      
      
      </dependency>
      Note

      The <version> used must match the version of Hadoop in Big Data Service cluster.
  3. Add other required configurations and plugins to pom.xml.

    In the following example, the shaded plugin is added to bundle all jars referred in dependencies (apart from provided scope) along with maven build plugin.

    <build>
    
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
            <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
                </transformer>
            </transformers>
            </configuration>
            <executions>
            <execution>
                <phase>package</phase>
                    <goals>
                    <goal>shade</goal>
                    </goals>
            </execution>
            </executions>
            </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
            <source>1.8</source>
            <target>1.8</target>
            </configuration>
        </plugin>
        </plugins>
    </build>
  4. Save the pom.xml file.