我想知道一个人可能来自哪里,根据他们所关注的人,以及大致的纬度和经度来获得这个国家。我有两种类型的节点:用户(包含名称,可能是lat和lng)和国家(包含名称)。我还有两种类型的边:Follow和LivesIn(包含lat和lng)。
目前,Account和LivesIn边缘都包含lat和lng,因为我不完全确定哪个更好,但此刻我倾向于把它放在边缘。< / p>
以下是一个包含五个用户的示例网络。其中三个我知道他们来自哪里。现在我想对Alice来自哪里进行有根据的猜测:
CREATE CLASS Account EXTENDS V
CREATE PROPERTY Account.name string
CREATE PROPERTY Account.lat double
CREATE PROPERTY Account.lng double
CREATE CLASS Country EXTENDS V
CREATE PROPERTY Country.countryname string
CREATE CLASS LivesIn EXTENDS E
CREATE PROPERTY LivesIn.lat double
CREATE PROPERTY LivesIn.lng double
CREATE CLASS Follows EXTENDS E
CREATE VERTEX Account SET name='Alice'
CREATE VERTEX Account SET name='Bob', lat=50.503887, lng=4.469936 /* Belgium */
CREATE VERTEX Account SET name='Carol', lat=51.165691, lng=10.451526 /* Germany */
CREATE VERTEX Account SET name='Eve', lat=51.115691, lng=10.481526 /* Germany */
CREATE VERTEX Account SET name='Dave'
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Alice') TO (SELECT FROM Account WHERE name='Bob')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Alice') TO (SELECT FROM Account WHERE name='Carol')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Alice') TO (SELECT FROM Account WHERE name='Eve')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Alice') TO (SELECT FROM Account WHERE name='Dave')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Bob') TO (SELECT FROM Account WHERE name='Alice')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Carol') TO (SELECT FROM Account WHERE name='Alice')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Eve') TO (SELECT FROM Account WHERE name='Alice')
CREATE EDGE Follows FROM (SELECT FROM Account WHERE name='Dave') TO (SELECT FROM Account WHERE name='Alice')
CREATE VERTEX Country SET countryname='Belgium'
CREATE VERTEX Country SET countryname='Germany'
CREATE EDGE LivesIn FROM (SELECT FROM Account WHERE name='Bob') TO (SELECT FROM Country WHERE countryname='Belgium') SET lat=50.503887, lng=4.469936
CREATE EDGE LivesIn FROM (SELECT FROM Account WHERE name='Carol') TO (SELECT FROM Country WHERE countryname='Germany') SET lat=51.165691, lng=10.451526
CREATE EDGE LivesIn FROM (SELECT FROM Account WHERE name='Eve') TO (SELECT FROM Country WHERE countryname='Germany') SET lat=51.115691, lng=10.481526
我的问题是,是否有一种有效的方法可以使用OrientDB中的特定sql命令实现此目的,或者是否需要新功能。
我得到了一些小问题,比如获得所有传出的Follows
连接:
SELECT out("Follows") FROM Account WHERE name='Alice'
但我无法真正从那里获得所有LivesIn边缘。
或者我可以在OrientDB中创建一个新函数,因为它们也here。类似的东西:
var gdb = orient.getGraphNoTx();
var v = gdb.command("sql", "select from Account where name='" + name + "'");
neighbours = v[0].getRecord().field("out_Follows").iterator();
var result = []
print('\n');
country_dict = {}
while(neighbours.hasNext()) {
var neighbour = neighbours.next();
var temp = neighbour.field("in").field("out_LivesIn");
if(temp) {
it = temp.iterator();
print(it.next());
// Count each country and keep track of sum of lat and lng so it can be divided
// once all neighbours have been visited
}
}
但是,这并没有真正使用任何(可能有效的?)内置的sql方法。考虑到一个人可能会关注成千上万的其他帐户。
有人会建议我如何解决这个问题吗?
答案 0 :(得分:3)
尝试此查询
True