首页 文章

是否可以为顶点中的标签构建索引

提问于
浏览
1

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

val v0 = graph + "A"

我的每个gremlin查询基于顶点标签 . 获取下面的警告消息

WARN c.t.t.g.transaction.StandardTitanTx - Query requires iterating over all vertices [(~label = 301)]. For better performance, use indexes

项目使用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:为key [~T $ SchemaName]和value [rtendpoint]添加此属性违反了唯一性约束[SystemIndex#~T $ SchemaName]

1 回答

  • 2

    根据this

    如果需要,您可以随时轻松访问底层的Gremlin-Java对象,例如:访问图表db特定的索引之类的东西

    所以我假设该过程应该与定义的过程相同here .

    基于该假设,首先,您将索引应用于不属于标签的属性 . 但是,在高级加载数据时创建图形模式是个好主意 . 话虽这么说你可能想要做以下事情:

    TitanManagement management = graph.openManagement();
    

    1. Define Your Vertex Labels

    这可以通过以下方式完成:

    VertexLabel foundLabel = management.getVertexLabel("A");
    if(foundLabel == null)
        management.makeVertexLabel("A").make();
    

    2. Define your properties

    这让Titan知道它可以索引哪些属性:

    if (management.getPropertyKey("ID") == null) {
        management.makePropertyKey("ID").dataType(String.class).make();
    }
    

    3. Define the Indexed Properties

    索引属性时,对这些属性的遍历将更快:

    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();
    }
    

相关问题