我正在尝试为顶点标签创建索引。顶点创建如下
val v0 = graph + "A"
我的每个 gremlin 查询都基于顶点标签。获取以下警告消息
警告 c.t.t.g.transaction.StandardTitanTx - 查询需要迭代所有顶点 [(~label = 301)]。为了获得更好的性能,请使用索引
项目使用Titan + cassandra(存储后端),以下是使用的SBT依赖项,
"com.michaelpollmeier" %% "gremlin-scala" % "3.0.2-incubating.2",
"com.thinkaurelius.titan" % "titan-core" % "1.0.0",
"com.thinkaurelius.titan" % "titan-cassandra" % "1.0.0",
"com.netflix.astyanax" % "astyanax-cassandra" % "3.9.0",
"com.netflix.astyanax" % "astyanax-core" % "3.9.0",
"com.netflix.astyanax" % "astyanax-thrift" % "3.9.0"
创建索引如下,
mgmt.makePropertyKey("endpoint").dataType(classOf[String]).make();
mgmt.buildIndex("endpoint",classOf[Vertex]).addKey(name1).unique().buildCompositeIndex()
mgmt.commit()
graph.tx().commit()
出现此错误
com.thinkaurelius.titan.core.SchemaViolationException:为键 [~T$SchemaName] 和值 [rtendpoint] 添加此属性违反了唯一性约束 [SystemIndex#~T$SchemaName]
最佳答案
根据this :
you can always easily access the underlying Gremlin-Java objects if needed, e.g. to access graph db specifics things like indexes
所以我认为该过程应该与定义的过程相同here .
基于该假设,首先,您将索引应用于属性而不是标签。然而,在加载数据之前创建图形模式是一个好主意。话虽如此,您可能想要执行以下操作:
TitanManagement management = graph.openManagement();
<强>1。定义您的顶点标签
这可以通过以下方式完成:
VertexLabel foundLabel = management.getVertexLabel("A");
if(foundLabel == null)
management.makeVertexLabel("A").make();
<强>2。定义您的属性
这让 Titan 知道它可以索引哪些属性:
if (management.getPropertyKey("ID") == null) {
management.makePropertyKey("ID").dataType(String.class).make();
}
<强>3。定义索引属性
当您为属性建立索引时,对这些属性的遍历将会快得多:
TitanIndex index = management.getGraphIndex("byID");
if(index == null) {
PropertyKey key = management.getPropertyKey("ID");
TitanManagement.IndexBuilder indexBuilder = management.buildIndex("byID", Vertex.class).addKey(key);
if (isUnique) //Do you want the property to be unique ?
indexBuilder.unique();
indexBuilder.buildCompositeIndex();
}
关于scala - 是否可以为顶点中的标签建立索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823969/