-
Notifications
You must be signed in to change notification settings - Fork 149
Scala API
Ram Sriharsha edited this page Jul 20, 2015
·
14 revisions
Magellan is hosted on Spark Packages
When launching the Spark Shell, Magellan can be included like any other spark package using the --packages option:
> $SPARK_HOME/bin/spark-shell --packages harsha2010:magellan:1.0.0-s_2.10
A few common packages you might want to import within Magellan
import magellan.{Point, Polygon}
import org.apache.spark.sql.magellan.dsl.expressions._
import org.apache.spark.sql.types._
val points = sc.parallelize(Seq((-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0))).toDF("x", "y").select(point($"x", $"y").as("point"))
points.show()
+-----------------+
| point|
+-----------------+
|Point(-1.0, -1.0)|
| Point(-1.0, 1.0)|
| Point(1.0, -1.0)|
+-----------------+
case class PolygonRecord(polygon: Polygon)
val ring = Array(new Point(1.0, 1.0), new Point(1.0, -1.0),
new Point(-1.0, -1.0), new Point(-1.0, 1.0),
new Point(1.0, 1.0))
val polygons = sc.parallelize(Seq(
PolygonRecord(new Polygon(Array(0), ring))
)).toDF()
polygons.show()
+--------------------+
| polygon|
+--------------------+
|Polygon(5, Vector...|
+--------------------+
polygons.select(point(0.5, 0.5) within $"polygon").count()
points.join(polygons).where($"point" intersects $"polygon").show()
+-----------------+--------------------+
| point| polygon|
+-----------------+--------------------+
|Point(-1.0, -1.0)|Polygon(5, Vector...|
| Point(-1.0, 1.0)|Polygon(5, Vector...|
| Point(1.0, -1.0)|Polygon(5, Vector...|
+-----------------+--------------------+