【发布时间】:2017-05-21 18:29:43
【问题描述】:
我想通过 spark 摄取许多小文本文件到 parquet。目前,我使用wholeTextFiles 并额外执行一些解析。
更准确地说,这些小文本文件是 ESRi ASCII Grid 文件,每个文件的最大大小约为 400kb。 GeoTools 用于解析它们,如下所述。
您是否看到任何优化的可能性?也许可以避免创建不必要的对象?或者更好地处理小文件的东西。我想知道是否最好只获取文件的路径并手动读取它们而不是使用String -> ByteArrayInputStream。
case class RawRecords(path: String, content: String)
case class GeometryId(idPath: String, value: Double, geo: String)
@transient lazy val extractor = new PolygonExtractionProcess()
@transient lazy val writer = new WKTWriter()
def readRawFiles(path: String, parallelism: Int, spark: SparkSession) = {
import spark.implicits._
spark.sparkContext
.wholeTextFiles(path, parallelism)
.toDF("path", "content")
.as[RawRecords]
.mapPartitions(mapToSimpleTypes)
}
def mapToSimpleTypes(iterator: Iterator[RawRecords]): Iterator[GeometryId] = iterator.flatMap(r => {
val extractor = new PolygonExtractionProcess()
// http://docs.geotools.org/latest/userguide/library/coverage/arcgrid.html
val readRaster = new ArcGridReader(new ByteArrayInputStream(r.content.getBytes(StandardCharsets.UTF_8))).read(null)
// TODO maybe consider optimization of known size instead of using growable data structure
val vectorizedFeatures = extractor.execute(readRaster, 0, true, null, null, null, null).features
val result: collection.Seq[GeometryId] with Growable[GeometryId] = mutable.Buffer[GeometryId]()
while (vectorizedFeatures.hasNext) {
val vectorizedFeature = vectorizedFeatures.next()
val geomWKTLineString = vectorizedFeature.getDefaultGeometry match {
case g: Geometry => writer.write(g)
}
val geomUserdata = vectorizedFeature.getAttribute(1).asInstanceOf[Double]
result += GeometryId(r.path, geomUserdata, geomWKTLineString)
}
result
})
【问题讨论】:
-
如果有帮助,请在此处查看我的答案,但同样是数据框:stackoverflow.com/a/45227410/297113
标签: scala apache-spark optimization geotools