IDEA+Scala +Spark实现wordCount单词计数-上
一、新建一个Scala的object单例对象,修改pom文件
(1)下面文章可以帮助参考安装 IDEA 和 新建一个Scala程序。
(2)pom文件
<?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>com.mcb.scala02</groupId> <artifactId>scala02</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <encoding>UTF-8</encoding> <scala.version>2.10.5</scala.version> <spark.version>1.6.3</spark.version> <hadoop.version>2.7.5</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.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>day05.SparkWordCount</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
二、Scala 代码
package day05 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} import scala.collection.mutable object SparkWordCount { def main(args: Array[String]): Unit = { //配置信息类 //1,setAppName(任务名称) setMaster(表示开启多少个线程运行) val conf: SparkConf = new SparkConf().setAppName("SparkWordCount").setMaster("local[*]") //上下文对象 val sc: SparkContext = new SparkContext(conf) //读取数据(数据通过数组 args进入) val lines: RDD[String] = sc.textFile(args(0)) //处理数据 val map01: RDD[(String, Int)] = lines.flatMap(_.split(" ")).map((_,1)) val wordCount: RDD[(String, Int)] = map01.reduceByKey(_+_).sortBy(_._2,false) val wcToBuffer: mutable.Buffer[(String, Int)] = wordCount.collect().toBuffer println(wcToBuffer) sc.stop() } }
三、在服务器上面启动Hadoop的hdfs和spark(我这儿启动的hdfs的高可用)
文章链接点击:
(1)分布式系统详解--框架(Hadoop-HDFS的HA搭建及测试)
(2)Spark-集群安装、部署、启动、测试(1.6.3)稳定版
3.1 查看Jps(三台,其中centos01 为namenode,centos02是namenode,MyLinux是datanode)
3.2 web ui看一下hdfs 内部文件
(1)web ui 显示图
(2)查看文件内容(三个文件均问以空格分割的单词。)
3.3 IDEA 配置(传参args)
(1)点击 右上角Edit Configurations
(2)添加application,名称叫做SparkWordCount
3.4 运行结果(读取并运行成功)~~~