如何在maven中提供参数 - 对于hadoop WordCount

时间:2013-11-16 09:57:38

标签: java eclipse maven hadoop mapreduce

我在eclipse中正在做Hadoop WordCount.java

我将输入和输出路径作为参数。

我正试图将我的hadoop MR从eclipse juno转换为maven。

我写了pom.xml。但是我应该在哪里加入我的参数?

  • input: /home/sree/myfiles/book.txt
  • output: /home/sree/myfiles/wcout

我的编辑pom.xml

   <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>TryMaven</groupId>
    <artifactId>TryMaven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>

        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.WordCount</mainClass>

                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <arguments>
                        <argument>-Dinput=${input}</argument>
                        <argument>-Doutput=${output}</argument>
                        <mainClass>org.WordCount</mainClass>
                    </arguments>
                    <mainClass>org.WordCount</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <repositories>

        <repository>
            <id>sonatype-nexus-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>


</project>

1 个答案:

答案 0 :(得分:2)

Maven项目是构建工具,迁移到maven之后,您仍然可以像以前一样运行程序。这与maven无关。

Java解决方案

您可以在没有maven的情况下运行WordCount

java -jar target/TryMaven-0.0.1-SNAPSHOT.jar -Dinput=/home/sree/myfiles/book.txt -Doutput=/home/sree/myfiles/wcout

Maven解决方案

对pom.xml的更改

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Dinput=${input}</argument>
            <argument>-Doutput=${output}</argument>

            <argument>-classpath</argument>
            <classpath/>

            <mainClass>org.WordCount</mainClass>
        </arguments>
    </configuration>
</plugin>

使用命令执行

mvn exec:exec -Dinput=/home/sree/myfiles/book.txt -Doutput=/home/sree/myfiles/wcout

您可以直接从Eclipse执行此目标!

Run as - &gt; Maven bulid并使用所需的参数指定目标exec:exec