scala - 是否可以为顶点中的标签建立索引

标签 scala graph-databases titan gremlin gremlin-server

我正在尝试为顶点标签创建索引。顶点创建如下

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]).m‌​ake(); 

mgmt.buildIndex("endpoint",classOf[Vertex]).addKey(name1).un‌​ique().buildComposit‌​eIndex() 

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/

相关文章:

scala - 在 Scala 中,对案例类类标签使用反射来查找伴随对象的应用方法

orientdb select 不返回所有顶点

neo4j - 我应该使用 TitanDB 吗?

java - Titan Factory.open() 配置

java - 使用 TITAN DB 手动安装 gremlin 服务器

scala - <scala> 范围委托(delegate)在 SBT 中如何工作?

scala - Apache Spark和域驱动设计

scala - Mapreduce 使用 Scala 错误 : java. lang.ClassNotFoundException : scala. Predef$

node.js - 避免 oriento 中的查询超时(OrientDB 的 Node.js 驱动程序)

elasticsearch - 在titan图的同一键上创建复合索引和混合索引