我将更新列表属性的整个值,并在 Titan 1.0 中为其设置一个新值,对于单基数,我可以使用 vertex.property("single_property",new_value)
,并覆盖整个值,但对于 List 类型的基数,新值将添加到属性中(它不会覆盖整个值)。此外,如果我删除该属性并添加新值,在同一事务中,整个操作似乎将被泰坦忽略!因此,我的问题是如何以适当的方式更新列表属性的整个值?
关于phani提供的解决方案,以下代码对我来说不起作用,插入部分起作用,但删除部分不起作用。
keywords = keywordExtractor.getKeywords(getId(nextVertex))
if (keywords.size() > 0) {
nextVertex.property(VertexProperty.Cardinality.single, "post_keyword", keywords.get(0));
keywords.remove(0);
for (String keyword : keywords) {
nextVertex.property(VertexProperty.Cardinality.list, "post_keyword", keyword);
}
}
nextVertex.graph().tx().commit();
下面提供的 Jason 提供的解决方案也不起作用。问题出在删除部分。
keywords = keywordExtractor.getKeywords(getId(nextVertex))
if (keywords.size() > 0) {
nextVertex.graph().traversal().V(nextVertex).properties("post_keyword").drop().iterate();
for (String keyword : keywords) {
nextVertex.property("post_keyword", keyword);
}
}
nextVertex.graph().tx().commit();
我还研究了以下解决方案;也没有用。
keywords = keywordExtractor.getKeywords(getId(nextVertex))
if (keywords.size() > 0) {
Iterator<VertexProperty<Object>> iter = nextVertex.properties("post_keyword");
while(iter.hasNext()){
iter.next().remove();
}
for (String keyword : keywords) {
nextVertex.property("post_keyword", keyword);
}
}
nextVertex.graph().tx().commit();
最佳答案
Phani 提供了一个可靠的答案,非常适合 TinkerGraph 。 Titan 中的行为略有不同,因此我的建议是首先 drop()
属性,然后向其中添加新项目。
gremlin> graph = TitanFactory.open('inmemory'); g = graph.traversal()
==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
gremlin> mgmt = graph.openManagement()
==>com.thinkaurelius.titan.graphdb.database.management.ManagementSystem@71a06021
gremlin> name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(Cardinality.LIST).make()
==>name
gremlin> mgmt.commit()
==>null
gremlin> v = g.addV('name','marko','name','marko a. rodriguez').next()
==>v[4312]
gremlin> g.V(v).properties('name').count()
==>2
gremlin> g.V(v).properties('name').drop().iterate()
gremlin> g.V(v).properties('name').count()
==>0
gremlin> v.property(list, 'name', 'm. a. rodriguez')
==>vp[name->m. a. rodriguez]
gremlin> g.V(v).properties('name').count()
==>1
关于java - 使用 Titan 图数据库中的列表基数更新顶点属性的整个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540170/