0
votes

In Orientdb 3.0 RC1 (Tinkerpop/Gremlin community edition) I have 2 vertex classes:
- 'Company' with 1 property 'address'
- 'Address' with 1 property 'address' having a UNIQUE_HASH_INDEX on this property

Need to create edges of class 'Location' between 'Company' vertexes to the corresponding 'Address' vertex based on having the same 'address' property value.

First I tried using Gremlin the following way:

g.V().hasLabel("Company").as("a").
V().hasLabel("Address").as("b").
where("a", eq("b")).by("address").
addE("Location").next()

But the mid-traversal is not hitting the index .....I guess the OrientDB-Gremlin implementation not complete yet or my above query not good.

Then I converted the above to use a sideEffect():

g.V().hasLabel("Company").sideEffect{g.V().hasLabel("Address").has("address",it.get().property('address').value()).addE('Location').from(it.get()).next()}

but after quickly adding around 1k edges the query abruptly aborts and the OrientDB logs a lot of warnings like this:

"WARNI {db=ter1050} This database instance has 1280 open command/query result sets, please make sure you close them with OResultSet.close()"

So once again .....my query has a problem or I hit a bug.

I didn't find a way to do it in OrientDB SQL also.

I know this can be done using Tinkerpop API in Java but I was hoping for something more simple.

1

1 Answers

0
votes

Try this:

OrientDB orientDB = new OrientDB("remote:localhost/", "<username>", "<password>", OrientDBConfig.defaultConfig());
        ODatabaseDocument db = orientDB.open("<db name>","<username>", "<password>");

        OResultSet result = db.command("select from Company");
        OResultSet address_class = db.command("select from Address");

        List<OResult> company = new ArrayList<OResult>();
        List<OResult> address = new ArrayList<OResult>();

        while(result.hasNext())
        {
            OResult record = result.next();
            company.add(record);
        }
        while(address_class.hasNext())
        {
            OResult record = address_class.next();
            address.add(record);
        }

        for(int i = 0; i < company.size(); i++)
        {
            String company_address = company.get(i).getProperty("address");
            for(int j = 0; j < address.size(); j++)
            {
                String addresses = address.get(j).getProperty("address");
                if(company_address.equals(addresses))
                {
                    ORecordId company_rid = company.get(i).getProperty("@rid");
                    ORecordId addresses_rid = address.get(j).getProperty("@rid");
                    db.command("create edge Location from " + company_rid + " to " + addresses_rid);
                }
            }
        }

        db.close();
        orientDB.close()

this is the result:

enter image description here

Hope it helps

Regards