forked from byzer-org/byzer-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
2d1d8a3
commit 25c3da6
Showing
11 changed files
with
403 additions
and
21 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
38 changes: 38 additions & 0 deletions
38
src/main/java/streaming/common/DefaultShortNameMapping.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,38 @@ | ||
package streaming.common | ||
|
||
import serviceframework.dispatcher.ShortNameMapping | ||
|
||
/** | ||
* 11/21/16 WilliamZhu([email protected]) | ||
*/ | ||
|
||
class DefaultShortNameMapping extends ShortNameMapping { | ||
private val compositorNameMap: Map[String, String] = Map[String, String]( | ||
"spark" -> "streaming.core.strategy.SparkStreamingStrategy", | ||
|
||
"batch.source" -> "streaming.core.compositor.spark.source.SQLSourceCompositor", | ||
"batch.sql" -> "streaming.core.compositor.spark.transformation.SQLCompositor", | ||
"batch.table" -> "streaming.core.compositor.spark.transformation.JSONTableCompositor", | ||
"batch.refTable" -> "streaming.core.compositor.spark.transformation.JSONRefTableCompositor", | ||
|
||
"stream.source.kafka" -> "streaming.core.compositor.spark.streaming.source.KafkaStreamingCompositor", | ||
"stream.sql" -> "streaming.core.compositor.spark.streaming.transformation.SQLCompositor", | ||
"stream.table" -> "streaming.core.compositor.spark.streaming.transformation.JSONTableCompositor", | ||
"stream.refTable" -> "streaming.core.compositor.spark.streaming.transformation.JSONRefTableCompositor", | ||
|
||
"ss.source" -> "streaming.core.compositor.spark.ss.source.SQLSourceCompositor", | ||
"ss.source.mock" -> "streaming.core.compositor.spark.ss.source.MockSQLSourceCompositor", | ||
"ss.sql" -> "streaming.core.compositor.spark.transformation.SQLCompositor", | ||
"ss.table" -> "streaming.core.compositor.spark.transformation.JSONTableCompositor", | ||
"ss.refTable" -> "streaming.core.compositor.spark.transformation.JSONRefTableCompositor", | ||
"ss.output" -> "streaming.core.compositor.spark.ss.output.SQLOutputCompositor" | ||
) | ||
|
||
override def forName(shortName: String): String = { | ||
if (compositorNameMap.contains(shortName)) { | ||
compositorNameMap(shortName) | ||
} else { | ||
shortName | ||
} | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
src/main/java/streaming/core/compositor/spark/ss/output/SQLOutputCompositor.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,84 @@ | ||
package streaming.core.compositor.spark.ss.output | ||
|
||
import java.util | ||
import java.util.concurrent.TimeUnit | ||
|
||
import org.apache.log4j.Logger | ||
import org.apache.spark.sql.DataFrame | ||
import org.apache.spark.sql.streaming.ProcessingTime | ||
import serviceframework.dispatcher.{Compositor, Processor, Strategy} | ||
import streaming.core.compositor.spark.streaming.CompositorHelper | ||
import streaming.core.strategy.ParamsValidator | ||
|
||
import scala.collection.JavaConversions._ | ||
|
||
/** | ||
* 5/11/16 WilliamZhu([email protected]) | ||
*/ | ||
class SQLOutputCompositor[T] extends Compositor[T] with CompositorHelper with ParamsValidator { | ||
|
||
private var _configParams: util.List[util.Map[Any, Any]] = _ | ||
val logger = Logger.getLogger(classOf[SQLOutputCompositor[T]].getName) | ||
|
||
|
||
override def initialize(typeFilters: util.List[String], configParams: util.List[util.Map[Any, Any]]): Unit = { | ||
this._configParams = configParams | ||
} | ||
|
||
def path = { | ||
config[String]("path", _configParams) | ||
} | ||
|
||
def format = { | ||
config[String]("format", _configParams) | ||
} | ||
|
||
def mode = { | ||
config[String]("mode", _configParams) | ||
} | ||
|
||
def cfg = { | ||
val _cfg = _configParams(0).map(f => (f._1.asInstanceOf[String], f._2.asInstanceOf[String])).toMap | ||
_cfg - "path" - "mode" - "format" | ||
} | ||
|
||
override def result(alg: util.List[Processor[T]], ref: util.List[Strategy[T]], middleResult: util.List[T], params: util.Map[Any, Any]): util.List[T] = { | ||
val oldDf = middleResult.get(0).asInstanceOf[DataFrame] | ||
val func = params.get("_func_").asInstanceOf[(DataFrame) => DataFrame] | ||
val _resource = if (params.containsKey("streaming.sql.out.path")) Some(params("streaming.sql.out.path").toString) else path | ||
|
||
val duration = params.getOrElse("streaming.duration", "10").toString.toInt | ||
val _mode = if (mode.isDefined) mode.get else "append" | ||
|
||
|
||
val _cfg = cfg | ||
val _format = format.get | ||
|
||
try { | ||
val df = func(oldDf) | ||
val query = df.writeStream | ||
if (params.containsKey("streaming.checkpoint")) { | ||
val checkpointDir = params.get("streaming.checkpoint").toString | ||
query.option("checkpointLocation", checkpointDir) | ||
} | ||
|
||
_resource match { | ||
case Some(p) => query.option("path", p) | ||
case None => | ||
} | ||
|
||
query.outputMode(_mode) | ||
|
||
val rt = query.trigger(ProcessingTime(duration, TimeUnit.SECONDS)).options(_cfg).format(_format).start() | ||
rt.awaitTermination() | ||
} catch { | ||
case e: Exception => e.printStackTrace() | ||
} | ||
params.remove("sql") | ||
new util.ArrayList[T]() | ||
} | ||
|
||
override def valid(params: util.Map[Any, Any]): (Boolean, String) = { | ||
(true, "") | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/streaming/core/compositor/spark/ss/source/MockSQLSourceCompositor.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,35 @@ | ||
package streaming.core.compositor.spark.ss.source | ||
|
||
import java.util | ||
|
||
import org.apache.log4j.Logger | ||
import org.apache.spark.sql.execution.streaming.MemoryStream | ||
import serviceframework.dispatcher.{Compositor, Processor, Strategy} | ||
import streaming.core.compositor.spark.streaming.CompositorHelper | ||
import streaming.core.strategy.platform.SparkStructuredStreamingRuntime | ||
|
||
import scala.collection.JavaConversions._ | ||
|
||
/** | ||
* 11/21/16 WilliamZhu([email protected]) | ||
*/ | ||
class MockSQLSourceCompositor[T] extends Compositor[T] with CompositorHelper { | ||
private var _configParams: util.List[util.Map[Any, Any]] = _ | ||
|
||
val logger = Logger.getLogger(classOf[MockSQLSourceCompositor[T]].getName) | ||
|
||
override def initialize(typeFilters: util.List[String], configParams: util.List[util.Map[Any, Any]]): Unit = { | ||
this._configParams = configParams | ||
} | ||
|
||
override def result(alg: util.List[Processor[T]], ref: util.List[Strategy[T]], middleResult: util.List[T], params: util.Map[Any, Any]): util.List[T] = { | ||
val sparkSSRt = sparkStructuredStreamingRuntime(params) | ||
val sparkSession = sparkSSRt.sparkSession | ||
import sparkSession.implicits._ | ||
implicit val sqlContext = sparkSession.sqlContext | ||
val inputData = MemoryStream[Int] | ||
inputData.addData(Seq(1, 2, 3, 4)) | ||
val df = inputData.toDS() | ||
List(df.asInstanceOf[T]) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/streaming/core/compositor/spark/ss/source/SQLSourceCompositor.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,31 @@ | ||
package streaming.core.compositor.spark.ss.source | ||
|
||
import java.util | ||
|
||
import org.apache.log4j.Logger | ||
import serviceframework.dispatcher.{Compositor, Processor, Strategy} | ||
import streaming.core.compositor.spark.streaming.CompositorHelper | ||
import streaming.core.strategy.platform.SparkStructuredStreamingRuntime | ||
|
||
import scala.collection.JavaConversions._ | ||
|
||
/** | ||
* 11/21/16 WilliamZhu([email protected]) | ||
*/ | ||
class SQLSourceCompositor[T] extends Compositor[T] with CompositorHelper { | ||
private var _configParams: util.List[util.Map[Any, Any]] = _ | ||
|
||
val logger = Logger.getLogger(classOf[SQLSourceCompositor[T]].getName) | ||
|
||
override def initialize(typeFilters: util.List[String], configParams: util.List[util.Map[Any, Any]]): Unit = { | ||
this._configParams = configParams | ||
} | ||
|
||
override def result(alg: util.List[Processor[T]], ref: util.List[Strategy[T]], middleResult: util.List[T], params: util.Map[Any, Any]): util.List[T] = { | ||
val sparkSSRt = sparkRuntime(params).asInstanceOf[SparkStructuredStreamingRuntime] | ||
val sourcePath = if (params.containsKey("streaming.sql.source.path")) params("streaming.sql.source.path").toString else _configParams(0)("path").toString | ||
val df = sparkSSRt.sparkSession.readStream.format(_configParams(0)("format").toString).options( | ||
(_configParams(0) - "format" - "path").map(f => (f._1.asInstanceOf[String], f._2.asInstanceOf[String])).toMap).load(sourcePath) | ||
List(df.asInstanceOf[T]) | ||
} | ||
} |
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
Oops, something went wrong.