Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Inputer as DOperation #68

Open
wants to merge 3 commits into
base: spark-2.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/build_and_run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ run_tests() {
sbt clean
sbt -DSPARK_VERSION=$1 test ds-it
}
run_tests 2.2.0
run_tests 2.1.1
run_tests 2.0.2

Expand Down
2 changes: 1 addition & 1 deletion build/e2e_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ SPARK_STANDALONE_MANAGEMENT="./seahorse-workflow-executor/docker/spark-standalon
MESOS_SPARK_DOCKER_COMPOSE="testing/mesos-spark-cluster/mesos-cluster.dc.yml"
YARN_SPARK_DOCKER_COMPOSE="testing/yarn-spark-cluster/yarn-cluster.dc.yml"

SPARK_VERSION="2.1.1"
SPARK_VERSION="2.2.0"
HADOOP_VERSION="2.7"

## Make sure that when job is aborted/killed all dockers will be turned off
Expand Down
2 changes: 1 addition & 1 deletion build/manage-docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
sbt_type = 'sbt'


spark_version = "2.1.1"
spark_version = "2.2.0"
hadoop_version = "2.7"


Expand Down
1 change: 1 addition & 0 deletions build/workflow-executor_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ build_workflowexecutor "2.0.1"
build_workflowexecutor "2.0.2"
build_workflowexecutor "2.1.0"
build_workflowexecutor "2.1.1"
build_workflowexecutor "2.2.0"
2 changes: 1 addition & 1 deletion deployment/spark-docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ENV SPARK_PACKAGE spark-$SPARK_VERSION-bin-hadoop$HADOOP_VERSION
ENV SPARK_HOME /opt/spark-$SPARK_VERSION
ENV PATH $PATH:$SPARK_HOME/bin
RUN wget -q -O - \
"http://d3kbcqa49mib13.cloudfront.net/$SPARK_PACKAGE.tgz" \
"https://archive.apache.org/dist/spark/spark-$SPARK_VERSION/$SPARK_PACKAGE.tgz" \
| gunzip \
| tar x -C /tmp/ \
&& mv /tmp/$SPARK_PACKAGE $SPARK_HOME \
Expand Down
3 changes: 2 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ object Version {
val scoverage = "1.0.4"
val slick = "3.1.1"

val spark = sys.props.getOrElse("SPARK_VERSION", "2.1.1")
val spark = sys.props.getOrElse("SPARK_VERSION", "2.2.0")
val (scala, hadoop, akka, sprayRoutingLib) = spark match {
case "2.2.0" => ("2.11.8", "2.7", "2.4.9", "routing-shapeless2")
case "2.1.0" | "2.1.1" => ("2.11.8", "2.7", "2.4.9", "routing-shapeless2")
case "2.0.0" | "2.0.1" | "2.0.2" => ("2.11.8", "2.7", "2.4.9", "routing")
}
Expand Down
2 changes: 1 addition & 1 deletion remote_notebook/code/sparkr_kernel/kernel_init.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ assign("sc", get(".sparkRjsc", envir = SparkR:::.sparkREnv), envir = .GlobalEnv)
sparkSQLSession <- SparkR:::callJMethod(entryPoint, "getNewSparkSQLSession")

sparkVersion <- SparkR:::callJMethod(sc, "version")
if (sparkVersion %in% c("2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.1.1")) {
if (sparkVersion %in% c("2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.1.1", "2.2.0", "2.2.1")) {
assign(".sparkRsession", SparkR:::callJMethod(sparkSQLSession, "getSparkSession"), envir = SparkR:::.sparkREnv)
assign("spark", get(".sparkRsession", envir = SparkR:::.sparkREnv), envir = .GlobalEnv)
} else {
Expand Down
2 changes: 1 addition & 1 deletion seahorse-sdk-example
32 changes: 30 additions & 2 deletions seahorse-workflow-executor/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,51 @@ lazy val settingsForNotPublished = CommonSettingsPlugin.assemblySettings ++

lazy val sparkVersion = Version.spark



lazy val sparkUtils = sparkVersion match {
case "2.0.0" | "2.0.1" | "2.0.2" =>
val sparkUtils2_0_x = project in file("sparkutils2.0.x") settings settingsForPublished
sparkUtils2_0_x
case "2.1.0" | "2.1.1" =>
val sparkUtils2_1_0 = project in file("sparkutils2.1.x") settings settingsForPublished
sparkUtils2_1_0
case "2.2.0" =>
val sparkUtils2_1_0 = project in file("sparkutils2.2.x") settings settingsForPublished
sparkUtils2_1_0
}

lazy val sparkUtils2_x = project in file(s"sparkutils2.x") dependsOn sparkUtils settings settingsForPublished
lazy val sparkUtils2_x = project in file(s"sparkutils2.x") dependsOn (csvlib, sparkUtils) settings settingsForPublished

lazy val csv2_2 = project in file(s"sparkutilsfeatures/csv2_2") settings settingsForPublished
lazy val csv2_0 = project in file(s"sparkutilsfeatures/csv2_0") dependsOn sparkUtils settings settingsForPublished

lazy val csvlib = sparkVersion match {
case "2.0.0" | "2.0.1" | "2.0.2" =>
csv2_0
case "2.1.0" | "2.1.1" =>
csv2_0
case "2.2.0" =>
csv2_2
}

lazy val readjsondataset = project in file(s"sparkutilsfeatures/readjsondataset") dependsOn sparkUtils2_x settings settingsForPublished
lazy val readjsondataframe = project in file(s"sparkutilsfeatures/readjsondataframe") dependsOn sparkUtils2_x settings settingsForPublished

lazy val readjson = sparkVersion match {
case "2.0.0" | "2.0.1" | "2.0.2" => readjsondataframe
case "2.1.0" | "2.1.1" => readjsondataframe
case "2.2.0" => readjsondataset
}

lazy val rootProject = project
.in(file("."))
.settings(name := "seahorse")
.settings(PublishSettings.disablePublishing)
.aggregate(
api,
csvlib,
readjson,
sparkUtils2_x,
sparkUtils,
commons,
Expand All @@ -54,7 +82,7 @@ lazy val api = project settings settingsForPublished

lazy val commons = project dependsOn (api, sparkUtils2_x) settings settingsForPublished

lazy val deeplang = project dependsOn (commons,
lazy val deeplang = project dependsOn (commons, readjson, csvlib,
commons % "test->test",
graph,
graph % "test->test",
Expand Down
8 changes: 4 additions & 4 deletions seahorse-workflow-executor/build/build_and_run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@


TASKS="clean scalastyle test:scalastyle test it:compile ds-it"
#sbt -DSPARK_VERSION=2.0.0 $TASKS
#sbt -DSPARK_VERSION=2.0.1 $TASKS
#sbt -DSPARK_VERSION=2.1.0 $TASKS
sbt -DSPARK_VERSION=2.0.0 $TASKS
sbt -DSPARK_VERSION=2.0.1 $TASKS
sbt -DSPARK_VERSION=2.1.0 $TASKS
sbt -DSPARK_VERSION=2.1.1 $TASKS
sbt -DSPARK_VERSION=2.0.2 $TASKS
sbt -DSPARK_VERSION=2.2.0 $TASKS
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class CatalogRecorder private (jars: Seq[URL]) {
catalog.registerDOperation(DOperationCategories.Filtering, () => new FilterRows())
catalog.registerDOperation(DOperationCategories.Filtering, () => new HandleMissingValues())
catalog.registerDOperation(DOperationCategories.Filtering, () => new Projection())
catalog.registerDOperation(DOperationCategories.Filtering, () => new Imputer())

catalog.registerDOperation(DOperationCategories.Transformation.Custom, () => new CreateCustomTransformer())
catalog.registerDOperation(DOperationCategories.Transformation.Custom, () => new SqlColumnTransformation())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright (c) 2016, CodiLime Inc.
*/
package ai.deepsense.deeplang.doperations

import scala.language.reflectiveCalls
import scala.reflect.runtime.universe.TypeTag

import ai.deepsense.commons.models.Id
import ai.deepsense.commons.utils.Version
import ai.deepsense.deeplang._
import ai.deepsense.deeplang.documentation.OperationDocumentation
import ai.deepsense.deeplang.doperables.Transformer
import ai.deepsense.deeplang.doperables.dataframe.DataFrame
import ai.deepsense.deeplang.doperations.InputerTransformer.{Mean, Strategy}
import ai.deepsense.deeplang.params.choice.Choice
import ai.deepsense.deeplang.params.{ColumnSelectorParam, Param}
import ai.deepsense.deeplang.params.selections.{MultipleColumnSelection, NameColumnSelection}
import ai.deepsense.deeplang.params.wrappers.spark.{ChoiceParamWrapper, ParamsWithSparkWrappers}
import org.apache.spark.ml
import org.apache.spark.ml.feature.{Imputer => SparkImputer}
import org.apache.spark.sql.types.StructType



object InputerTransformer {
sealed abstract class Strategy(override val name: String) extends Choice {
override val params: Array[Param[_]] = Array()

override val choiceOrder: List[Class[_ <: Choice]] = List(
classOf[Mean],
classOf[Median]
)
}
case class Mean() extends Strategy("mean")
case class Median() extends Strategy("median")
}


class InputerTransformer extends Transformer with ParamsWithSparkWrappers {

val selectedColumns = ColumnSelectorParam(
name = "selected columns",
description = Some("Columns to complete missing values in."),
portIndex = 0)

def getSelectedColumns: MultipleColumnSelection = $(selectedColumns)

def setSelectedColumns(value: MultipleColumnSelection): this.type =
set(selectedColumns, value)

def setSelectedColumns(retainedColumns: Seq[String]): this.type =
setSelectedColumns(
MultipleColumnSelection(
Vector(NameColumnSelection(retainedColumns.toSet)),
excluding = false))

val strategy = new ChoiceParamWrapper[
ml.param.Params { val strategy: ml.param.Param[String] }, Strategy](
name = "strategy",
description = Some("Algorithm used to compute missing value"),
sparkParamGetter = _.strategy)
setDefault(strategy, Mean())

override val params: Array[Param[_]] = Array(selectedColumns, strategy)



override def applyTransform(ctx: ExecutionContext, df: DataFrame): DataFrame = {
val columns = df.getColumnNames(getSelectedColumns)
val inputer = new SparkImputer()
.setInputCols(columns.toArray)
.setOutputCols(columns.toArray)
val sparkParams =
sparkParamMap(inputer, df.sparkDataFrame.schema)

val imputerModel = inputer.fit(df.sparkDataFrame, sparkParams)
val res = imputerModel.transform(df.sparkDataFrame).toDF()
DataFrame.fromSparkDataFrame(res)
}

override def applyTransformSchema(schema: StructType): Option[StructType] = {
Some(schema)
}
}


class Imputer extends TransformerAsOperation[InputerTransformer] with OperationDocumentation {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see Imputer and ImputerModel in Spark, which means Imputer is an Estimator which trains ImputerModel, which is in turn used to transform dataframes. We should have it reflected in seahorse. You can do it in a manner similar to how we handle it in other operations, using SparkEstimatorWrapper, SparkModelWrapper and than use EstimatorAsOperation on wrapped estimator.

override val id: Id = "37d8ce21-0aa9-4448-8fb0-defb58c5e53f"
override val name: String = "Imputer"
override val description: String =
"Completes missing values using Estimator on a DataFrame"

override lazy val tTagTI_0: TypeTag[DataFrame] = typeTag
override lazy val tTagTO_0: TypeTag[DataFrame] = typeTag
override lazy val tTagTO_1: TypeTag[InputerTransformer] = typeTag

override val since: Version = Version(1, 5, 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ package ai.deepsense.deeplang.doperations.readwritedataframe.filestorage
import java.io.{File, IOException, PrintWriter}

import scala.io.Source

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.execution.datasources.csv.{DataframeToDriverCsvFileWriter, RawCsvRDDToDataframe}
import org.apache.spark.sql.{SaveMode, DataFrame => SparkDataFrame}
import org.apache.spark.sql.{Dataset, Encoders, Row, SaveMode, DataFrame => SparkDataFrame}
import ai.deepsense.commons.resources.ManagedResource
import ai.deepsense.deeplang.ExecutionContext
import ai.deepsense.deeplang.doperables.dataframe.DataFrame
import ai.deepsense.deeplang.doperations.inout.{InputFileFormatChoice, OutputFileFormatChoice}
import ai.deepsense.deeplang.doperations.readwritedataframe.filestorage.csv.CsvOptions
import ai.deepsense.deeplang.doperations.readwritedataframe.{FilePath, FileScheme}
import ai.deepsense.deeplang.readjsondataset.JsonReader
import ai.deepsense.sparkutils.SQL

object DriverFiles {
object DriverFiles extends JsonReader {

def read(driverPath: String, fileFormat: InputFileFormatChoice)
(implicit context: ExecutionContext): SparkDataFrame = fileFormat match {
Expand Down Expand Up @@ -59,13 +61,14 @@ object DriverFiles {
val lines = Source.fromFile(driverPath).getLines().toStream
val fileLinesRdd = context.sparkContext.parallelize(lines)

RawCsvRDDToDataframe.parse(fileLinesRdd, context.sparkSQLSession, params)
RawCsvRDDToDataframe.parse(fileLinesRdd, context.sparkSQLSession.sparkSession, params)
}

private def readJson(driverPath: String)(implicit context: ExecutionContext) = {
val lines = Source.fromFile(driverPath).getLines().toStream
val fileLinesRdd = context.sparkContext.parallelize(lines)
context.sparkSQLSession.read.json(fileLinesRdd)
val sparkSession = context.sparkSQLSession.sparkSession
readJsonFromRdd(fileLinesRdd, sparkSession)
}

private def writeCsv
Expand All @@ -77,7 +80,8 @@ object DriverFiles {
dataFrame.sparkDataFrame,
params,
dataFrame.schema.get,
path.pathWithoutScheme
path.pathWithoutScheme,
context.sparkSQLSession.sparkSession
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function networkRm {
ACTION=$1
SPARK_VERSION=$2
export SPARK_VERSION=${SPARK_VERSION}
if [ "$SPARK_VERSION" == "2.1.0" ] || [ "$SPARK_VERSION" == "2.1.1" ]; then
if [ "$SPARK_VERSION" == "2.1.0" ] || [ "$SPARK_VERSION" == "2.1.1" ] || [ "$SPARK_VERSION" == "2.2.0" ]; then
export HADOOP_VERSION="2.7"
# We use 2.7.1 for Spark 2.1.x despite the fact that the latter depends on 2.7.3, but 2.7.3
# doesn't have docker image released yet.
Expand Down
3 changes: 2 additions & 1 deletion seahorse-workflow-executor/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import sbt._

object Version {

val spark = sys.props.getOrElse("SPARK_VERSION", "2.1.1")
val spark = sys.props.getOrElse("SPARK_VERSION", "2.2.0")
val (scala, java, hadoop, akka, apacheCommons) = spark match {
case "2.2.0" => ("2.11.8", "1.8", "2.7.3", "2.4.9", "3.5")
case "2.1.0" | "2.1.1" => ("2.11.8", "1.8", "2.7.3", "2.4.9", "3.5")
case "2.0.0" | "2.0.1" | "2.0.2" => ("2.11.8", "1.8", "2.7.1", "2.4.9", "3.3.+")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _run_custom_code(self, workflow_id, node_id, custom_operation_code):

new_sql_context = None
spark_version = self.spark_context.version
if spark_version in ["2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.1.1"]:
if spark_version in ["2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.1.1", "2.2.0"]:
new_sql_context = SQLContext(self.spark_context, new_spark_session)
else:
raise ValueError("Spark version {} is not supported".format(spark_version))
Expand Down
2 changes: 1 addition & 1 deletion seahorse-workflow-executor/python/pyexecutor/pyexecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _initialize_spark_contexts(gateway):
java_spark_sql_session = gateway.entry_point.getSparkSQLSession()
spark_version = spark_context.version
spark_sql_session = None
if spark_version in ["2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.1.1"]:
if spark_version in ["2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.1.1", "2.2.0"]:
from pyspark.sql import SparkSession
java_spark_session = java_spark_sql_session.getSparkSession()
spark_sql_session = SparkSession(spark_context, java_spark_session)
Expand Down
2 changes: 1 addition & 1 deletion seahorse-workflow-executor/scalastyle-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parameters>
<parameter name="regex">true</parameter>
<parameter name="header"><![CDATA[/\*\*
\* Copyright 201[567] deepsense.ai \(CodiLime, Inc\)
\* Copyright 201[56789] deepsense.ai \(CodiLime, Inc\)
\*
\* Licensed under the Apache License, Version 2.0 \(the \"License\"\);
\* you may not use this file except in compliance with the License\.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
* limitations under the License.
*/


package org.apache.spark.sql.execution.datasources.csv

import org.apache.spark.rdd.RDD
import org.apache.spark.sql._
import org.apache.spark.sql.execution.LogicalRDD
import org.apache.spark.sql.types.{StructType, _}


object SparkCsvReader {
def create(options: CSVOptions): CsvReader = new CsvReader(options)
def univocityTokenizer(
rdd: RDD[String],
header: Array[String],
firstLine: String,
options: CSVOptions): RDD[Array[String]] =
rdd: RDD[String],
header: Array[String],
firstLine: String,
options: CSVOptions): RDD[Array[String]] =
CSVRelation.univocityTokenizer(rdd, firstLine, options)
}
19 changes: 19 additions & 0 deletions seahorse-workflow-executor/sparkutils2.2.x/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2017 deepsense.ai (CodiLime, Inc)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

name := "seahorse-executor-sparkutils2.2.x"

libraryDependencies ++= Dependencies.sparkutils(Version.spark)
Loading