-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
95 changed files
with
2,960 additions
and
549 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
-Djava.awt.headless=true | ||
-J-Xmx2g | ||
-J-XX:MaxMetaspaceSize=1g |
76 changes: 0 additions & 76 deletions
76
bench/src/main/scala/astraea/spark/rasterframes/bench/HistogramEncodeBench.scala
This file was deleted.
Oops, something went wrong.
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
144 changes: 144 additions & 0 deletions
144
core/src/main/scala/astraea/spark/rasterframes/PairRDDConverter.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,144 @@ | ||
package astraea.spark.rasterframes | ||
|
||
import astraea.spark.rasterframes.util._ | ||
import geotrellis.raster.{MultibandTile, Tile, TileFeature} | ||
import geotrellis.spark.{SpaceTimeKey, SpatialKey, TemporalKey} | ||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.sql._ | ||
import org.apache.spark.sql.gt.types.TileUDT | ||
import org.apache.spark.sql.types._ | ||
|
||
import scala.annotation.implicitNotFound | ||
|
||
/** | ||
* Typeclass for converting a Pair RDD into a dataframe. | ||
* | ||
* @since 4/8/18 | ||
*/ | ||
@implicitNotFound("An RDD converter is required create a RasterFrame. " + | ||
"Please provide an implementation of PairRDDConverter[${K}, ${V}].") | ||
trait PairRDDConverter[K, V] extends Serializable { | ||
val schema: StructType | ||
def toDataFrame(rdd: RDD[(K, V)])(implicit spark: SparkSession): DataFrame | ||
} | ||
|
||
object PairRDDConverter { | ||
/** Enrichment over a pair RDD for converting it to a DataFrame given a converter. */ | ||
implicit class RDDCanBeDataFrame[K, V](rdd: RDD[(K, V)])(implicit spark: SparkSession, converter: PairRDDConverter[K, V]) { | ||
def toDataFrame: DataFrame = converter.toDataFrame(rdd) | ||
} | ||
|
||
// Hack around Spark bug when singletons are used in schemas | ||
private val serializableTileUDT = new TileUDT() | ||
|
||
/** Fetch converter from implicit scope. */ | ||
def apply[K, V](implicit sp: PairRDDConverter[K, V]) = sp | ||
|
||
/** Enables conversion of `RDD[(SpatialKey, Tile)]` to DataFrame. */ | ||
implicit val spatialTileConverter = new PairRDDConverter[SpatialKey, Tile] { | ||
val schema: StructType = { | ||
StructType(Seq( | ||
StructField(SPATIAL_KEY_COLUMN.columnName, spatialKeyEncoder.schema, nullable = false), | ||
StructField(TILE_COLUMN.columnName, serializableTileUDT, nullable = false) | ||
)) | ||
} | ||
|
||
def toDataFrame(rdd: RDD[(SpatialKey, Tile)])(implicit spark: SparkSession): DataFrame = { | ||
import spark.implicits._ | ||
rdd.toDF(schema.fields.map(_.name): _*) | ||
} | ||
} | ||
|
||
/** Enables conversion of `RDD[(SpaceTimeKey, Tile)]` to DataFrame. */ | ||
implicit val spaceTimeTileConverter = new PairRDDConverter[SpaceTimeKey, Tile] { | ||
val schema: StructType = { | ||
val base = spatialTileConverter.schema | ||
val addedFields = Seq(StructField(TEMPORAL_KEY_COLUMN.columnName, temporalKeyEncoder.schema, nullable = false)) | ||
StructType(base.fields.patch(1, addedFields, 0)) | ||
} | ||
|
||
def toDataFrame(rdd: RDD[(SpaceTimeKey, Tile)])(implicit spark: SparkSession): DataFrame = { | ||
import spark.implicits._ | ||
rdd.map{ case (k, v) ⇒ (k.spatialKey, k.temporalKey, v)}.toDF(schema.fields.map(_.name): _*) | ||
} | ||
} | ||
|
||
/** Enables conversion of `RDD[(SpatialKey, TileFeature[Tile, D])]` to DataFrame. */ | ||
implicit def spatialTileFeatureConverter[D: Encoder] = new PairRDDConverter[SpatialKey, TileFeature[Tile, D]] { | ||
implicit val featureEncoder = implicitly[Encoder[D]] | ||
implicit val rowEncoder = Encoders.tuple(spatialKeyEncoder, singlebandTileEncoder, featureEncoder) | ||
|
||
val schema: StructType = { | ||
val base = spatialTileConverter.schema | ||
StructType(base.fields :+ StructField(TILE_FEATURE_DATA_COLUMN.columnName, featureEncoder.schema, nullable = true)) | ||
} | ||
|
||
def toDataFrame(rdd: RDD[(SpatialKey, TileFeature[Tile, D])])(implicit spark: SparkSession): DataFrame = { | ||
import spark.implicits._ | ||
rdd.map{ case (k, v) ⇒ (k, v.tile, v.data)}.toDF(schema.fields.map(_.name): _*) | ||
} | ||
} | ||
|
||
/** Enables conversion of `RDD[(SpaceTimeKey, TileFeature[Tile, D])]` to DataFrame. */ | ||
implicit def spaceTimeTileFeatureConverter[D: Encoder] = new PairRDDConverter[SpaceTimeKey, TileFeature[Tile, D]] { | ||
implicit val featureEncoder = implicitly[Encoder[D]] | ||
implicit val rowEncoder = Encoders.tuple(spatialKeyEncoder, temporalKeyEncoder, singlebandTileEncoder, featureEncoder) | ||
|
||
val schema: StructType = { | ||
val base = spaceTimeTileConverter.schema | ||
StructType(base.fields :+ StructField(TILE_FEATURE_DATA_COLUMN.columnName, featureEncoder.schema, nullable = true)) | ||
} | ||
|
||
def toDataFrame(rdd: RDD[(SpaceTimeKey, TileFeature[Tile, D])])(implicit spark: SparkSession): DataFrame = { | ||
import spark.implicits._ | ||
val tupRDD = rdd.map { case (k, v) ⇒ (k.spatialKey, k.temporalKey, v.tile, v.data) } | ||
|
||
rddToDatasetHolder(tupRDD) | ||
tupRDD.toDF(schema.fields.map(_.name): _*) | ||
} | ||
} | ||
|
||
/** Enables conversion of `RDD[(SpatialKey, MultibandTile)]` to DataFrame. */ | ||
def forSpatialMultiband(bands: Int) = new PairRDDConverter[SpatialKey, MultibandTile] { | ||
val schema: StructType = { | ||
val base = spatialTileConverter.schema | ||
|
||
val basename = TILE_COLUMN.columnName | ||
|
||
val tiles = for(i ← 1 to bands) yield { | ||
StructField(s"${basename}_$i" , serializableTileUDT, nullable = false) | ||
} | ||
|
||
StructType(base.fields.patch(1, tiles, 1)) | ||
} | ||
|
||
def toDataFrame(rdd: RDD[(SpatialKey, MultibandTile)])(implicit spark: SparkSession): DataFrame = { | ||
spark.createDataFrame( | ||
rdd.map { case (k, v) ⇒ Row(Row(k.col, k.row) +: v.bands: _*) }, | ||
schema | ||
) | ||
} | ||
} | ||
|
||
/** Enables conversion of `RDD[(SpaceTimeKey, MultibandTile)]` to DataFrame. */ | ||
def forSpaceTimeMultiband(bands: Int) = new PairRDDConverter[SpaceTimeKey, MultibandTile] { | ||
val schema: StructType = { | ||
val base = spaceTimeTileConverter.schema | ||
|
||
val basename = TILE_COLUMN.columnName | ||
|
||
val tiles = for(i ← 1 to bands) yield { | ||
StructField(s"${basename}_$i" , serializableTileUDT, nullable = false) | ||
} | ||
|
||
StructType(base.fields.patch(2, tiles, 1)) | ||
} | ||
|
||
def toDataFrame(rdd: RDD[(SpaceTimeKey, MultibandTile)])(implicit spark: SparkSession): DataFrame = { | ||
spark.createDataFrame( | ||
rdd.map { case (k, v) ⇒ Row(Seq(Row(k.spatialKey.col, k.spatialKey.row), Row(k.temporalKey)) ++ v.bands: _*) }, | ||
schema | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.