Search code examples
databasenosqlorientdb

OrientDB - Trouble storing data from a java application


I am trying to store triplets inside of OrientDB as Vertex-Edge-Vertex relationships inside of a Java application that I am working on. My understanding of using OrientDB is that I can use the Tinkerpop API and instantiate a graph like this:

OrientGraph graph = new OrientGraph("local:/tmp/orient/test_db");

That is really all I do to instantiate the graph, then I try to connect vertices with edges in a loop like this: (Note that a Statement is a triplet consisting of subject-relationship-object.)

for (Statement s : statements) {

     Vertex a = graph.addVertex(null);
     Vertex b = graph.addVertex(null);
     a.setProperty("Subject", s.getSubject().toBELShortForm());
     RelationshipType r = s.getRelationshipType();
     if (s.getObject() != null) {
         b.setProperty("Object", s.getObject().toBELShortForm());
         Edge e = graph.addEdge(null, a, b, r.toString());
     }
     else {
         b.setProperty("Object", "null");
         Edge e = graph.addEdge(null, a, b, "no-relationship");
     }

}

I then loop through the vertices of the graph and print them out like this:

for (Vertex v : graph.getVertices()) {
     out.println("Vertex: " +v.toString());
}

It does print a lot of vertices, but when I log into the server via the command line, using server.sh, all I see are the 3 records for ORole and 4 records for OUser. What am I missing here? Because it seems like although my java program runs and completes, the data is not being put into the database.


Solution

  • The answer, at least for now, seems to be not to use the Tinkerpop API but rather the Orient API directly. This is the same thing I was doing with Tinkerpop, but using the OrientDB API. This actually does store my data into the database:

    for (Statement s : statements) {
                ODocument sNode = db.createVertex();
                sNode.field("Subject", s.getSubject().toBELShortForm());
                sNode.save();
    
                ODocument oNode = db.createVertex();
                if (s.getObject() != null) {
                    oNode.field("Object", s.getObject().toBELShortForm());
                    oNode.save();
                }
                else {
                    oNode.field("Object", "null");
                    oNode.save();
                }
    
                RelationshipType r = s.getRelationshipType();
                ODocument edge = db.createEdge(sNode, oNode);
                if (r != null) {
                    edge.field(r.toString());
                    edge.save();
                }
                else {
                    edge.field("no relationship");
                    edge.save();
                }
    }