Quick Start: https://spark.apache.org/docs/latest/quick-start.htmlhtml
scala> val textFile = spark.read.textFile("README.md") # 構建一個Dataset textFile: org.apache.spark.sql.Dataset[String] = [value: string] scala> textFile.count() # Dataset的簡單計算 res0: Long = 104 scala> val linesWithSpark = textFile.filter(line => line.contain("Spark")) # 由現有Dataset生成新Dataset res1: org.apache.spark.sql.Dataset[String] = [value: string] # 等價於: # res1 = new Dataset() # for line in textFile: # if line.contain("Spark"): # res1.append(line) # linesWithSpark = res1 scala> linesWithSpark.count() res2: Long = 19 # 能夠將多個操做串行起來 scala> textFile.filter(line => line.contain("Spark")).count() res3: Long = 19
進一步的Dataset分析:java
scala> textFile.map(line => line.split(" ").size).reduce((a,b) => if (a > b) a else b) res12: Int = 16 # 其實map和reduce就是兩個普通的算子,不要被MapReduce中一個map配一個reduce、先map後reduce的思想所束縛 # map算子就是對Dataset的元素X計算fun(X),而且將全部f(X)做爲新的Dataset返回 # reduce算子其實就是經過兩兩計算fun(X,Y)=Z,將Dataset中的全部元素歸約爲1個值 # 也能夠引入庫進行計算 scala> import java.lang.Math import java.lang.Math scala> textFile.map(line => line.split(" ").size).reduce((a, b) => Math.max(a, b)) res14: Int = 16 # 還可使用其餘算子 scala> val wordCounts = textFile.flatMap(line => line.split(" ")).groupByKey(identity).count() # flatMap算子也是對Dataset的每一個元素X執行fun(X)=Y,只不過map的res是 # res.append(Y),如[[Y11, Y12], [Y21, Y22]],結果按元素區分 # 而flatMap是 # res += Y,如[Y11, Y12, Y21, Y22],各元素結果合在一塊兒 # groupByKey算子將Dataset的元素X做爲參數傳入進行計算f(X),並以f(X)做爲key進行分組,返回值爲KeyValueGroupedDataset類型 # 形式相似於(key: k; value: X1, X2, ...),不過KeyValueGroupedDataset不是一個Dataset,value列表也不是一個array # 注意:這裏的textFile和textFile.flatMap都是Dataset,不是RDD,groupByKey()中能夠傳func;若是以sc.textFile()方法讀文件,獲得的是RDD,groupByKey()中間不能傳func # identity就是函數 x => x,即返回自身的函數 # KeyValueGroupedDataset的count()方法返回(key, len(value))列表,結果是Dataset類型 scala> wordCounts.collect() res37: Array[(String, Long)] = Array((online,1), (graphs,1), ... # collect操做:將分佈式存儲在集羣上的RDD/Dataset中的全部數據都獲取到driver端
數據的cache:sql
scala> linesWithSpark.cache() # in-memory cache,讓數據在分佈式內存中緩存 res38: linesWithSpark.type = [value: string] scala> linesWithSpark.count() res41: Long = 19
import org.apache.spark.sql.SparkSession object SimpleApp { def main(args: Array[String]) { val logFile = "/Users/dxm/work-space/spark-2.4.5-bin-hadoop2.7/README.md" val spark = SparkSession.builder.appName("Simple Application").getOrCreate() val logData = spark.read.textFile(logFile).cache() val numAs = logData.filter(line => line.contains("a")).count() # 包含字母a的行數 val numBs = logData.filter(line => line.contains("b")).count() # 包含字母b的行數 println(s"Lines with a: $numAs, Lines with b: $numBs") spark.stop() } }
2)編寫sbt依賴文件build.sbtshell
name := "Simple Application" version := "1.0" scalaVersion := "2.12.10" libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.4.5"
其中,"org.apache.spark" %% "spark-sql" % "2.4.5"這類庫名能夠在網上查到,例如https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.10/1.0.0apache
$ find . . ./build.sbt ./src ./src/main ./src/main/scala ./src/main/scala/SimpleApp.scala
sbt目錄格式要求見官方文檔 https://www.scala-sbt.org/1.x/docs/Directories.html編程
src/ main/ resources/ <files to include in main jar here> scala/ <main Scala sources> scala-2.12/ <main Scala 2.12 specific sources> java/ <main Java sources> test/ resources <files to include in test jar here> scala/ <test Scala sources> scala-2.12/ <test Scala 2.12 specific sources> java/ <test Java sources>
使用sbt打包緩存
# 打包 $ sbt package ... [success] Total time: 97 s (01:37), completed 2020-6-10 10:28:24 # jar包位於 target/scala-2.12/simple-application_2.12-1.0.jar
4)提交併執行Spark任務app
$ bin/spark-submit --class "SimpleApp" --master spark://xxx:7077 ../scala-tests/SimpleApp/target/scala-2.12/simple-application_2.12-1.0.jar # 報錯:Caused by: java.lang.ClassNotFoundException: scala.runtime.LambdaDeserialize # 參考:https://stackoverflow.com/questions/47172122/classnotfoundexception-scala-runtime-lambdadeserialize-when-spark-submit # 這是spark版本和scala版本不匹配致使的
查詢spark所使用的scala的版本maven
$ bin/spark-shell --master spark://xxx:7077 scala> util.Properties.versionString res0: String = version 2.11.12
$ bin/spark-submit --class "SimpleApp" --master spark://xxx:7077 ../scala-tests/SimpleApp/target/scala-2.11/simple-application_2.11-1.0.jar Lines with a: 61, Lines with b: 30