rename piflow-api to piflow-server;add getFlowInfo Api

This commit is contained in:
judy0131 2018-09-04 09:53:19 +08:00
parent c347710084
commit 508f0639a9
6 changed files with 159 additions and 127 deletions

View File

@ -1,4 +1,9 @@
server.ip=10.0.86.98
server.port=8001
spark.version=2.1.0
spark.master=10.0.86.89
spark.port=6066
yarn.url=http://192.168.6.22:8032/ws/v1/cluster/apps/
checkpoint.path=hdfs://10.0.86.89:9000/xjzhu/piflow/checkpoints/"

View File

@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>piflow-api</artifactId>
<artifactId>piflow-server</artifactId>
<dependencies>
<dependency>
<groupId>piflow</groupId>

View File

@ -6,7 +6,13 @@ import org.apache.spark.sql.SparkSession
import cn.piflow.conf.util.{FileUtil, OptionUtil}
import cn.piflow.Process
import cn.piflow.api.util.PropertyUtil
import com.github.ywilkof.sparkrestclient.{JobStatusResponse, SparkRestClient}
import com.github.ywilkof.sparkrestclient.SparkRestClient.SparkRestClientBuilder
import jodd.util.PropertiesUtil
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import scala.util.parsing.json.JSON
@ -24,7 +30,7 @@ object API {
//execute flow
val spark = SparkSession.builder()
.master("spark://10.0.86.89:7077")
.appName("piflow-hive-bundle")
.appName(flowBean.name)
.config("spark.driver.memory", "1g")
.config("spark.executor.memory", "2g")
.config("spark.cores.max", "2")
@ -32,6 +38,7 @@ object API {
.enableHiveSupport()
.getOrCreate()
val process = Runner.create()
.bind(classOf[SparkSession].getName, spark)
.bind("checkpoint.path", PropertyUtil.getPropertyValue("checkpoint.path"))
@ -46,4 +53,17 @@ object API {
process.stop()
"ok"
}
def getFlowInfo(appID : String) : String = {
val url = PropertyUtil.getPropertyValue("yarn.url") + appID
val client = HttpClients.createDefault()
val get:HttpGet = new HttpGet(url)
val response:CloseableHttpResponse = client.execute(get)
val entity = response.getEntity
val str = EntityUtils.toString(entity,"UTF-8")
println("Code is " + str)
str
}
}

View File

@ -1,6 +1,7 @@
package cn.piflow.api
import org.apache.http.client.methods.{CloseableHttpResponse, HttpPost}
import cn.piflow.api.util.PropertyUtil
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils

View File

@ -1,89 +1,95 @@
package cn.piflow.api
import java.io.File
import java.util.concurrent.CompletionStage
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.server.Directives
import akka.stream.ActorMaterializer
import cn.piflow.api.util.PropertyUtil
import com.typesafe.config.ConfigFactory
import scala.concurrent.Future
import scala.util.parsing.json.JSON
import cn.piflow.Process
import spray.json.DefaultJsonProtocol
object HTTPService extends DefaultJsonProtocol with Directives with SprayJsonSupport{
implicit val system = ActorSystem("HTTPService", ConfigFactory.load())
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
var processMap = Map[String, Process]()
def toJson(entity: RequestEntity): Map[String, Any] = {
entity match {
case HttpEntity.Strict(_, data) =>{
val temp = JSON.parseFull(data.utf8String)
temp.get.asInstanceOf[Map[String, Any]]
}
case _ => Map()
}
}
def route(req: HttpRequest): Future[HttpResponse] = req match {
case HttpRequest(GET, Uri.Path("/"), headers, entity, protocol) => {
Future.successful(HttpResponse(entity = "Get OK!"))
}
case HttpRequest(POST, Uri.Path("/flow/start"), headers, entity, protocol) =>{
entity match {
case HttpEntity.Strict(_, data) =>{
val flowJson = data.utf8String
val process = API.startFlow(flowJson)
processMap += (process.pid() -> process)
Future.successful(HttpResponse(entity = process.pid()))
}
case _ => Future.failed(new Exception("Can not start flow!"))
}
}
case HttpRequest(POST, Uri.Path("/flow/stop"), headers, entity, protocol) =>{
val data = toJson(entity)
val processId = data.get("processId").getOrElse("").asInstanceOf[String]
if(processId.equals("") || !processMap.contains(processId)){
Future.failed(new Exception("Can not found process Error!"))
}else{
val result = API.stopFlow(processMap.get(processId).asInstanceOf[Process])
Future.successful(HttpResponse(entity = result))
}
}
case _: HttpRequest =>
Future.successful(HttpResponse(404, entity = "Unknown resource!"))
}
def run = {
val ip = PropertyUtil.getPropertyValue("server.ip")
val port = PropertyUtil.getIntPropertyValue("server.port")
Http().bindAndHandleAsync(route, ip, port)
println("Server:" + ip + ":" + port + " Started!!!")
}
}
object Main {
def main(argv: Array[String]):Unit = {
HTTPService.run
}
}
package cn.piflow.api
import java.io.File
import java.util.concurrent.CompletionStage
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.server.Directives
import akka.stream.ActorMaterializer
import cn.piflow.api.util.PropertyUtil
import com.typesafe.config.ConfigFactory
import scala.concurrent.Future
import scala.util.parsing.json.JSON
import cn.piflow.Process
import spray.json.DefaultJsonProtocol
object HTTPService extends DefaultJsonProtocol with Directives with SprayJsonSupport{
implicit val system = ActorSystem("HTTPService", ConfigFactory.load())
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
var processMap = Map[String, Process]()
def toJson(entity: RequestEntity): Map[String, Any] = {
entity match {
case HttpEntity.Strict(_, data) =>{
val temp = JSON.parseFull(data.utf8String)
temp.get.asInstanceOf[Map[String, Any]]
}
case _ => Map()
}
}
def route(req: HttpRequest): Future[HttpResponse] = req match {
case HttpRequest(GET, Uri.Path("/"), headers, entity, protocol) => {
Future.successful(HttpResponse(entity = "Get OK!"))
}
case HttpRequest(GET, Uri.Path("/flow/info"), headers, entity, protocol) => {
val appID = ""
val result = API.getFlowInfo(appID)
Future.successful(HttpResponse(entity = "Get OK!"))
}
case HttpRequest(POST, Uri.Path("/flow/start"), headers, entity, protocol) =>{
entity match {
case HttpEntity.Strict(_, data) =>{
val flowJson = data.utf8String
val process = API.startFlow(flowJson)
processMap += (process.pid() -> process)
Future.successful(HttpResponse(entity = process.pid()))
}
case _ => Future.failed(new Exception("Can not start flow!"))
}
}
case HttpRequest(POST, Uri.Path("/flow/stop"), headers, entity, protocol) =>{
val data = toJson(entity)
val processId = data.get("processId").getOrElse("").asInstanceOf[String]
if(processId.equals("") || !processMap.contains(processId)){
Future.failed(new Exception("Can not found process Error!"))
}else{
val result = API.stopFlow(processMap.get(processId).asInstanceOf[Process])
Future.successful(HttpResponse(entity = result))
}
}
case _: HttpRequest =>
Future.successful(HttpResponse(404, entity = "Unknown resource!"))
}
def run = {
val ip = PropertyUtil.getPropertyValue("server.ip")
val port = PropertyUtil.getIntPropertyValue("server.port")
Http().bindAndHandleAsync(route, ip, port)
println("Server:" + ip + ":" + port + " Started!!!")
}
}
object Main {
def main(argv: Array[String]):Unit = {
HTTPService.run
}
}

View File

@ -1,35 +1,35 @@
package cn.piflow.api.util
import java.io.{FileInputStream, InputStream}
import java.util.Properties
object PropertyUtil {
private val prop: Properties = new Properties()
var fis: InputStream = null
try{
//val path = Thread.currentThread().getContextClassLoader.getResource("config.properties").getPath
//fis = this.getClass.getResourceAsStream("")
val userDir = System.getProperty("user.dir")
val path = userDir + "/conf/" + "config.properties"
prop.load(new FileInputStream(path))
} catch{
case ex: Exception => ex.printStackTrace()
}
def getPropertyValue(propertyKey: String): String ={
val obj = prop.get(propertyKey)
if(obj != null){
return obj.toString
}
null
}
def getIntPropertyValue(propertyKey: String): Int ={
val obj = prop.getProperty(propertyKey)
if(obj != null){
return obj.toInt
}
throw new NullPointerException
}
}
package cn.piflow.api.util
import java.io.{FileInputStream, InputStream}
import java.util.Properties
object PropertyUtil {
private val prop: Properties = new Properties()
var fis: InputStream = null
try{
//val path = Thread.currentThread().getContextClassLoader.getResource("config.properties").getPath
//fis = this.getClass.getResourceAsStream("")
val userDir = System.getProperty("user.dir")
val path = userDir + "/conf/" + "config.properties"
prop.load(new FileInputStream(path))
} catch{
case ex: Exception => ex.printStackTrace()
}
def getPropertyValue(propertyKey: String): String ={
val obj = prop.get(propertyKey)
if(obj != null){
return obj.toString
}
null
}
def getIntPropertyValue(propertyKey: String): Int ={
val obj = prop.getProperty(propertyKey)
if(obj != null){
return obj.toInt
}
throw new NullPointerException
}
}