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
c9c7605
commit 8d9c030
Showing
11 changed files
with
263 additions
and
46 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...ro-spark-2.0/src/main/java/org/apache/spark/sql/execution/streaming/ForeachHttpSink.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,18 @@ | ||
package org.apache.spark.sql.execution.streaming | ||
|
||
import net.csdn.modules.http.RestResponse | ||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql._ | ||
|
||
/** | ||
* Created by allwefantasy on 28/3/2017. | ||
*/ | ||
class ForeachHttpSink(writer: RestResponse) extends Sink with Logging { | ||
|
||
|
||
override def addBatch(batchId: Long, data: DataFrame): Unit = synchronized { | ||
val result = data.sparkSession.createDataFrame( | ||
data.sparkSession.sparkContext.parallelize(data.collect()), data.schema).toJSON.collect().mkString("") | ||
writer.write(result) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...mingpro-spark-2.0/src/main/java/org/apache/spark/sql/execution/streaming/SQLExecute.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,24 @@ | ||
package org.apache.spark.sql.execution.streaming | ||
|
||
import net.csdn.modules.http.RestResponse | ||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.streaming.{OutputMode, StreamingQueryListener} | ||
|
||
/** | ||
* Created by allwefantasy on 28/3/2017. | ||
*/ | ||
class SQLExecute(spark: SparkSession, restResponse: RestResponse) { | ||
def query(sql: String) = { | ||
val ds = spark.sql(sql) | ||
val sink = new ForeachHttpSink(restResponse) | ||
val df = ds.toDF() | ||
val streamQuery = spark.sessionState.streamingQueryManager.startQuery( | ||
None, | ||
None, | ||
df, | ||
sink, | ||
OutputMode.Append(),useTempCheckpointLocation = true | ||
) | ||
streamQuery.awaitTermination(5000) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
streamingpro-spark-2.0/src/main/java/streaming/core/LoalSparkServiceApp.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,28 @@ | ||
package streaming.core | ||
|
||
/** | ||
* Created by allwefantasy on 30/3/2017. | ||
*/ | ||
object LocalSparkServiceApp { | ||
def main(args: Array[String]): Unit = { | ||
StreamingApp.main(Array( | ||
"-streaming.master", "local[2]", | ||
"-streaming.name", "god", | ||
"-streaming.rest", "true", | ||
"-streaming.thrift", "true", | ||
"-streaming.platform", "spark", | ||
"-streaming.job.file.path", "classpath:///test/empty.json", | ||
"-streaming.enableHiveSupport", "true", | ||
"-streaming.spark.service", "true", | ||
"-streaming.enableCarbonDataSupport", "true", | ||
"-spark.sql.hive.thriftServer.singleSession","true" | ||
//"-streaming.sql.out.path","file:///tmp/test/pdate=20160809" | ||
|
||
//"-streaming.jobs","idf-compute" | ||
//"-streaming.sql.source.path","hdfs://m2:8020/data/raw/live-hls-formated/20160725/19/cdn148-16-52_2016072519.1469444764341" | ||
//"-streaming.driver.port", "9005" | ||
//"-streaming.zk.servers", "127.0.0.1", | ||
//"-streaming.zk.conf_root_dir", "/streamingpro/jack" | ||
)) | ||
} | ||
} |
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
123 changes: 123 additions & 0 deletions
123
streamingpro-spark-2.0/src/main/java/streaming/core/strategy/platform/SparkRuntime.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,123 @@ | ||
package streaming.core.strategy.platform | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
import java.util.{Map => JMap} | ||
|
||
import org.apache.spark.SparkConf | ||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.hive.thriftserver.HiveThriftServer2 | ||
|
||
import scala.collection.JavaConversions._ | ||
|
||
/** | ||
* Created by allwefantasy on 30/3/2017. | ||
*/ | ||
class SparkRuntime(_params: JMap[Any, Any]) extends StreamingRuntime with PlatformManagerListener { | ||
|
||
def name = "SPARK" | ||
|
||
var sparkSession: SparkSession = createRuntime | ||
|
||
def operator = null | ||
|
||
def createRuntime = { | ||
val conf = new SparkConf() | ||
params.filter(f => f._1.toString.startsWith("spark.")).foreach { f => | ||
conf.set(f._1.toString, f._2.toString) | ||
} | ||
|
||
if (params.containsKey("streaming.master")) { | ||
conf.setMaster(params.get("streaming.master").toString) | ||
} | ||
|
||
conf.setAppName(params.get("streaming.name").toString) | ||
|
||
val sparkSession = SparkSession.builder().config(conf) | ||
if (params.containsKey("streaming.enableHiveSupport") && | ||
params.get("streaming.enableHiveSupport").toString.toBoolean) { | ||
sparkSession.enableHiveSupport() | ||
} | ||
sparkSession.getOrCreate() | ||
} | ||
|
||
params.put("_session_", sparkSession) | ||
|
||
|
||
override def startRuntime: StreamingRuntime = { | ||
this | ||
} | ||
|
||
override def awaitTermination: Unit = { | ||
if (params.getOrElse("streaming.spark.service", false).toString.toBoolean) { | ||
Thread.currentThread().join() | ||
} | ||
} | ||
|
||
|
||
override def streamingRuntimeInfo: StreamingRuntimeInfo = null | ||
|
||
override def destroyRuntime(stopGraceful: Boolean, stopContext: Boolean): Boolean = { | ||
sparkSession.stop() | ||
SparkRuntime.clearLastInstantiatedContext() | ||
true | ||
} | ||
|
||
|
||
override def configureStreamingRuntimeInfo(streamingRuntimeInfo: StreamingRuntimeInfo): Unit = {} | ||
|
||
override def resetRuntimeOperator(runtimeOperator: RuntimeOperator): Unit = { | ||
|
||
} | ||
|
||
override def params: JMap[Any, Any] = _params | ||
|
||
override def processEvent(event: Event): Unit = {} | ||
|
||
SparkRuntime.setLastInstantiatedContext(this) | ||
|
||
override def startThriftServer: Unit = { | ||
HiveThriftServer2.startWithContext(sparkSession.sqlContext) | ||
} | ||
|
||
override def startHttpServer: Unit = {} | ||
|
||
} | ||
|
||
object SparkRuntime { | ||
|
||
|
||
private val INSTANTIATION_LOCK = new Object() | ||
|
||
/** | ||
* Reference to the last created SQLContext. | ||
*/ | ||
@transient private val lastInstantiatedContext = new AtomicReference[SparkRuntime]() | ||
|
||
/** | ||
* Get the singleton SQLContext if it exists or create a new one using the given SparkContext. | ||
* This function can be used to create a singleton SQLContext object that can be shared across | ||
* the JVM. | ||
*/ | ||
def getOrCreate(params: JMap[Any, Any]): SparkRuntime = { | ||
INSTANTIATION_LOCK.synchronized { | ||
if (lastInstantiatedContext.get() == null) { | ||
new SparkRuntime(params) | ||
} | ||
} | ||
PlatformManager.getOrCreate.register(lastInstantiatedContext.get()) | ||
lastInstantiatedContext.get() | ||
} | ||
|
||
private[platform] def clearLastInstantiatedContext(): Unit = { | ||
INSTANTIATION_LOCK.synchronized { | ||
PlatformManager.getOrCreate.unRegister(lastInstantiatedContext.get()) | ||
lastInstantiatedContext.set(null) | ||
} | ||
} | ||
|
||
private[platform] def setLastInstantiatedContext(sparkRuntime: SparkRuntime): Unit = { | ||
INSTANTIATION_LOCK.synchronized { | ||
lastInstantiatedContext.set(sparkRuntime) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
streamingpro-spark-2.0/src/main/java/streaming/rest/RestController.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,27 @@ | ||
package streaming.rest | ||
|
||
import net.csdn.annotation.rest.At | ||
import net.csdn.common.exception.RenderFinish | ||
import net.csdn.modules.http.ApplicationController | ||
import net.csdn.modules.http.RestRequest.Method._ | ||
import org.apache.spark.sql.execution.streaming.{ForeachHttpSink, ForeachSink, SQLExecute} | ||
import streaming.core.strategy.platform.{PlatformManager, SparkStructuredStreamingRuntime} | ||
|
||
/** | ||
* Created by allwefantasy on 28/3/2017. | ||
*/ | ||
class RestController extends ApplicationController { | ||
@At(path = Array("/run/sql"), types = Array(GET,POST)) | ||
def ss = { | ||
if (!runtime.isInstanceOf[SparkStructuredStreamingRuntime]) { | ||
render(400, "runtime should be spark_structured_streaming") | ||
} | ||
|
||
val spark = runtime.asInstanceOf[SparkStructuredStreamingRuntime].sparkSession | ||
|
||
new SQLExecute(spark,restResponse).query(param("sql")) | ||
throw new RenderFinish() | ||
} | ||
|
||
def runtime = PlatformManager.getRuntime | ||
} |
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
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