-
Notifications
You must be signed in to change notification settings - Fork 22
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
108 changed files
with
651 additions
and
53 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package vectorpipe.encoders | ||
|
||
import geotrellis.vector._ | ||
import geotrellis.vectortile._ | ||
import org.apache.spark.sql.{Encoder, Encoders} | ||
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
|
||
object GTEncoders { | ||
implicit def gtGeometryEncoder: Encoder[Geometry] = Encoders.kryo[Geometry] | ||
implicit def gtPointEncoder: Encoder[Point] = ExpressionEncoder() | ||
implicit def gtMultiPointEncoder: Encoder[MultiPoint] = ExpressionEncoder() | ||
implicit def gtLineEncoder: Encoder[Line] = ExpressionEncoder() | ||
implicit def gtMultiLineEncoder: Encoder[MultiLine] = ExpressionEncoder() | ||
implicit def gtPolygonEncoder: Encoder[Polygon] = ExpressionEncoder() | ||
implicit def gtMultiPolygonEncoder: Encoder[MultiPolygon] = ExpressionEncoder() | ||
|
||
implicit def gtFeatureEncoder[G <: Geometry, D](implicit ev1: Encoder[G], ev2: Encoder[D]): Encoder[Feature[G, D]] = Encoders.kryo[Feature[G, D]] | ||
|
||
implicit def gtVectorTileEncoder: Encoder[VectorTile] = Encoders.kryo[VectorTile] | ||
//implicit def gtLayerEncoder: Encoder[Layer] = Encoders.javaSerialization[Layer] | ||
//implicit def gtStrictLayerEncoder: Encoder[StrictLayer] = Encoders.kryo[StrictLayer] | ||
} |
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
119 changes: 119 additions & 0 deletions
119
core/src/main/scala/vectorpipe/vectortile/export/SaveToHadoop.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,119 @@ | ||
/* | ||
* Copyright 2016 Azavea | ||
* | ||
* 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. | ||
*/ | ||
|
||
package geotrellis.spark.io.hadoop | ||
|
||
import geotrellis.spark.render._ | ||
import geotrellis.spark.SpatialKey | ||
|
||
import java.net.URI | ||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.fs.{FileSystem, Path} | ||
import org.apache.spark.sql._ | ||
|
||
import scala.collection.concurrent.TrieMap | ||
|
||
object SaveToHadoop { | ||
/** Saves records from an iterator and returns them unchanged. | ||
* | ||
* @param recs Key, Value records to be saved | ||
* @param keyToUri A function from K (a key) to Hadoop URI | ||
* @param toBytes A function from record to array of bytes | ||
* @param conf Hadoop Configuration to used to get FileSystem | ||
*/ | ||
def saveIterator[K, V]( | ||
recs: Iterator[(K, V)], | ||
keyToUri: K => String, | ||
conf: Configuration | ||
)(toBytes: (K, V) => Array[Byte]): Iterator[(K, V)] = { | ||
val fsCache = TrieMap.empty[String, FileSystem] | ||
|
||
for ( row @ (key, data) <- recs ) yield { | ||
val path = keyToUri(key) | ||
val uri = new URI(path) | ||
val fs = fsCache.getOrElseUpdate( | ||
uri.getScheme, | ||
FileSystem.get(uri, conf)) | ||
val out = fs.create(new Path(path)) | ||
try { out.write(toBytes(key, data)) } | ||
finally { out.close() } | ||
row | ||
} | ||
} | ||
|
||
/** | ||
* Sets up saving to Hadoop, but returns an RDD so that writes can | ||
* be chained. | ||
* | ||
* @param keyToUri A function from K (a key) to a Hadoop URI | ||
*/ | ||
def setup[K]( | ||
dataset: Dataset[(K, Array[Byte])], | ||
keyToUri: K => String | ||
)(implicit ev: Encoder[(K, Array[Byte])]): Dataset[(K, Array[Byte])] = { | ||
import dataset.sparkSession.implicits._ | ||
dataset.mapPartitions { partition => | ||
saveIterator(partition, keyToUri, new Configuration){ (k, v) => v } | ||
} | ||
} | ||
|
||
/** | ||
* Sets up saving to Hadoop, but returns an RDD so that writes can | ||
* be chained. | ||
* | ||
* @param keyToUri A function from K (a key) to a Hadoop URI | ||
* @param toBytes A function from record to array of bytes | ||
*/ | ||
def setup[K, V]( | ||
dataset: Dataset[(K, V)], | ||
keyToUri: K => String, | ||
toBytes: (K, V) => Array[Byte] | ||
)(implicit ev: Encoder[(K, V)]): Dataset[(K, V)] = { | ||
import dataset.sparkSession.implicits._ | ||
val conf = dataset.sparkSession.sparkContext.hadoopConfiguration | ||
dataset.mapPartitions { partition => | ||
saveIterator(partition, keyToUri, new Configuration)(toBytes) | ||
} | ||
} | ||
|
||
/** | ||
* Saves to Hadoop FileSystem, returns an count of records saved. | ||
* | ||
* @param keyToUri A function from K (a key) to a Hadoop URI | ||
*/ | ||
def apply[K]( | ||
dataset: Dataset[(K, Array[Byte])], | ||
keyToUri: K => String | ||
)( implicit ev: Encoder[(K, Array[Byte])]): Long = { | ||
import dataset.sparkSession.implicits._ | ||
setup(dataset, keyToUri).count | ||
} | ||
|
||
/** | ||
* Saves to Hadoop FileSystem, returns an count of records saved. | ||
* | ||
* @param keyToUri A function from K (a key) to a Hadoop URI | ||
* @param toBytes A function from record to array of bytes | ||
*/ | ||
def apply[K, V]( | ||
dataset: Dataset[(K, V)], | ||
keyToUri: K => String, | ||
toBytes: (K, V) => Array[Byte] | ||
)(implicit ev: Encoder[(K, V)]): Long = { | ||
import dataset.sparkSession.implicits._ | ||
setup(dataset, keyToUri, toBytes).count | ||
} | ||
} |
Oops, something went wrong.