在IDEA中編寫Spark入門級(jí)程序WordCount
Spark是用Scala語(yǔ)言開發(fā)的,目前對(duì)Scala語(yǔ)言支持較好的是IDEA的插件,這里我們編寫一個(gè)Spark入門級(jí)程序,然后用Maven編譯成jar包,然后提交到集群。
1.創(chuàng)建一個(gè)項(xiàng)目,利用Maven來(lái)管理jar包的依賴。
2.選擇Maven項(xiàng)目,然后點(diǎn)擊next
3.填寫maven的GAV,然后點(diǎn)擊next
4.填寫項(xiàng)目名稱,然后點(diǎn)擊finish
5.創(chuàng)建好maven項(xiàng)目后,點(diǎn)擊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.新建一個(gè)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,該對(duì)象是提交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()
}
}
點(diǎn)擊idea右側(cè)的Maven Project選項(xiàng),再點(diǎn)擊Lifecycle,選擇clean和package,然后點(diǎn)擊Run Maven Build
10.選擇編譯成功的jar包,并將該jar上傳到Spark集群中的某個(gè)節(jié)點(diǎn)上
11.首先啟動(dòng)hdfs和Spark集群
啟動(dòng)hdfs
/usr/local/hadoop-2.6.1/sbin/start-dfs.sh
啟動(dòng)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)