Windows Eclipse Scala编写WordCount程序:
1)无需启动hadoop,因为我们用的是本地文件。先像原来一样,做一个普通的scala项目和Scala Object。
但这里一定注意版本是2.10.6,因为缺省的不好使。改的方法是:右击项目/properties/Scala Compiler.
2)像spark的java版WordCount项目一模一样导包,什么都一样。(导包的方法和原来普通的java项目一样)
例:5.1
package com
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
object WordCount {
def main(args: Array[String]) {
val conf = new SparkConf();
conf.setAppName("First Spark scala App!");
conf.setMaster("local");
val sc = new SparkContext(conf);
val lines = sc.textFile("E://temp//input//friend.txt", 1);
val words = lines.flatMap { lines => lines.split(" ") };
val pairs = words.map { word => (word, 1) }
val wordCounts = pairs.reduceByKey(_ + _)
wordCounts.foreach(wordNumberPair => println(wordNumberPair._1 + ":" + wordNumberPair._2))
}
}
文章转载自原文:https://blog.csdn.net/qq_44596980/article/details/93383684