建立一个maven工程,把src下的java文件夹改为scala或者新添加scala文件夹
项目结构如下:
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>mySpark01</groupId>
<artifactId>mySpark01</artifactId>
<version>0.0.1-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>
<spark.version>1.6.1</spark.version>
<hadoop.version>2.6.4</hadoop.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>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</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.2</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-shade-plugin</artifactId>
<version>2.4.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.lijie.spark.WordCount</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
WordCount类如下:
package cn.touna
import org.apache.spark.{SparkConf, SparkContext}
/**
* User: lijie
* Email:lijiewj51137@touna.cn
* Date:
* Time:
*/
class WordCount {
}
object WordCount {
def main(args: Array[String]): Unit = {
var arg = Array("hdfs://lijie:9000/sz/access.log.fensi", "hdfs://lijie:9000/sz/out")
// val conf = new SparkConf().setMaster("spark://lijie:7077").setAppName("wd")
val conf = new SparkConf().setMaster("local").setAppName("wd")
val sc = new SparkContext(conf)
println(1111)
// sc.textFile(arg(0)).flatMap(_.split(" ")).map((_, 1)).aggregateByKey(0)(_ + _, _ + _).sortBy(_._2, true).saveAsTextFile(arg(1))
// sc.textFile(arg(0)).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).sortBy(_._2, true).saveAsTextFile(arg(1))
// sc.textFile(arg(0)).flatMap(_.split(" ")).map((_,1)).combineByKey(x => x,(a:Int,b:Int) => a+b, (c:Int,d:Int) => c+d).sortBy(_._2,true).saveAsTextFile(arg(1))
sc.textFile(arg(0)).flatMap(_.split(" ")).map((_, 1)).foldByKey(0)(_ + _).sortBy(_._2, true).saveAsTextFile(arg(1))
println(2222)
sc.stop
}
}
执行打包操作
提交到服务器上面执行
执行命令如下:
[hadoop@cdhnode1 ~]$ /home/hadoop/app/spark-1.3.1-bin-hadoop2.6/bin/spark-submit --master spark://cdhnode1:7077 --class com.lijie.spark.WorldCount --executor-memory 512m --total-executor-cores 2 ./original-mySpark01-0.0.1-SNAPSHOT.jar
执行结果: