【问题标题】:OrientDB create edge between two nodes with the same day of yearOrientDB 在同一天的两个节点之间创建边缘
【发布时间】:2016-09-14 22:21:07
【问题描述】:

如果它们共享一年中的同一天并且 v.year = u.year+1,我有兴趣在图中同一类的两个节点之间创建一条边 (u,v)。

假设我有 vertices.csv:

id,date
A,2014-01-02
B,2015-01-02
C,2016-01-02
D,2013-06-01
E,2014-06-01
F,2016-06-01

我希望看到的边缘结构是这样的:

A --> B --> C
D --> E
F

让我们将顶点类设置为“myVertex”,边类设置为“myEdge”?是否可以使用 SQL 接口生成这些边?

基于this question,我开始尝试这样的事情:

BEGIN
LET source = SELECT FROM myVertex
LET target = SELECT from myVertex
LET edge   = CREATE EDGE myEdge
             FROM $source
             TO (SELECT FROM $target WHERE $source.date.format('MM-dd') = $target.date.format('MM-dd')
                 AND $source.date.format('yyyy').asInteger() = $target.date.format('yyyy').asInteger()-1)
COMMIT

很遗憾,这是不正确的。所以我没有那么雄心勃勃,想看看我是否可以仅根据匹配的日期来创建边缘:

BEGIN
LET source = SELECT FROM myVertex
LET target = SELECT from myVertex
LET edge   = CREATE EDGE myEdge FROM $source TO (SELECT FROM $target WHERE $source.date.format('MM-dd') = $target.date.format('MM-dd'))
COMMIT

仍然有错误...我敢肯定,对于有经验的 OrientDB 用户来说,这很简单。

我曾考虑将一个 JavaScript 函数组合在一起,例如 Michela suggested on this question,但我现在更愿意尽可能多地使用 SQL 命令。

非常感谢您的帮助。


其他堆栈溢出参考

【问题讨论】:

  • 使用 JS 服务端功能会更容易。如果您需要这方面的帮助,请告诉我。
  • @oleksandr-gubchenko 我正在为此寻找服务器端函数...是否有一些好的文档链接,其中包含创建/使用函数的示例?自从我做了很多 Javascript 编码以来已经有几年了......如果我使用V[i].getRecord().field('date') 在我的脚本中提取一个带有日期的记录,那只会返回一个javascript date object 吗?现在我可以在 Studio 中运行,但接下来我可能想学习如何从某个控制台界面在数据库中添加和使用它们。
  • Javascript API 官方文档:orientdb.com/docs/last/Javascript-Driver.html
  • 谢谢...我仍然遇到一些麻烦。在getRecord().field('date') 命令中返回的日期字段似乎不是javascript date,因为如果我尝试对其执行getDate() 之类的方法,则会出现错误。我如何才能找到我正在处理的数据类型?谢谢!

标签: orientdb


【解决方案1】:

我尝试过使用 OSQL 批处理,但我认为您无法获得想要的结果。

使用 whis OSQL 批处理

begin
let a = select @rid, $a1 as toAdd from test let $a1 = (select from test where date.format("MM") == $parent.$current.date.format("MM") and date.format("dd") == $parent.$current.date.format("dd") and @rid<>$parent.$current.@rid and date.format("yyyy") == sum($parent.$current.date.format("yyyy").asInteger(),1))
commit
return $a

我收到了

但问题是当你创建边缘时,你不能在上一步获得的表上循环。 我认为最好的解决方案是使用 JS 服务端函数。 希望对您有所帮助。

【讨论】:

  • 我可以做一个服务器端功能,但我可能无法在所有环境中运行服务器。我正在编写 java 代码,通过查询来获取边缘对的表,然后我将分别执行 create-edge。这个例子很有帮助,因为我之前没有尝试过 sum() 函数和 asInteger() 方法。我添加了一个不那么雄心勃勃的问题,专注于date manipulations in OSQL。这对此有很大帮助。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 2018-04-30
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多