原由参考我以前建立图数据表的操做,要给“刘备”和“关羽”添加【兄弟】关系,要分别输入刘备有个兄弟叫关羽,关羽有个兄弟叫刘备,神烦!java
在网上搜查了半天愣是没找到怎么给jg图数据库的2个顶点添加一个双向的关系,无奈翻了下官网和源码,整理分享以下:数据库
JanusGraph有双向边吗?post
先说答案:"没有"。学习
须要双向边怎么办?测试
"若是须要双向边,则经过添加两条相反方向的单向边实现。"this
建议使用时封装一个方法便可。spa
看源码的org.janusgraph.graphdb.relations.EdgeDirection:code
public class EdgeDirection {
public static final Direction[] PROPER_DIRS = {Direction.IN, Direction.OUT};
……
}
复制代码
白纸黑字仅有IN和OUT两个方向。接口
JanusGraph不存在无向边。get
通常来讲,在图数据库中这两个概念的表达意图是同样的,也就是说“双向”和“无向”都是表示【关系】对两个顶点均适用。如“同窗”关系,“同事”关系,“兄弟”关系,“掘友”关系等。你是我同窗,我也是你同窗。
但我爱你,你就不必定爱我,“爱”有方向:I --[love]--> you。
可是源码org.janusgraph.core.schema.EdgeLabelMaker接口中有两个方法:
/** * Configures the label to be directed. * By default, the label is directed. * @return this EdgeLabelMaker * @see org.janusgraph.core.EdgeLabel#isDirected() */
EdgeLabelMaker directed();
/** * Configures the label to be unidirected. * By default, the type is directed. * @return this EdgeLabelMaker * @see org.janusgraph.core.EdgeLabel#isUnidirected() */
EdgeLabelMaker unidirected();
复制代码
意思是说建立边标签时默认是“directed”(有指向的),但这个“unidirected”是无指向的意思吗?
官网的解释是“单向边”(Unidirected Edges),注意这个和“入边”,“出边”的单个方向概念不同。单向边占用更低的存储空间,但遍历受限。出顶点能够沿边遍历,但入顶点不知道它的存在。就像万维网中的【超连接】同样。
【请注意,删除其入顶点( in-vertex)时,不会自动删除未定向的边(Unidirected Edges)。参阅 Ghost Vertices】
添加【刘备】--out--【兄弟】--in--> 【关羽】
g.addE("brother").from(liubei).to(guanyu)
和addEdge方法(2)liubei.addEdge("brother",guanyu)
添加关系能正确查出结果打√
类别 | directed | unidirected |
---|---|---|
out (1) | √ | √ |
in (1) | √ | × |
out (2) | √ | √ |
in (2) | √ | × |
正向的out出边查询均正常!
使用unidirected建立边标签后,关系不能入边(in)反向查询!!
按directed(默认)建立边标签后,关系能够入边反向查询
刘备总有兄弟叫关羽,关羽却不必定有个兄弟叫刘备~
归档:【JanusGraph学习笔记】