spark入门程序WordCount

1.依赖和插件

<properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.10.6</scala.version>
        <scala.compat.version>2.10</scala.compat.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>1.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.10</artifactId>
            <version>1.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-make:transitive</arg>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.xt.spark.WordCount</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.主程序:WordCount.scala

package com.xt.spark

import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by XT on 2018/4/15.
  */
object WordCount {

  def main(args: Array[String]): Unit = {
    //创建sparkconf对象并设置App的名称
    val conf = new SparkConf().setAppName("WC")
    //创建sparkcontext对象,改对象是提交spark app的入口
    val sc = new SparkContext(conf)
    //wordcount操作
    sc.textFile(args(0)).flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_,1).sortBy(_._2,false).saveAsTextFile(args(1))
    //停止sc,结束该任务
    sc.stop()

  }

}

3.使用maven打jar包

选择clean和package选项,然后run maven build。

这里写图片描述
生成jar包
这里写图片描述

4.使用spark集群运行Jar包

前提:启动hdfs和spark集群
使用spark-submit命令提交Spark应用(注意参数的顺序)

./spark-1.5.2-bin-hadoop2.6/bin/spark-submit \
--class com.xt.spark.WordCount \
--master spark://hadoop1:7077,hadoop2:7077 \
--executor-memory 2G \
--total-executor-cores 4 \
/root/spark-1.0-SNAPSHOT.jar \
hdfs://myservice/words.txt \
hdfs://myservice:9000/out

5.查看执行结果

hdfs dfs -cat /out/part-00000
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_35852328/article/details/79947537
文章标签: spark
个人分类: spark
上一篇Spark安装(高可用版)
下一篇Spark在Hadoop 高可用模式下读写hdfs
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭