/
AccumuloExportCommandTest.scala
272 lines (246 loc) · 11.7 KB
/
AccumuloExportCommandTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***********************************************************************
* Copyright (c) 2013-2024 Commonwealth Computer Research, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at
* http://www.opensource.org/licenses/apache2.0.php.
***********************************************************************/
package org.locationtech.geomesa.accumulo.tools.export
import org.apache.commons.csv.CSVFormat
import org.apache.commons.io.IOUtils
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.parquet.filter2.compat.FilterCompat
import org.geotools.api.feature.simple.{SimpleFeature, SimpleFeatureType}
import org.geotools.data.DataUtilities
import org.geotools.data.shapefile.ShapefileDataStore
import org.geotools.filter.text.ecql.ECQL
import org.geotools.util.URLs
import org.geotools.wfs.GML
import org.junit.runner.RunWith
import org.locationtech.geomesa.accumulo.TestWithFeatureType
import org.locationtech.geomesa.accumulo.data.AccumuloDataStoreParams
import org.locationtech.geomesa.arrow.io.SimpleFeatureArrowFileReader
import org.locationtech.geomesa.convert.EvaluationContext
import org.locationtech.geomesa.convert.text.DelimitedTextConverter
import org.locationtech.geomesa.convert2.SimpleFeatureConverter
import org.locationtech.geomesa.features.ScalaSimpleFeature
import org.locationtech.geomesa.features.avro.io.AvroDataFileReader
import org.locationtech.geomesa.fs.storage.common.jobs.StorageConfiguration
import org.locationtech.geomesa.fs.storage.orc.OrcFileSystemReader
import org.locationtech.geomesa.fs.storage.parquet.ParquetPathReader
import org.locationtech.geomesa.tools.export.formats.ExportFormat
import org.locationtech.geomesa.utils.bin.BinaryOutputEncoder
import org.locationtech.geomesa.utils.collection.SelfClosingIterator
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes
import org.locationtech.geomesa.utils.io.{PathUtils, WithClose, WithStore}
import org.specs2.runner.JUnitRunner
import java.io.{File, FileInputStream, FileWriter}
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.{Collections, Date}
import scala.util.{Failure, Success}
@RunWith(classOf[JUnitRunner])
class AccumuloExportCommandTest extends TestWithFeatureType {
import scala.collection.JavaConverters._
override val spec: String = "name:String,dtg:Date,*geom:Point:srid=4326"
val excludes = Seq(ExportFormat.Null)
val formats = ExportFormat.Formats.filterNot(excludes.contains)
val features = List(
// note: shapefiles don't support timestamps, so we leave them at 00:00...
ScalaSimpleFeature.create(sft, "id1", "name1", "2016-01-01T00:00:00.000Z", "POINT(1 0)"),
ScalaSimpleFeature.create(sft, "id2", "name2", "2016-01-02T00:00:00.000Z", "POINT(0 2)")
)
def createCommand(): AccumuloExportCommand = {
val command = new AccumuloExportCommand()
command.params.user = dsParams(AccumuloDataStoreParams.UserParam.key)
command.params.instance = dsParams(AccumuloDataStoreParams.InstanceNameParam.key)
command.params.zookeepers = dsParams(AccumuloDataStoreParams.ZookeepersParam.key)
command.params.password = dsParams(AccumuloDataStoreParams.PasswordParam.key)
command.params.catalog = dsParams(AccumuloDataStoreParams.CatalogParam.key)
command.params.featureName = sftName
command.params.maxFeatures = 100 // suppress leaflet complaints
command
}
var out: java.nio.file.Path = _
step {
out = Files.createTempDirectory("gm-export-fs-test")
addFeatures(features)
}
"Export command" should {
"export to different file formats" in {
forall(formats) { format =>
val file = s"$out/${format.name}/base/out.${format.extensions.head}"
val command = createCommand()
command.params.file = file
command.execute()
readFeatures(format, file) must containTheSameElementsAs(features)
}
}
"support filtering" in {
forall(formats) { format =>
val file = s"$out/${format.name}/filter/out.${format.extensions.head}"
val command = createCommand()
command.params.file = file
command.params.cqlFilter = ECQL.toFilter("dtg = '2016-01-01T00:00:00.000Z'")
command.execute()
readFeatures(format, file) must containTheSameElementsAs(features.take(1))
}
}
"support relational projections" in {
forall(formats) { format =>
val file = s"$out/${format.name}/project/out.${format.extensions.head}"
val command = createCommand()
command.params.file = file
command.params.attributes = List("dtg", "geom", "id").asJava
command.execute()
val tsft = SimpleFeatureTypes.createType(sft.getTypeName, "dtg:Date,*geom:Point:srid=4326")
readFeatures(format, file, tsft) must containTheSameElementsAs(features.map(ScalaSimpleFeature.retype(tsft, _)))
}
}
"support sorting" in {
// exclude BIN as we only sort per-batch but not globally
forall(formats.filter(_ != ExportFormat.Bin)) { format =>
val file = s"$out/${format.name}/sort/out.${format.extensions.head}"
val command = createCommand()
command.params.file = file
command.params.sortFields = Collections.singletonList("dtg")
command.execute()
readFeatures(format, file) mustEqual features
}
// exclude BIN as we only support sort in ascending order
forall(formats.filter(_ != ExportFormat.Bin)) { format =>
val file = s"$out/${format.name}/sort-rev/out.${format.extensions.head}"
val command = createCommand()
command.params.file = file
command.params.sortFields = Collections.singletonList("dtg")
command.params.sortDescending = true
command.execute()
readFeatures(format, file) mustEqual features.reverse
}
}
"support max features" in {
// note: arrow and bin use server side aggregation, so we can't get an exact feature limit
forall(formats.filter(f => f != ExportFormat.Arrow && f != ExportFormat.Bin)) { format =>
val file = s"$out/${format.name}/max/out.${format.extensions.head}"
val command = createCommand()
command.params.file = file
command.params.maxFeatures = 1
command.execute()
val read = readFeatures(format, file)
read must haveLength(1)
features must containAllOf(read)
}
}
}
step {
PathUtils.deleteRecursively(out)
}
def readFeatures(format: ExportFormat, file: String, sft: SimpleFeatureType = this.sft): Seq[SimpleFeature] = {
format match {
case ExportFormat.Arrow => readArrow(file)
case ExportFormat.Avro => readAvro(file)
case ExportFormat.AvroNative => readAvro(file)
case ExportFormat.Bin => readBin(file, sft)
case ExportFormat.Csv => readCsv(file)
case ExportFormat.Json => readJson(file, sft)
case ExportFormat.Leaflet => readLeaflet(file, sft)
case ExportFormat.Orc => readOrc(file, sft)
case ExportFormat.Parquet => readParquet(file, sft)
case ExportFormat.Shp => readShp(file, sft)
case ExportFormat.Tsv => readTsv(file)
case ExportFormat.Gml2 => readGml2(file, sft)
case ExportFormat.Gml3 => readGml3(file, sft)
}
}
def readArrow(file: String): Seq[SimpleFeature] = {
WithClose(SimpleFeatureArrowFileReader.streaming(() => new FileInputStream(file))) { reader =>
SelfClosingIterator(reader.features()).map(ScalaSimpleFeature.copy).toList
}
}
def readAvro(file: String): Seq[SimpleFeature] =
WithClose(new AvroDataFileReader(new FileInputStream(file)))(_.toList)
def readBin(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
val bytes = IOUtils.toByteArray(new FileInputStream(file))
// hack - set id and name from original features since they aren't exported in the bin format
bytes.grouped(16).map(BinaryOutputEncoder.decode).toSeq.map { values =>
val dtg = new Date(values.dtg)
val f1 = features.find(_.getAttribute("dtg") == dtg).get
val attributes = sft.getAttributeDescriptors.asScala.map(_.getLocalName).map {
case "geom" => s"POINT (${values.lon} ${values.lat})"
case "dtg" => dtg
case "name" => f1.getAttribute("name")
}
ScalaSimpleFeature.create(sft, f1.getID, attributes.toSeq: _*)
}
}
def readCsv(file: String): Seq[SimpleFeature] =
DelimitedTextConverter.magicParsing(sft.getTypeName, new FileInputStream(file)).toList
def readJson(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
val converter = SimpleFeatureConverter.infer(() => new FileInputStream(file), None, EvaluationContext.inputFileParam(file)) match {
case Failure(_) => ko(s"could not create converter from $file"); null: SimpleFeatureConverter
case Success((s, c)) => SimpleFeatureConverter(s, c)
}
val result = Seq.newBuilder[SimpleFeature]
val names = sft.getAttributeDescriptors.asScala.map(_.getLocalName)
WithClose(converter.process(new FileInputStream(file))) { features =>
features.foreach { f =>
val copy = new ScalaSimpleFeature(sft, f.getID)
names.foreach(a => copy.setAttribute(a, f.getAttribute(a)))
result += copy
}
}
result.result()
}
def readLeaflet(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
val html = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8)
val i = html.indexOf("var points = ") + 13
val json = html.substring(i, html.indexOf(";", i))
val tmp = Files.createTempFile("gm-export-leaflet", ".json").toFile
try {
WithClose(new FileWriter(tmp))(IOUtils.write(json, _))
readJson(tmp.getAbsolutePath, sft)
} finally {
if (!tmp.delete()) {
tmp.deleteOnExit()
}
}
}
def readOrc(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
val path = new Path(PathUtils.getUrl(file).toURI)
WithClose(new OrcFileSystemReader(sft, new Configuration, None, None).read(path)) { iter =>
iter.map(ScalaSimpleFeature.copy).toList
}
}
def readParquet(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
val path = new Path(PathUtils.getUrl(file).toURI)
val conf = new Configuration()
StorageConfiguration.setSft(conf, sft)
WithClose(new ParquetPathReader(conf, sft, FilterCompat.NOOP, None, None).read(path)) { iter =>
iter.map(ScalaSimpleFeature.copy).toList
}
}
def readShp(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
WithStore[ShapefileDataStore](Map("url" -> URLs.fileToUrl(new File(file)))) { ds =>
// hack - set id from original features since USE_PROVIDED_FID is not supported in shapefiles
SelfClosingIterator(ds.getFeatureReader).toList.map { f =>
val dtg = f.getAttribute("dtg")
val f1 = features.find(_.getAttribute("dtg") == dtg).get
val attributes = sft.getAttributeDescriptors.asScala.map(_.getLocalName).map {
case "geom" => f.getAttribute(0)
case "dtg" => dtg
case "name" => f.getAttribute("name")
}
ScalaSimpleFeature.create(sft, f1.getID, attributes.toSeq: _*)
}
}
}
def readTsv(file: String): Seq[SimpleFeature] =
DelimitedTextConverter.magicParsing(sft.getTypeName, new FileInputStream(file), CSVFormat.TDF).toList
def readGml2(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = readGml3(file, sft)
def readGml3(file: String, sft: SimpleFeatureType): Seq[SimpleFeature] = {
SelfClosingIterator(new GML(GML.Version.GML3).decodeFeatureIterator(new FileInputStream(file))).toList.map { f =>
ScalaSimpleFeature.copy(DataUtilities.reType(sft, f))
}
}
}