8.10. GeoMesa Scala控制台¶
GeoMesa工具具有 scala-console
启动配置为与GeoMesa一起使用的Scala REPL(读取/评估/打印/循环)的命令。该命令将把该发行版的GeoMesa类路径和配置放在REPL的类路径上。此外,它还将预加载常用的导入。该命令还附带工具,如果相应版本的Scala不可用,该工具将提供下载并运行该版本的选项。这是便携安装,不会改变机器的当前配置。
8.10.1. 用法示例¶
对象生成的文件系统数据存储执行以下示例 GeoMesa文件系统快速入门 。它演示了如何使用 scala-console
命令连接到文件系统数据存储区、发现功能类型名称、获取架构并查询数据。
备注
此代码针对文件系统数据存储区的输出运行。要准确地重现此代码,您必须首先运行文件系统数据存储区快速入门。
首先,我们运行该命令,这将启动Scala REPL,其中包含我们需要的所有资源。
$ bin/geomesa-fs scala-console
我们看到它导入了常用的库,并向我们显示REPL提示符。
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>
接下来,我们将连接到文件系统数据存储区。
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
现在,我们进行一些示例发现,以查看数据库中存储了哪些要素类型和模式。
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)
为了对数据进行采样,我们创建了一个 Query
以及一个 FeatureReader
然后运行查询。
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
接下来,我们使用 FeatureReader
,将结果打印出来。
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)
...
最后,我们清理我们的连接。
scala> reader.close()
scala> ds.dispose()