EMR Spark JavaWordCount

EMR Spark JavaWordCount

摘要

EMR Spark JavaWordCount

创建EMR



配置部分

[{"classification":"spark","properties":{"maximizeResourceAllocation":"true"}}]

参数部分Spark-submit options

--deploy-mode cluster 
--master yarn 
--num-executors 1 
--executor-cores 1 
--executor-memory 10g 
--conf spark.yarn.submit.waitAppCompletion=false
--class spark.JavaWordCount

s3输出目录记得要是清空的

手动提交不用网页

spark-submit --deploy-mode cluster --deploy-mode cluster --master yarn --num-executors 1 --executor-cores 1 --executor-memory 10g --conf spark.yarn.submit.waitAppCompletion=false --class  spark.JavaWordCount  s3://imeis/test-zhu/jar/spark-1.0-SNAPSHOT.jar s3://imeis/test-zhu/testinput/ s3://imeis/test-zhu/testoutput/
格式化
spark-submit \
--deploy-mode cluster \
--master yarn \
--num-executors 1 \
--executor-cores 1 \
--executor-memory 10g \
--conf spark.yarn.submit.waitAppCompletion=false \
--class  spark.JavaWordCount  \
s3://imeis/test-zhu/jar/spark-1.0-SNAPSHOT.jar   \
s3://imeis/test-zhu/testinput/   \
s3://imeis/test-zhu/testoutput/

应用程序源码

package spark;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public final class JavaWordCount {

    private static final Pattern SPACE = Pattern.compile(" ");

    public static void main(String[] args) throws Exception {


//      编译
//      mvn package

        if (args.length < 2) {
            System.err.println("Usage: spark-submit --class spark.JavaWordCount spark-1.0-SNAPSHOT.jar <inpath>  <outpath>");
            System.exit(1);
        }

        SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount");
//        sparkConf.setMaster("spark://ip-172-31-244-59.cn-north-1.compute.internal:7077");

//                .set("spark.local.ip", "192.168.1.108")
//                .set("spark.driver.host", "192.168.1.108");

        JavaSparkContext ctx = new JavaSparkContext(sparkConf);
        JavaRDD<String> lines = ctx.textFile(args[0]);


        JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterable<String> call(String s) {
                return Arrays.asList(SPACE.split(s));
            }
        });

        JavaPairRDD<String, Integer> ones = words.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String s) {
                return new Tuple2<String, Integer>(s, 1);
            }
        });

        JavaPairRDD<String, Integer> counts = ones.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer i1, Integer i2) {
                return i1 + i2;
            }
        });

        counts.saveAsTextFile(args[1]);
        List<Tuple2<String, Integer>> output = counts.collect();
        for (Tuple2<?, ?> tuple : output) {
            System.out.println(tuple._1() + ": " + tuple._2());
        }
        ctx.stop();
    }


}

对应的pom文件

<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>spark</groupId>
    <artifactId>spark</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spark</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!--<dependency>-->
        <!--<groupId>org.apache.spark</groupId>-->
        <!--<artifactId>spark-sql_2.10</artifactId>-->
        <!--<version>1.6.1</version>-->
        <!--</dependency>-->
        <!--<dependency>-->
        <!--<groupId>org.apache.spark</groupId>-->
        <!--<artifactId>spark-hive_2.10</artifactId>-->
        <!--<version>1.6.1</version>-->
        <!--</dependency>-->
    </dependencies>


    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>spark.JavaWordCount</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>

                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <!-- Additional configuration. -->
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>utf8</encoding>
                </configuration>
            </plugin>
        </plugins>

    </build>


</project>

注意

stderr中有输出内容,但是并不是错误.只要job可以正常Completed即可.
另外Completed之后立刻去看S3输出目录可能还没有结果,等那么几分钟再刷新一下应该就有了.

code地址

https://coding.net/u/javacore/p/spark-study/git/tree/master/testspark