大三上寒假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"
- cd ~/sparkapp
- find .
- /usr/local/sbt/sbt package #打包
- 运行
- /usr/local/spark/bin/spark-submit --class "SimpleApp" ~/sparkapp/target/scala-2.10/simple-project_2.10-1.0.jar
- # 上面命令执行后会输出太多信息,可以不使用上面命令,而使用下面命令查看想要的结果
- /usr/local/spark/bin/spark-submit --class "SimpleApp" ~/sparkapp/target/scala-2.10/simple-project_2.10-1.0.jar 2>&1 | grep "Lines with a:"
10.结果
又卡住了,下载一个小时了多了
无语。