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.
Merge pull request byzer-org#897 from allwefantasy/ISSUE-896
PythonAlg supports python in MLSQL
- Loading branch information
Showing
6 changed files
with
169 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,16 @@ import org.apache.spark.ml.param.Param | |
import org.apache.spark.sql.expressions.UserDefinedFunction | ||
import org.apache.spark.sql.mlsql.session.MLSQLException | ||
import org.apache.spark.sql.{DataFrame, SparkSession} | ||
import streaming.common.HDFSOperator | ||
import streaming.dsl.ScriptSQLExec | ||
import streaming.dsl.mmlib.SQLAlg | ||
import streaming.dsl.mmlib.algs.param.BaseParams | ||
import streaming.dsl.mmlib.algs.python.PythonTrain | ||
import streaming.dsl.mmlib.algs.param.{BaseParams, WowParams} | ||
import streaming.dsl.mmlib.algs.python.{AutoCreateMLproject, PythonTrain} | ||
|
||
|
||
/** | ||
* 2019-01-08 WilliamZhu([email protected]) | ||
*/ | ||
class SQLPythonParallelExt(override val uid: String) extends SQLAlg with Functions with BaseParams { | ||
class SQLPythonParallelExt(override val uid: String) extends SQLAlg with Functions with WowParams { | ||
def this() = this(BaseParams.randomUID()) | ||
|
||
private def validateParams(params: Map[String, String]) = { | ||
|
@@ -43,51 +42,23 @@ class SQLPythonParallelExt(override val uid: String) extends SQLAlg with Functio | |
} | ||
} | ||
|
||
def projectName = "mlsql-python-project" | ||
|
||
/* | ||
We will automatically create project for user according the configuration | ||
*/ | ||
private def saveProject(sparkSession: SparkSession, path: String) = { | ||
val projectPath = path + s"/${projectName}" | ||
$(scripts).split(",").foreach { script => | ||
val content = sparkSession.table(script).head().getString(0) | ||
HDFSOperator.saveFile(projectPath, script + ".py", Seq(("", content)).iterator) | ||
} | ||
HDFSOperator.saveFile(projectPath, "MLproject", Seq(("", MLprojectTemplate)).iterator) | ||
val condaContent = sparkSession.table($(condaFile)).head().getString(0) | ||
HDFSOperator.saveFile(projectPath, "conda.yaml", Seq(("", condaContent)).iterator) | ||
projectPath | ||
} | ||
|
||
|
||
private def MLprojectTemplate = { | ||
s""" | ||
|name: mlsql-python | ||
| | ||
|conda_env: conda.yaml | ||
| | ||
|entry_points: | ||
| main: | ||
| train: | ||
| command: "python ${$(entryPoint)}.py" | ||
| batchPredict: | ||
| command: "python ${$(entryPoint)}.py" | ||
""".stripMargin | ||
} | ||
|
||
override def train(df: DataFrame, path: String, params: Map[String, String]): DataFrame = { | ||
pythonCheckRequirements(df) | ||
val mlsqlContext = ScriptSQLExec.contextGetOrForTest() | ||
|
||
validateParams(params) | ||
|
||
val projectPath = saveProject(df.sparkSession, path) | ||
val autoCreateMLproject = new AutoCreateMLproject($(scripts), $(condaFile), $(entryPoint)) | ||
|
||
val projectPath = autoCreateMLproject.saveProject(df.sparkSession, path) | ||
|
||
var newParams = params | ||
|
||
newParams += ("enableDataLocal" -> ($(feedMode) == "file").toString) | ||
newParams += ("pythonScriptPath" -> projectPath) | ||
newParams += ("pythonDescPath" -> projectPath) | ||
|
||
val pt = new PythonTrain() | ||
pt.train_per_partition(df, path, newParams) | ||
|
||
|
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