Cause reference to my previous operation to create the chart data table, to add the “brother” relationship to “Liu Bei” and “Guan Yu”, to input respectively Liu Bei has a brother named Guan Yu, Guan Yu has a brother named Liu Bei, god annoying!

The problem

How to add a bidirectional relationship to the 2 vertices of jG graph database?

Does JanusGraph have bidirectional edges?

First answer :” No “.

What if I need a two-way edge?

“If bidirectional edges are needed, this is done by adding two one-way edges in opposite directions.”

It is recommended to encapsulate a method.

The direction of the edge

Look at the source code of org. Janusgraph. Graphdb. Relations. EdgeDirection:

public class EdgeDirection {
    public static finalDirection[] PROPER_DIRS = {Direction.IN, Direction.OUT}; ... }Copy the code

There are only two directions IN and OUT IN black and white.

JanusGraph does not have undirected edges.

Undirected edges and bidirectional edges

In general, the intent of the two concepts is the same in a graph database, that is, “bidirectional” and “undirected” both mean that the relationship applies to both vertices. Such as “classmate” relationship, “colleague” relationship, “brother” relationship, “digging friend” relationship and so on. You are my classmate and I am yours, too.

But if I love you, you may not love me.

Directed and unidirected

But the source org. Janusgraph. Core. Schema. EdgeLabelMaker interface has two methods:

  • directed()
  • unidirected()
/**
     * Configures the label to be directed.
     * By default, the label is directed.
     * @return this EdgeLabelMaker
     * @see org.janusgraph.core.EdgeLabel#isDirected()
     */
    EdgeLabelMaker directed(a);

    /**
     * Configures the label to be unidirected.
     * By default, the type is directed.
     * @return this EdgeLabelMaker
     * @see org.janusgraph.core.EdgeLabel#isUnidirected()
     */
    EdgeLabelMaker unidirected(a);
Copy the code

The default for creating edge tags is “directed”, but does “unidirected” mean undirected?

As explained on the website, “Unidirected Edges”, note that this is not the same as the “in” and “out” single orientation concepts. One-way edges take up less storage space, but are limited in traversal. The outgoing vertex can be traversed along the edge, but the incoming vertex is unaware of its existence. It’s like hyperlinks in the World Wide Web.

Note that removing in-vertex Edges does not automatically delete the Unidirected Edges.

The measured

Test scenarios

Add [liu Bei] –out– in–> [Guan Yu]

The test method

  • Side labels are created with directed and Unidirected
  • Then use addE method (1) respectivelyg.addE("brother").from(liubei).to(guanyu)And addEdge method (2)liubei.addEdge("brother",guanyu)Add the relationship
  • Test out/in query statement: Out is Zha Liu Bei’s brother, in is Zha Guan Yu’s brother

The test results

Check √ if you can find out the result correctly

category directed unidirected
out (1) Square root Square root
in (1) Square root x
out (2) Square root Square root
in (2) Square root x

conclusion

Positive out outgoing queries are normal!

Relationships cannot be queried backwards into edges after creating edge tags with Unidirected!!

After the edge label is created by pressing Directed (the default), the relationship can be queried backwards into the edge

Liu Bei always had a brother named Guan Yu, but Guan Yu did not necessarily have a brother named Liu Bei


Archived: [JanusGraph Study Notes]

Related column

How do I create a JanusGraph and write data to it?

JanusGraph first lesson – Creating an IDEA project

JanusGraph Learning Notes – Problems and Solutions (PART 1)