请求帮助通过 R-neo4j 进行 Cypher 查询
Request for help wirting Cypher query through R-neo4j
我刚开始使用 R、neo4j 和 R-neo4j 编程,所以如果我的问题很简单,请谅解。
我使用 R-neo4j 和以下 R 项目代码 [2] 创建了以下数据库(请附上照片)[1]。
该数据库包含四名玩家之间的电脑游戏比赛结果。数据集由四个节点组成,玩家 1 到玩家 4。节点通过关系 "defeats" 连接,表示比赛的结果。每个关系都有两个标签条目,包含以下数据:judge、game。
从使用Cypher查询的图数据库中,我想提取如下形式的数据(请提供[1]中的图片):
Winning player Loosing player Game Judge
player 1 player 4 Starcraft player 2
player 1 player 4 LOL player 3
player 4 player 1 LOL player 2
player 1 player 4 Starcraft player 3
player 1 player 2 LOL player 3
player 2 player 1 LOL player 4
player 4 player 1 Starcraft player 4
我想查询(R-neo4j环境下首选)图数据库,输入"player 1"返回上面的table
我希望我的问题很清楚并且有人可以帮助我。
祝你有美好的一天。
基督教徒
[2] R (Rneo4j) 代码:
clear(graph)
Y
player1 = createNode(graph,"user",ID="Player 1",male=T)
player2 = createNode(graph,"user",ID="Player 2",male=T)
player3 = createNode(graph,"user",ID="Player 3",male=F)
player4 = createNode(graph,"user",ID="Player 4",male=F)
addConstraint(graph,"user","ID")
rel1 = createRel(player1,"defeats",player4)
rel2 = createRel(player1,"defeats",player4)
rel3 = createRel(player4,"defeats",player1)
rel4 = createRel(player1,"defeats",player4)
rel5 = createRel(player1,"defeats",player2)
rel6 = createRel(player2,"defeats",player1)
rel7 = createRel(player3,"defeats",player1)
rel1 = updateProp(rel1, game = "Starcraft", judge = "Player 2")
rel2 = updateProp(rel2, game = "League of Legends", judge = "Player 3")
rel3 = updateProp(rel3, game = "League of Legends", judge = "Player 2")
rel4 = updateProp(rel4, game = "Starcraft", judge = "Player 3")
rel5 = updateProp(rel5, game = "League of Legends", judge = "Player 3")
rel6 = updateProp(rel6, game = "League of Legends", judge = "Player 4")
rel7 = updateProp(rel7, game = "Starcraft", judge = "Player 4")
几件事。如果您想使用 clear(graph)
而不必输入 "Y",您可以使用 clear(graph, input=F)
。此外,如果您不知道,您可以在创建关系时设置关系的属性:
rel1 = createRel(player1, "defeats", player4, game="Starcraft", judge="Player 2")
为了回答这个问题,我会这样做:
getDataForPlayer = function(name) {
query = "
MATCH (winner:user)-[game:defeats]->(loser:user)
WHERE winner.ID = {name} OR loser.ID = {name}
RETURN winner.ID AS `Winning Player`,
loser.ID AS `Losing Player`,
game.game AS Game,
game.judge AS Judge
"
return(cypher(graph, query, name=name))
}
getDataForPlayer("Player 1")
输出:
Winning Player Losing Player Game Judge
1 Player 4 Player 1 League of Legends Player 2
2 Player 2 Player 1 League of Legends Player 4
3 Player 3 Player 1 Starcraft Player 4
4 Player 1 Player 2 League of Legends Player 3
5 Player 1 Player 4 Starcraft Player 2
6 Player 1 Player 4 League of Legends Player 3
7 Player 1 Player 4 Starcraft Player 3
看着你的图表,我觉得结构不正确。尽管每个场景可能不同,但考虑添加更多数据时会发生什么总是好的。你的模型可以处理吗?
比如你用关系来表示比赛的结果,那当然需要属性来存储裁判和比赛。游戏名称对我来说实际上看起来像锦标赛游戏,但你会知道哪个更好用。在存储球员和锦标赛名称时,您最终会出现很多重复,因为相同的名称和球员随处可见。
如果您继续在玩家之间添加结果,您最终会得到很多关系,并且错误和重复的可能性会不断增加。
那你可以做些什么来改进你的模型呢?将您的基本关系视为起点,但现在它已经超出了最初的要求:您可以为锦标赛引入节点,为游戏引入节点;保持关系以存储玩家在游戏中的角色等。总是有不止一种方法可以做到这一点 (TIMTOWTDI)。
一图抵千字,看这里的改进模型:
您会看到如何更轻松地向模型中的相应节点或关系添加其他属性。
为了产生您想要的 table 结果,您可以使用:
MATCH
(g:Game)-[:WINNER]->(w:Player),
(g)-[:LOSER]->(l:Player),
(g)-[:JUDGE]->(j:Player),
(g)<-[:HAS_GAMES]-(t:Tournament)
WHERE
w.name = 'Player 1' OR l.name = 'Player 1'
RETURN
w.name AS 'Winning Player',
l.name AS 'Losing Player',
t.name AS 'Game',
j.name AS 'Judge'
并按照 Nicole 的建议适应 R。如果你假装添加很多数据,我认为这种结构会更好地适应你的需求,你也可以探索查询相同数据的不同方式,因为你现在可以从锦标赛开始或直接探索游戏。
我刚开始使用 R、neo4j 和 R-neo4j 编程,所以如果我的问题很简单,请谅解。
我使用 R-neo4j 和以下 R 项目代码 [2] 创建了以下数据库(请附上照片)[1]。
该数据库包含四名玩家之间的电脑游戏比赛结果。数据集由四个节点组成,玩家 1 到玩家 4。节点通过关系 "defeats" 连接,表示比赛的结果。每个关系都有两个标签条目,包含以下数据:judge、game。
从使用Cypher查询的图数据库中,我想提取如下形式的数据(请提供[1]中的图片):
Winning player Loosing player Game Judge
player 1 player 4 Starcraft player 2
player 1 player 4 LOL player 3
player 4 player 1 LOL player 2
player 1 player 4 Starcraft player 3
player 1 player 2 LOL player 3
player 2 player 1 LOL player 4
player 4 player 1 Starcraft player 4
我想查询(R-neo4j环境下首选)图数据库,输入"player 1"返回上面的table
我希望我的问题很清楚并且有人可以帮助我。
祝你有美好的一天。
基督教徒
[2] R (Rneo4j) 代码:
clear(graph)
Y
player1 = createNode(graph,"user",ID="Player 1",male=T)
player2 = createNode(graph,"user",ID="Player 2",male=T)
player3 = createNode(graph,"user",ID="Player 3",male=F)
player4 = createNode(graph,"user",ID="Player 4",male=F)
addConstraint(graph,"user","ID")
rel1 = createRel(player1,"defeats",player4)
rel2 = createRel(player1,"defeats",player4)
rel3 = createRel(player4,"defeats",player1)
rel4 = createRel(player1,"defeats",player4)
rel5 = createRel(player1,"defeats",player2)
rel6 = createRel(player2,"defeats",player1)
rel7 = createRel(player3,"defeats",player1)
rel1 = updateProp(rel1, game = "Starcraft", judge = "Player 2")
rel2 = updateProp(rel2, game = "League of Legends", judge = "Player 3")
rel3 = updateProp(rel3, game = "League of Legends", judge = "Player 2")
rel4 = updateProp(rel4, game = "Starcraft", judge = "Player 3")
rel5 = updateProp(rel5, game = "League of Legends", judge = "Player 3")
rel6 = updateProp(rel6, game = "League of Legends", judge = "Player 4")
rel7 = updateProp(rel7, game = "Starcraft", judge = "Player 4")
几件事。如果您想使用 clear(graph)
而不必输入 "Y",您可以使用 clear(graph, input=F)
。此外,如果您不知道,您可以在创建关系时设置关系的属性:
rel1 = createRel(player1, "defeats", player4, game="Starcraft", judge="Player 2")
为了回答这个问题,我会这样做:
getDataForPlayer = function(name) {
query = "
MATCH (winner:user)-[game:defeats]->(loser:user)
WHERE winner.ID = {name} OR loser.ID = {name}
RETURN winner.ID AS `Winning Player`,
loser.ID AS `Losing Player`,
game.game AS Game,
game.judge AS Judge
"
return(cypher(graph, query, name=name))
}
getDataForPlayer("Player 1")
输出:
Winning Player Losing Player Game Judge
1 Player 4 Player 1 League of Legends Player 2
2 Player 2 Player 1 League of Legends Player 4
3 Player 3 Player 1 Starcraft Player 4
4 Player 1 Player 2 League of Legends Player 3
5 Player 1 Player 4 Starcraft Player 2
6 Player 1 Player 4 League of Legends Player 3
7 Player 1 Player 4 Starcraft Player 3
看着你的图表,我觉得结构不正确。尽管每个场景可能不同,但考虑添加更多数据时会发生什么总是好的。你的模型可以处理吗?
比如你用关系来表示比赛的结果,那当然需要属性来存储裁判和比赛。游戏名称对我来说实际上看起来像锦标赛游戏,但你会知道哪个更好用。在存储球员和锦标赛名称时,您最终会出现很多重复,因为相同的名称和球员随处可见。
如果您继续在玩家之间添加结果,您最终会得到很多关系,并且错误和重复的可能性会不断增加。
那你可以做些什么来改进你的模型呢?将您的基本关系视为起点,但现在它已经超出了最初的要求:您可以为锦标赛引入节点,为游戏引入节点;保持关系以存储玩家在游戏中的角色等。总是有不止一种方法可以做到这一点 (TIMTOWTDI)。
一图抵千字,看这里的改进模型:
为了产生您想要的 table 结果,您可以使用:
MATCH
(g:Game)-[:WINNER]->(w:Player),
(g)-[:LOSER]->(l:Player),
(g)-[:JUDGE]->(j:Player),
(g)<-[:HAS_GAMES]-(t:Tournament)
WHERE
w.name = 'Player 1' OR l.name = 'Player 1'
RETURN
w.name AS 'Winning Player',
l.name AS 'Losing Player',
t.name AS 'Game',
j.name AS 'Judge'
并按照 Nicole 的建议适应 R。如果你假装添加很多数据,我认为这种结构会更好地适应你的需求,你也可以探索查询相同数据的不同方式,因为你现在可以从锦标赛开始或直接探索游戏。