简体   繁体   English

如何在节点之间创建随机关系?

[英]How to create random relationships between nodes?

I'm trying to create random transaction between bank accounts.我正在尝试在银行账户之间创建随机交易。 I have created the following query:我创建了以下查询:

//Create transactions
CALL apoc.periodic.iterate("
match (a:BANK_ACCOUNT)
WITH apoc.coll.randomItem(collect(a)) as sender
return sender", "
MATCH (b:BANK_ACCOUNT)
WHERE NOT sender = b
WITH apoc.coll.randomItem(collect(b)) as receiver
MERGE (sender)-[r:HAS_TRANSFERED {time: datetime()}]->(receiver)
set r.amount = rand()*1000",
{batchSize:100, parallel:false});

I would assume that it would create 100 random transactions between random bank accounts.我假设它会在随机银行账户之间创建 100 次随机交易。 Instead it creates 1 new bank account and 1 new relationship.相反,它会创建 1 个新银行账户和 1 个新关系。 What am I doing wrong and what should I do?我做错了什么,我该怎么办?

Thanks for your help !谢谢你的帮助 !

The following query uses apoc.coll.randomItems to get 200 different random accounts at one time (which is much faster than getting one random account 200 times):以下查询使用apoc.coll.randomItems 一次获取 200 个不同的随机帐户(这比获取一个随机帐户 200 次要快得多):

MATCH (ba:BankAccount)
WITH apoc.coll.randomItems(COLLECT(ba), 200) AS accts
WHERE SIZE(accts) > 1 
UNWIND RANGE(0, SIZE(accts)/2*2-1, 2) AS i
WITH accts[i] AS sender, accts[i+1] AS receiver
CREATE (sender)-[:TRANSFERED_TO {time: datetime()}]->(receiver)

Notes:笔记:

  1. This query uses CREATE instead of MERGE because it is unlikely that a TRANSFERED_TO relationship already exists with the current time as the time value.此查询使用CREATE而不是MERGE因为不太可能已经存在以当前时间作为time值的TRANSFERED_TO关系。 (You can choose to use MERGE anyway, if duplication is still possible.) (如果仍然可以复制,您可以选择使用MERGE 。)
  2. The WHERE SIZE(accts) > 1 test avoids errors when there are not at least 2 accounts. WHERE SIZE(accts) > 1测试可避免在帐户数量不足 2 时出现错误。
  3. SIZE(accts)/2*2-1 calculation prevents the RANGE function from generating a list index ( i ) that exceeds the last valid index for a sender account. SIZE(accts)/2*2-1计算可防止RANGE函数生成超过sender帐户的最后一个有效索引的列表索引 ( i )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM