[JAVA] How to create custom edge types?

240 views
Skip to first unread message

Marc Tigges

unread,
Apr 22, 2014, 9:07:37 AM4/22/14
to orient-...@googlegroups.com
Hello friends,

since a few days i have problems to create custom edge types.
here a small example:

OrientVertexType vertexType = graph.createVertexType("person", null);
vertexType.createProperty("name", OType.STRING);

Vertex outVertex = graph.addVertex("class:person", null);
outVertex.setProperty("name", "Roberta");

Vertex inVertex = graph.addVertex("class:person", null);
inVertex
.setProperty("name", "Peter");

OrientEdgeType edgeType = graph.createEdgeType("relationship", null);
edgeType
.createProperty("since", OType.DATETIME);

Edge edge = graph.addEdge("class:relationship", outVertex, inVertex, "married");
edge
.setProperty("since", aDate);

Creating the vertex type and vertices of that type works fine!
But when it comes to the edge type and edges of that type i get problems.

Somehow i cant instantiate the class "class:relationship". At the moment
when i set the property of the edge, i get this output:

WARNING: Committing the active transaction to create the new type 'married' as subclass of 'E'.
The transaction will be reopen right after that. To avoid this behavior create the classes outside the transaction.

Setting the option for using lightweight edges to false does not solve this problem.

So how can i create an edge type and instantiate it?

Artem Orobets

unread,
Apr 23, 2014, 1:58:28 AM4/23/14
to orient-...@googlegroups.com
Hi Marc,

The issue there is that createEdgeType() changes database schema. And OrientDB does not support transactional schema changes yet. That's why graph should commit active transaction before creation a schema.

To avoid the warning you need to create all types before inserting data.

Try following:

OrientVertexType
 vertexType = graph.createVertexType("person", null);
vertexType.createProperty("name", OType.STRING);

OrientEdgeType edgeType = graph.createEdgeType("relationship", null);
edgeType
.createProperty("since", OType.DATETIME);

Vertex outVertex = graph.addVertex("class:person", null);
outVertex.setProperty("name", "Roberta");

Vertex inVertex = graph.addVertex("class:person", null);
inVertex
.setProperty("name", "Peter");

Edge edge = graph.addEdge("class:relationship", outVertex, inVertex, "married");
edge
.setProperty("since", aDate);



Best regards,
Artem Orobets

Orient Technologies

the Company behind OrientDB



--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marc Tigges

unread,
Apr 23, 2014, 2:59:20 AM4/23/14
to orient-...@googlegroups.com
Hi Artem

That's exactly what i did! The code you posted is exactly the same code i posted above!
The only problem i have is the following:

I created all edge types (for example the edge type "relationship") outside of a transaction
as it should be. But when i now create new edges as instances of the created edge types,
this code is creating new edge types instead.

Edge edge = graph.addEdge("class:relationship", outVertex, inVertex, "married");
edge
.setProperty("since", aDate);

This part of code is the problem. The program does simply not care about the fact that 
i want the "married" edge to be of the type "class:relationship". The program creates
a new edge type "married" instead. And of course i create my edges inside of a 
transaction (much faster). So because he creates this new edge type "married" the
program has to close the transaction and to reopen it after he created the edge type
"married".

I want to know, why the program is ignoring my "class:relationship" statement and
why the program is creating the new edge type "married" when it is absolutely not
neccessary because "married" should be a "normal" edge of the type "relationship".
And the edge type "relationship" does already have the property "since" so there
is just no reason for the program to create the new edge type "married".
I just dont get it.

Luca Garulli

unread,
Apr 23, 2014, 5:16:50 AM4/23/14
to orient-database
Hi Marc,
This is because the Edge's label is taken as Edge's class. If you want to disable such feature execute this command once:


alter database custom useClassForEdgeLabel=false
But this could slow down edge operations. WDYT about using the edge's label as class? You could create also the Relationship Edge class and then Married as subclass of Relationship ;-)

Lvc@



Marc Tigges

unread,
Apr 23, 2014, 6:12:32 AM4/23/14
to orient-...@googlegroups.com
Setting useClassForEdgeLabel to false unforeunately does not solve the problem. Of course it solves the 
problem of creating new edge types but i still have no edge in my database of the type "relationship".

After i created the edge "married" there must be at least one edge of the type "relationship" in my 
database but it isnt. In OrientDB Studio in the Schema browser i see my edge type "relationship"
but at the column "records" it shows that there are 0 records. So my edge "married" still does not
seem to be of the type "relationship".

I have a very special schema definition and i need that schema because of freebase. I import
the freebase data dump into orientdb and there are some things i cant do because of too much 
overhead. So i have to do some workarounds instead.

For example:

I first have to create all the vertex types.
After that i have to create all the instances of that types (all vertices). In freebase every instance has
an id like "m_xyz". I find the instances very easy because they are stored as triples like "m_xyz type.type.instance people.person".
In freebase there are many properties for example you have the class person with the property name
and they are also stored as triples like that "people.person.name type.object.type type.property". 
The value of the property is again stored as a triple like "m_xyz people.person.name "obama"".

I think here is the point where you can see the problem. To create a simple property i would have
to brows three times through a 250GB big file. Thats way to much overhead. 
The same problem i get when i try to find all property definitions of one freebase type like 
"people.person".

So i decided to make a workaround like that:

I created a Vertex for each datatype existing in freebase. After i created all types and instances
i can ignore entries like "people.person.name type.object.type type.property" and can just look 
at "m_xyz people.person.name "obama"". I find out the datatype of "obama" and than just create
an edge with the label "people.person.name" from "m_xyz" to the vertex which represents the 
datatype "string" and set the property "value" of the edge "people.person.name" to "obama".

Lets get back to the small example i posted in my first post.

When i create the edge type "relationship" and create the edge "married" of the type "relationship"
all i want is to have the following entry in my orientdb schema browser:

name               superClass    records
relationship       E                  1

And when i browse my edges of the type relationship i want to have all my cool little edges
like "married" with the properties i set.

Thats my only dream for today to get that working.

Luca Garulli

unread,
Apr 23, 2014, 6:22:36 AM4/23/14
to orient-database
Hi Marc,
so if you've created married as subtype of relationship, that is a subtype of E you should just do:

Edge edge = graph.addEdge("class:married", outVertex, inVertex, "married");

With OrientDB default settings.

Lvc@


Marc Tigges

unread,
Apr 23, 2014, 6:33:07 AM4/23/14
to orient-...@googlegroups.com
No, i want to have many edges of the type relationship as subtype of E with different labels.

- one edge of the type "relationship" with the label "married"
- one edge of the type "relationship" with the label "engaged"
- one edge of the type "relationship" with the label "friends"
- ....

I will select edges! I know it is slow but i have to do it.

Iterable<Edge> edges = graph.getEdges();

for(Edge edge : edges){
   
if(edge.getLabel().equals("married"){
       
// do some stuff
   
} else if(edge.getLabel().equals("engaged"){
       
// do some stuff
   
}

   
//....
}

Luca Garulli

unread,
Apr 23, 2014, 6:40:24 AM4/23/14
to orient-database
Marc,
everything is polymorphic, so if you do:

- "married" as sub-type of "relationship"
- "engaged" as sub-type of "relationship"
- "friends" as sub-type of "relationship"

And if you execute a query like:

select from relationship where ...

It will return instances of Married, Engaged and Friends. Also if you traverse by label it's polymorphic. Example:

select out('relationship') from V

Will traverse any outgoing edge of type "relationship" or any subclass. To have only "married":

select out('married') from V

And if you want select the edge's label by using the @class you can do pretty the same:

Iterable<Edge> edges = graph.getEdges();

for(Edge edge : edges){
    
if(edge.getLabel().equals("married"){
       
// do some stuff
    
} else if(edge.getLabel().equals("engaged"){
       
// do some stuff
    
}

    
//....
}

Lvc@

Marc Tigges

unread,
Apr 23, 2014, 8:23:52 AM4/23/14
to orient-...@googlegroups.com
Right... now i got what you meant.
Thank you
Reply all
Reply to author
Forward
0 new messages