教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Maven編譯Spark程序

更新時間:2015年12月29日15時10分 來源:傳智播客云計算學(xué)科 瀏覽次數(shù):


在IDEA中編寫Spark入門級程序WordCount
Spark是用Scala語言開發(fā)的,目前對Scala語言支持較好的是IDEA的插件,這里我們編寫一個Spark入門級程序,然后用Maven編譯成jar包,然后提交到集群。
1.創(chuàng)建一個項目,利用Maven來管理jar包的依賴。
 


2.選擇Maven項目,然后點擊next
 

3.填寫maven的GAV,然后點擊next
 

4.填寫項目名稱,然后點擊finish
 

5.創(chuàng)建好maven項目后,點擊Enable Auto-Import
 

6.配置Maven的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>cn.itcast.spark</groupId>
    <artifactId>spark-mvn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <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>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

7.將src/main/java和src/test/java分別修改成src/main/scala和src/test/scala,與pom.xml中的配置保持一致
 
 

8.新建一個scala class,類型為Object
 

9.編寫spark程序
package cn.itcast.spark

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

object WordCount {
  def main(args: Array[String]) {
    //創(chuàng)建SparkConf()并設(shè)置App名稱
    val conf = new SparkConf().setAppName("WC")
    //創(chuàng)建SparkContext,該對象是提交spark App的入口
    val sc = new SparkContext(conf)
    //使用sc創(chuàng)建RDD并執(zhí)行相應(yīng)的transformation和action
    sc.textFile(args(0)).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_, 1).sortBy(_._2, false).saveAsTextFile(args(1))
    //停止sc,結(jié)束該任務(wù)
    sc.stop()
  }
}
點擊idea右側(cè)的Maven Project選項,再點擊Lifecycle,選擇clean和package,然后點擊Run Maven Build
 

10.選擇編譯成功的jar包,并將該jar上傳到Spark集群中的某個節(jié)點上
 

11.首先啟動hdfs和Spark集群
啟動hdfs
/usr/local/hadoop-2.6.1/sbin/start-dfs.sh
啟動spark
/usr/local/spark-1.5.2-bin-hadoop2.6/sbin/start-all.sh

12.使用spark-submit命令提交Spark應(yīng)用(注意參數(shù)的順序)
/usr/local/spark-1.5.2-bin-hadoop2.6/bin/spark-submit \
--class cn.itcast.spark.WordCount \
--master spark://node1.itcast.cn:7077 \
--executor-memory 2G \
--total-executor-cores 4 \
/root/spark-mvn-1.0-SNAPSHOT.jar \
hdfs://node1.itcast.cn:9000/words.txt \
hdfs://node1.itcast.cn:9000/out

查看程序執(zhí)行結(jié)果
hdfs dfs -cat hdfs://node1.itcast.cn:9000/out/part-00000
(hello,6)
(tom,3)
(kitty,2)
(jerry,1)
0 分享到:
和我們在線交談!