9.10. GeoMesa Scala Console¶
The GeoMesa tools have a scala-console command that starts a Scala REPL (read/evaluate/print/loop) configured
for use with GeoMesa. The command will place the GeoMesa classpath and configuration
for that distribution on the REPL’s classpath. Additionally it will preload commonly
used imports. The command also comes with tooling that will provide the option to
download and run the appropriate Scala version if it is not available. This is a
portable install and will not alter the machine’s current configuration.
9.10.1. Example Usage¶
The following example was performed against the Redis data store generated by the
GeoMesa Redis Quick Start. It shows how to use the scala-console command to connect
to a data store, discover the feature type names, get the schema and query for data.
Note
This code is running against a Redis instance populated by the Redis quick start. To reproduce this code exactly you must run the GeoMesa Redis Quick Start first.
First we run the command, this starts up the Scala REPL with all of the resources we need.
$ bin/geomesa-redis scala-console
After waiting for a few seconds, it will silently import some commonly used libraries and then present us with the REPL prompt.
Next we get a connection to the Redis Datastore.
scala> val dsParams = Map("redis.url" -> "localhost:6379", "redis.catalog" -> "geomesa")
dsParams: scala.collection.immutable.Map[String,String] = Map(redis.url -> localhost:6379, redis.catalog -> geomesa)
scala> val ds = DataStoreFinder.getDataStore(dsParams.asJava)
ds: org.geotools.api.data.DataStore = org.locationtech.geomesa.redis.data.RedisDataStore@6aa6c17
Now we do some example discovery to see what feature types and schemas are stored in the database.
scala> ds.getTypeNames()
res0: Array[String] = Array(gdelt-quickstart)
scala> val sft = ds.getSchema("gdelt-quickstart")
sft: org.geotools.api.feature.simple.SimpleFeatureType = SimpleFeatureType gdelt-quickstart GLOBALEVENTID:String,Actor1Name:String,Actor1CountryCode:String,Actor2Name:String,Actor2CountryCode:String,EventCode:String,NumMentions:Integer,NumSources:Integer,NumArticles:Integer,ActionGeo_Type:Integer,ActionGeo_FullName:String,ActionGeo_CountryCode:String,dtg:Date,*geom:Point:srid=4326
In order to sample the data we create a Query and a FeatureReader and
then run the query.
scala> val query = new Query(sft.getName.toString())
query: org.geotools.api.data.Query =
Query:
feature type: gdelt-quickstart
filter: Filter.INCLUDE
[properties: ALL ]
scala> val reader = ds.getFeatureReader(query, Transaction.AUTO_COMMIT)
reader: org.geotools.api.data.FeatureReader[org.geotools.api.feature.simple.SimpleFeatureType,org.geotools.api.feature.simple.SimpleFeature] = org.locationtech.geomesa.index.geotools.GeoMesaFeatureReader$GeoMesaFeatureReaderWithAudit$ResultReaderWithAudit@3e3a07ab
Next, we consume the FeatureReader, printing out the results.
scala> while (reader.hasNext()) { println(reader.next().toString()) }
LazyMutableSimpleFeature:719024956:719024956|||GANG||120|6|1|6|1|Brazil|BR|Sun Dec 31 19:00:00 EST 2017|POINT (-55 -10)
LazyMutableSimpleFeature:719024898:719024898|||SYDNEY|AUS|010|14|2|14|4|Sydney, New South Wales, Australia|AS|Sun Dec 31 19:00:00 EST 2017|POINT (151.217 -33.8833)
LazyMutableSimpleFeature:719024882:719024882|SECURITY COUNCIL||PYONGYANG|PRK|163|2|1|2|1|Russia|RS|Sun Dec 24 19:00:00 EST 2017|POINT (100 60)
LazyMutableSimpleFeature:719024881:719024881|||RUSSIA|RUS|042|2|1|2|3|Allegheny County, Pennsylvania, United States|US|Sun Dec 24 19:00:00 EST 2017|POINT (-80.1251 40.6253)
LazyMutableSimpleFeature:719025149:719025149|ARGENTINE|ARG|DIOCESE||010|1|1|1|4|Corrientes, Corrientes, Argentina|AR|Sun Dec 31 19:00:00 EST 2017|POINT (-58.8341 -27.4806)
...
Finally, we cleanup our connections.
scala> reader.close()
scala> ds.dispose()