我正在开发一个可以很好地使用图形数据库(Titan)的应用程序,但它在处理具有许多边的顶点(例如supernodes )时存在问题。
上面的超级节点链接指向Titan作者的一篇博客文章,解释了一种解决问题的方法。解决方案似乎是通过对边进行过滤来减少顶点的数量。
不幸的是,我想要groupCount边或顶点的属性。例如,我有一百万个用户,每个用户都属于一个国家。如何才能快速计算出每个国家/地区的用户数量?
到目前为止,我所尝试的内容可以在这个精心设计的groovy脚本中显示出来:
g = TitanFactory.open('titan.properties') // Cassandra
r = new Random(100)
people = 1e6
def newKey(g, name, type) {
return g
.makeType()
.name(name)
.simple()
.functional()
.indexed()
.dataType(type)
.makePropertyKey()
}
def newLabel(g, name, key) {
return g
.makeType()
.name(name)
.primaryKey(key)
.makeEdgeLabel()
}
country = newKey(g, 'country', String.class)
newLabel(g, 'lives', country)
g.stopTransaction(SUCCESS)
root = g.addVertex()
countries = ['AU', 'US', 'CN', 'NZ', 'UK', 'PL', 'RU', 'NL', 'FR', 'SP', 'IT']
(1..people).each {
country = countries[(r.nextFloat() * countries.size()).toInteger()]
g.startTransaction()
person = g.addVertex([name: 'John the #' + it])
g.addEdge(g.getVertex(root.id), person, 'lives', [country: country])
g.stopTransaction(SUCCESS)
}
t0 = new Date().time
m = [:]
root = g.getVertex(root.id)
root.outE('lives').country.groupCount(m).iterate()
t1 = new Date().time
println "groupCount seconds: " + ((t1 - t0) / 1000)基本上是一个根节点(对于Titan没有“所有”节点查找),通过具有country属性的边链接到许多person。当我对一百万个顶点运行groupCount()时,它需要一分钟以上的时间。
我意识到Titan可能会迭代每条边并收集计数,但有没有办法在Titan或任何其他图形数据库中运行得更快?是否可以对索引本身进行计数,使其不必遍历?我的索引正确吗?
发布于 2012-11-20 06:30:11
如果您将' country‘设置为'lives’标签的primary key,那么您可以更快地检索特定国家的所有人。然而,在您的例子中,您感兴趣的是一个组计数器,它需要检索根节点的所有边,以便迭代它们并存储国家/地区。
因此,这个分析查询更适合图形分析框架Faunus。它不需要根顶点,因为它通过完整的数据库扫描来执行groupcount,从而避免了超节点问题。Faunus还使用Gremlin作为查询语言,因此您只需对查询稍作修改:
g.V.country.groupCount.cap...HTH,Matthias
https://stackoverflow.com/questions/13133339
复制相似问题