内存中索引¶
这个 geomesa-memory 模块提供内存中的缓存 SimpleFeature
s that supports indexing and filtering, using the CQEngine 集合查询引擎。这是由 GeoCQEngine
班级。
索引配置¶
在创建 GeoCQEngine
中,要索引的属性以元组列表的形式传入 (name, type)
,在哪里 name 对应于属性名称,并且 type 对应于CQEngine索引类型:
索引类型 |
属性类型 |
描述 |
---|---|---|
|
任何 |
根据要编制索引的属性类型选择索引类型 |
|
日期和数字类型 |
支持相等、大于和小于 |
|
细绳 |
支持字符串匹配操作 |
|
字符串、整型或长整型 |
支持唯一值。如果存在重复值,则会导致异常 |
|
字符串、整型或长整型 |
支持平等 |
|
几何图形 |
几何类型的自定义索引 |
如果没有合适的索引用于查询,则将搜索整个数据集。
示例用法¶
import org.geotools.filter.text.ecql.ECQL
import org.locationtech.geomesa.memory.cqengine.GeoCQEngine
import org.locationtech.geomesa.memory.cqengine.utils.CQIndexType
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes
// setup
val spec = "Who:String,*Where:Point:srid=4326"
val sft = SimpleFeatureTypes.createType("test", spec)
def buildFeature(sft: SimpleFeatureType, fid: Int): SimpleFeature = ???
// create a new cache
val cq = new GeoCQEngine(sft, Seq(("Who", CQIndexType.DEFAULT), ("Where", CQIndexType.GEOMETRY)))
// add a collection of features
cq.insert(Seq.tabulate(1000)(i => buildFeature(sft, i)))
// add a single feature
val feature = buildFeature(sft, 1001)
cq.insert(feature)
// remove a single feature
cq.remove(feature.getID)
// get an iterator with all features that match a filter
val filter = ECQL.toFilter("Who = 'foo' AND BBOX(Where, 0, 0, 180, 90)")
val reader = cq.query(filter)
// clear the cache
cq.clear()
您还可以选择几何图形索引类型。默认情况下,使用Bucket索引,以便在频繁插入或更新时提供更好的性能。如果您需要在一个不变的(或几乎没有变化的)数据集合中进行搜索,您可以使用QuadTree或SRTtree索引。为此,您必须创建GeoCqEngine,如下所示:
val cq1 = new GeoCQEngine(featureType, Seq(("Who", CQIndexType.DEFAULT), ("Where", CQIndexType.GEOMETRY)),geoIndexType = GeoIndexType.STRtree,geoIndexParam = Option.apply(new STRtreeIndexParam(/*nodeCapacity*/10)))
val cq2 = new GeoCQEngine(featureType, Seq(("Who", CQIndexType.DEFAULT), ("Where", CQIndexType.GEOMETRY)),geoIndexType = GeoIndexType.QuadTree)