大三上寒假15天--第3天
学习于大数据原理与应用 第十六章 Spark 学习指南
三、独立应用程序编程
2.Scala应用程序代码
- cd ~ # 进入用户主文件夹
- mkdir ./sparkapp # 创建应用程序根目录
- mkdir -p ./sparkapp/src/main/scala # 创建所需的文件夹结构
vim ./sparkapp/src/main/scala/SimpleApp.scala #在 ./sparkapp/src/main/scala 下建立一个名为 SimpleApp.scala 的文件,并添加如下代码
/* SimpleApp.scala */ import org.apache.spark.SparkContext import org.apache.spark.SparkContext._ import org.apache.spark.SparkConf object SimpleApp { def main(args: Array[String]) { val logFile = "file:///usr/local/spark/README.md" // Should be some file on your system val conf = new SparkConf().setAppName("Simple Application") val sc = new SparkContext(conf) val logData = sc.textFile(logFile, 2).cache() val numAs = logData.filter(line => line.contains("a")).count() val numBs = logData.filter(line => line.contains("b")).count() println("Lines with a: %s, Lines with b: %s".format(numAs, numBs)) } }
如图:
- vim ./sparkapp/simple.sbt #./sparkapp 中新建文件 simple.sbt,并添加如下信息(声明该独立应用程序的信息以及与 Spark 的依赖关系)
name := "Simple Project" version := "1.0" scalaVersion := "2.11.12" libraryDependencies += "org.apache.spark" %% "spark-core" % "2.4.4"
相关推荐
86417413 2020-11-25
83206733 2020-11-19
86276537 2020-11-19
83266337 2020-11-19
86256434 2020-11-17
zhouboxiao 2020-11-16
rise 2020-11-22
sssdssxss 2020-11-20
windle 2020-11-10
孙雪峰 2020-10-30
85477104 2020-11-17
xfcyhades 2020-11-20
liuxudong00 2020-11-19
cheidou 2020-11-19
gunhunti 2020-09-25
csdnYF 2020-11-15
达观数据 2020-11-11
playlinuxxx 2020-11-11