-
Notifications
You must be signed in to change notification settings - Fork 16
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 #329 from alexarchambault/syntax
Add various syntax and Toree compatibility helpers
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
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
17 changes: 17 additions & 0 deletions
17
modules/almond-spark/src/main/scala/almond/spark/syntax.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,17 @@ | ||
package almond.spark | ||
|
||
import almond.display.Html | ||
import org.apache.spark.sql._ | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
|
||
object syntax { | ||
implicit class KernelToreeSparkOps(private val kernel: almond.api.JupyterApi) { | ||
def sparkContext: SparkContext = sparkSession.sparkContext | ||
def sparkConf: SparkConf = sparkContext.getConf | ||
def sparkSession: SparkSession = SparkSession.builder().getOrCreate() | ||
} | ||
implicit class AlmondNetflixDataFrameOps(private val df: DataFrame) { | ||
def render(limit: Int = 10): Html = | ||
Html(DataFrameRenderer.render(df, limit = limit)) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/almond-toree-spark/src/main/scala/almond/spark/ToreeSql.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,20 @@ | ||
package almond.spark | ||
|
||
object ToreeSql { | ||
|
||
var sqlLimit = 10 | ||
|
||
private def sqlMagic(content: String): String = { | ||
val tq = "\"\"\"" | ||
s"""_root_.almond.display.Html(_root_.almond.dfrenderer.AlmondDataFrameRenderer.render(spark.sql($tq$content$tq), limit = $sqlLimit))""" | ||
} | ||
|
||
def setup(): Unit = { | ||
almond.toree.CellMagicHook.addHandler("sql") { (_, content) => | ||
Right(sqlMagic(content)) | ||
} | ||
almond.toree.CellMagicHook.addHandler("sparksql") { (_, content) => | ||
Right(sqlMagic(content)) | ||
} | ||
} | ||
} |
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