Scala and INTELLIJ IDEA
根据Scala官方文档指示安装INTELLIJ的Scala插件。
然后就可以愉快地在INTELLIJ IDEA里面书写Scala代码了。
其实本文所指spark集成开发环境就是scala开发环境。
有了这个开发环境我们只需以下几步:
添加spark依赖
在项目下的build.sbt里面添加对spark的依赖:
注意最后一行
name := "scalaAllIn"
version := "0.1"
scalaVersion := "2.11.12"
libraryDependencies ++= Seq("org.scalatest" %% "scalatest" % "3.0.4" % Test ,
"org.apache.spark" %% "spark-core" % "2.2.1")
利用SparkContext编写一个文本去重的work
example.DistinctWords
package example
import org.apache.spark.{SparkConf, SparkContext}
object DistinctWords {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setMaster("local").setAppName("words distinct")
val sc = new SparkContext(conf)
// Load our input data.
val input = sc.textFile(args(0))
// Split it up into words.
val words = input.flatMap(line => line.split(" "))
// 去重
val res = words.distinct()
// Save the word count back out to a text file, causing evaluation.
res.saveAsTextFile(args(1))
}
}
设置程序的参数并运行
即设置input的位置和output的位置
然后就可以运行了
本文示例文件和结果
示例文件:
text1
apple apple banana orange
text2
扫描二维码关注公众号,回复:
566664 查看本文章
banana orange meat
用spark的api进行去重后,结果为:
→ cat *
orange
apple
banana
meat
where to go
通常spark RDD的数据来自HDFS,而不像本例是来自本地文件,这个好说,输入路径和输出路径改成hdfs的url即可;
spark的任务在生产环境下是提交给集群的,而不像本例是提交给单机的;
下一步应深入学习如何使用spark和scala来处理、分析数据或者完成机器学习任务。