8.10. GeoMesa Scala Console¶
The GeoMesa tools have a scala-console
command that starts a Scala REPL 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 version Scala if it is not available. This is a
portable install and will not alter the machine’s current configuration.
8.10.1. Example Usage¶
The following example was performed against the FileSystem data store generated by the
GeoMesa FileSystem Quick Start. It shows how to use the scala-console
command to connect
to a FileSystem Datastore, discover the feature type names, get the schema and query for data.
Note
This code is running against the output of the FileSystem Datastore. To reproduce this code exactly you must run the FileSystem Datastore Quickstart first.
First we run the command, this starts up the Scala REPL with all of the resources we need.
$ bin/geomesa-fs scala-console
We see it import commonly used libraries and present us with the REPL prompt.
Loading /tmp/geomesa-fs_2.11-2.0.0-SNAPSHOT/conf/.scala_repl_init...
import org.geotools.data._
import org.geotools.filter.text.ecql.ECQL
import org.opengis.feature.simple._
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes
import org.locationtech.geomesa.features.ScalaSimpleFeature
import org.locationtech.geomesa.utils.collection.SelfClosingIterator
import scala.collection.JavaConverters._
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.
scala>
Next we get a connection to the FileSystem Datastore.
scala> val dsParams = Map("fs.path" -> "file:///tmp/fsds/", "fs.encoding" -> "parquet")
dsParams: scala.collection.immutable.Map[String,String] = Map(fs.path -> file:///tmp/fsds/, fs.encoding -> parquet)
scala> val ds = DataStoreFinder.getDataStore(dsParams.asJava)
ds: org.geotools.data.DataStore = org.locationtech.geomesa.fs.FileSystemDataStore@27a7ef08
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.opengis.feature.simple.SimpleFeatureType = SimpleFeatureTypeImpl gdelt-quickstart identified extends Feature(GLOBALEVENTID:GLOBALEVENTID,Actor1Name:Actor1Name,Actor1CountryCode:Actor1CountryCode,Actor2Name:Actor2Name,Actor2CountryCode:Actor2CountryCode,EventCode:EventCode,NumMentions:NumMentions,NumSources:NumSources,NumArticles:NumArticles,ActionGeo_Type:ActionGeo_Type,ActionGeo_FullName:ActionGeo_FullName,ActionGeo_CountryCode:ActionGeo_CountryCode,dtg:dtg,geom:geom)
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.data.Query =
Query:
feature type: gdelt-quickstart
filter: Filter.INCLUDE
[properties: ALL ]
scala> val reader = ds.getFeatureReader(query, Transaction.AUTO_COMMIT)
reader: org.geotools.data.FeatureReader[org.opengis.feature.simple.SimpleFeatureType,org.opengis.feature.simple.SimpleFeature] = org.geotools.data.simple.DelegateSimpleFeatureReader@7bd96822
Next, we consume the FeatureReader
, printing out the results.
scala> while (reader.hasNext()) { println(reader.next().toString()) }
ScalaSimpleFeature:719024956:719024956|||GANG||120|6|1|6|1|Brazil|BR|Sun Dec 31 19:00:00 EST 2017|POINT (-55 -10)
ScalaSimpleFeature: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)
ScalaSimpleFeature:719024882:719024882|SECURITY COUNCIL||PYONGYANG|PRK|163|2|1|2|1|Russia|RS|Sun Dec 24 19:00:00 EST 2017|POINT (100 60)
ScalaSimpleFeature: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)
ScalaSimpleFeature: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()