-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name,age | ||
Maria,93 | ||
John,24 | ||
Peter,19 | ||
Cassandra,46 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/scala/com/felstar/restfulzio/spark/SparkApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.felstar.restfulzio.spark | ||
|
||
import zio._ | ||
import zio.http._ | ||
import zio.http.model.Method | ||
import org.apache.spark.sql.Row | ||
import zio.spark.experimental | ||
import zio.spark.experimental.Pipeline | ||
import zio.spark.parameter._ | ||
import zio.spark.rdd.RDD | ||
import zio.spark.sql._ | ||
import zio.spark.sql.implicits._ | ||
|
||
/** An http app that: | ||
* - Accepts a `Request` and returns a `Response` | ||
* - Does not fail | ||
* - Uses a String for the env, for the webapp root | ||
*/ | ||
object SparkApp { | ||
|
||
import zio.spark.sql.TryAnalysis.syntax.throwAnalysisException | ||
|
||
final case class Person(name: String, age: Int) | ||
|
||
val transform: DataFrame => Dataset[Person] = _.as[Person] | ||
|
||
val headOption: Dataset[Person] => Task[Option[Person]] = _.headOption | ||
|
||
val csv: SIO[DataFrame] = SparkSession.read | ||
.schema[Person] | ||
.withHeader | ||
.csv("src/main/resources/data.csv") | ||
|
||
val buildsbt: SIO[Dataset[String]] = SparkSession.read.textFile("build.sbt") | ||
|
||
def wordCount(inputDs: Dataset[String]): RDD[(String, Int)] = | ||
inputDs | ||
.flatMap(line => line.trim.split(" ")) | ||
.flatMap(word => word.split('.')) | ||
.map(_.replaceAll("[^a-zA-Z]", "")) | ||
.filter(_.length > 1) | ||
.map(word => (word, 1)) | ||
.rdd | ||
.reduceByKey(_ + _) | ||
|
||
val wordCountZIO: ZIO[SparkSession, Throwable, Seq[(String, Int)]] = | ||
for { | ||
words <- buildsbt.map(wordCount).flatMap(_.collect) | ||
mostUsedWords = words.sortBy(_._2).reverse | ||
} yield mostUsedWords | ||
|
||
val jobZIO: ZIO[SparkSession, Throwable, String] = | ||
for { | ||
somePeople <- experimental.Pipeline(csv, transform, headOption).run | ||
st = somePeople | ||
.map(p => s"The first person's name is ${p.name}.") | ||
.getOrElse("Nobody there") | ||
} yield st | ||
|
||
val allOutput: Dataset[Person] => Task[Iterator[Person]] = _.toLocalIterator | ||
|
||
val allZIO: ZIO[SparkSession, Throwable, String] = | ||
for { | ||
people <- experimental.Pipeline(csv, transform, allOutput).run | ||
st = people.mkString(",") | ||
} yield st | ||
|
||
def filterByName(name: String): DataFrame => Dataset[Person] = | ||
_.as[Person].filter(_.name == name) | ||
|
||
val byNameZIO: ZIO[SparkSession with String, Throwable, String] = | ||
for { | ||
name <- ZIO.service[String] | ||
people <- experimental.Pipeline(csv, filterByName(name), allOutput).run | ||
st = people.mkString(",") | ||
} yield st | ||
|
||
def apply(): Http[SparkSession, Throwable, Request, Response] = | ||
Http.fromZIO(ZIO.service[SparkSession]).flatMap { ss => | ||
val ssLayer = ZLayer.succeed(ss) | ||
Http.collectZIO[Request] { | ||
case Method.GET -> !! / "spark" => | ||
for { | ||
st <- allZIO.provide(ssLayer) | ||
_ <- ZIO.logInfo(st) | ||
} yield Response.text(st) | ||
case Method.GET -> !! / "spark" / "wordcount" => | ||
for { | ||
mostUsedWords <- wordCountZIO.provide(ssLayer) | ||
_ <- ZIO.logInfo(mostUsedWords.toString) | ||
} yield Response.text(mostUsedWords.mkString("\n")) | ||
case Method.GET -> !! / "spark" / "person" / name => | ||
for { | ||
st <- byNameZIO.provide(ssLayer, ZLayer.succeed(name)) | ||
_ <- ZIO.logInfo(st) | ||
} yield Response.text(st) | ||
case Method.GET -> !! / "spark" / "job" => | ||
for { | ||
st <- jobZIO.provide(ssLayer) | ||
_ <- ZIO.logInfo(st) | ||
} yield Response.text(st) | ||
} | ||
} | ||
} |