Thursday, 2 September 2021

ArangoDB: Working with edge documents

Edge document are like normal document with additional two attributes _from, _to.

 

Edge collection has ‘edges’ method which takes a vertex (or) vertices as input and return all edges starting from (outbound) or ending in (inbound) vertex (or) vertices.

 

Signature

edge-collection.edges(vertex)

edge-collection.edges(vertices)

 

Let’s experiment it with by building simple graph.


Step 1: Create two collections, one is of type document/vertex and other is of type edge.

 

db._createDocumentCollection("myVertexes")

db._createEdgeCollection("myEdges")

127.0.0.1:8529@abc_org> db._createDocumentCollection("myVertexes")
[ArangoCollection 36584, "myVertexes" (type document, status loaded)]

127.0.0.1:8529@abc_org> db._createEdgeCollection("myEdges")
[ArangoCollection 36601, "myEdges" (type edge, status loaded)]

 

Step 2: Create 5 vertex documents v1, v2, v3, v4 and v5.

var myGraph = {};
myGraph.v1 = db.myVertexes.save({ name : "vertex 1" });
myGraph.v2 = db.myVertexes.save({ name : "vertex 2" });
myGraph.v3 = db.myVertexes.save({ name : "vertex 3" });
myGraph.v4 = db.myVertexes.save({ name : "vertex 4" });
myGraph.v5 = db.myVertexes.save({ name : "vertex 5" });

127.0.0.1:8529@abc_org> var myGraph = {};

127.0.0.1:8529@abc_org> myGraph.v1 = db.myVertexes.save({ name : "vertex 1" });
{ 
  "_id" : "myVertexes/36641", 
  "_key" : "36641", 
  "_rev" : "_cS_C0bq---" 
}


 

127.0.0.1:8529@abc_org> myGraph.v2 = db.myVertexes.save({ name : "vertex 2" });
{ 
  "_id" : "myVertexes/36643", 
  "_key" : "36643", 
  "_rev" : "_cS_C0bq--A" 
}

127.0.0.1:8529@abc_org> myGraph.v3 = db.myVertexes.save({ name : "vertex 3" });
{ 
  "_id" : "myVertexes/36645", 
  "_key" : "36645", 
  "_rev" : "_cS_C0bu---" 
}

127.0.0.1:8529@abc_org> myGraph.v4 = db.myVertexes.save({ name : "vertex 4" });
{ 
  "_id" : "myVertexes/36647", 
  "_key" : "36647", 
  "_rev" : "_cS_C0by---" 
}

127.0.0.1:8529@abc_org> myGraph.v5 = db.myVertexes.save({ name : "vertex 5" });
{ 
  "_id" : "myVertexes/36649", 
  "_key" : "36649", 
  "_rev" : "_cS_C02u---" 
}

 

Step 3: Create edges between vertexes.

myGraph.e1 = db.myEdges.save(myGraph.v1, myGraph.v2,{ label : "linkTo"});
myGraph.e2 = db.myEdges.save(myGraph.v1, myGraph.v3,{ label : "linkTo"});
myGraph.e3 = db.myEdges.save(myGraph.v2, myGraph.v4,{ label : "linkTo"});
myGraph.e4 = db.myEdges.save(myGraph.v3, myGraph.v5,{ label : "linkTo"});
myGraph.e5 = db.myEdges.save(myGraph.v4, myGraph.v3,{ label : "linkTo"});
myGraph.e6 = db.myEdges.save(myGraph.v4, myGraph.v5,{ label : "linkTo"});

 

Get all the inbound and outbound edges of vertex v3

127.0.0.1:8529@abc_org> db.myEdges.edges(myGraph.v3)
[ 
  { 
    "_key" : "36717", 
    "_id" : "myEdges/36717", 
    "_from" : "myVertexes/36645", 
    "_to" : "myVertexes/36649", 
    "_rev" : "_cS_EWBq---", 
    "label" : "linkTo" 
  }, 
  { 
    "_key" : "36713", 
    "_id" : "myEdges/36713", 
    "_from" : "myVertexes/36641", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBi---", 
    "label" : "linkTo" 
  }, 
  { 
    "_key" : "36719", 
    "_id" : "myEdges/36719", 
    "_from" : "myVertexes/36647", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBu---", 
    "label" : "linkTo" 
  } 
]

 

Get all outbound edges of vertex v3

127.0.0.1:8529@abc_org> db.myEdges.outEdges(myGraph.v3)
[ 
  { 
    "_key" : "36717", 
    "_id" : "myEdges/36717", 
    "_from" : "myVertexes/36645", 
    "_to" : "myVertexes/36649", 
    "_rev" : "_cS_EWBq---", 
    "label" : "linkTo" 
  } 
]

 

Get all inbound edges of vertex v3

127.0.0.1:8529@abc_org> db.myEdges.inEdges(myGraph.v3)
[ 
  { 
    "_key" : "36713", 
    "_id" : "myEdges/36713", 
    "_from" : "myVertexes/36641", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBi---", 
    "label" : "linkTo" 
  }, 
  { 
    "_key" : "36719", 
    "_id" : "myEdges/36719", 
    "_from" : "myVertexes/36647", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBu---", 
    "label" : "linkTo" 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

ArangoDB: Get document id from key

'collection.documentId(documentKey)' method return the document id associated with this document key.

127.0.0.1:8529@abc_org> db.user.documentId("1234")
user/1234

 

 

Previous                                                    Next                                                    Home

Wednesday, 1 September 2021

ArangoDB: Get the type of collection

‘collection.type()’ method return an integer which specifies the type of collection.

 

Integer 2 specifies document collection and 3 specifies edge collection.

 

Let’s create document and edge collections.

127.0.0.1:8529@abc_org> db._createDocumentCollection("myDocCollection")
[ArangoCollection 34937, "myDocCollection" (type document, status loaded)]

127.0.0.1:8529@abc_org> db._createEdgeCollection("myEdgeCollection")
[ArangoCollection 34954, "myEdgeCollection" (type edge, status loaded)]

 

Let’s get the type of these collections.

127.0.0.1:8529@abc_org> db.myDocCollection.type()
2

127.0.0.1:8529@abc_org> db.myEdgeCollection.type()
3

 

 

 

Previous                                                    Next                                                    Home

Neo4j: ORDER BY Clause

ORDER BY clause is used to sort the data in ascending or descending order.

 

For example, below statement return the person details by their salary in ascending order.

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary

 

Let’s see the same with an example.

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 


Step 2: Insert data by executing below query.

CREATE (p1:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true, designation: "CEO", salary:540000000}),
(p2:Person{name: "Murali", age: 32, city: "Hyderabad", male: true, designation: "Vice President", salary: 43000000}),
(p3:Person{name: "Gopi", age: 34, city: "Chennai", male: true, designation: "Vice President", salary: 63067000}),
(p4:Person{name: "Kiran", age: 28, city: "Bangalore", male: true, designation: "Director", salary: 12000000}),
(p5:Person{name: "Siva", age: 45, city: "Hyderabad", male: true, designation: "Director", salary: 23000000}),
(p6:Person{name: "Kiran", age: 36, city: "Hyderabad", male: true, designation: "Manager", salary: 7800000}),
(p7:Person{name: "Ritweek", age: 23, city: "Bangalore", male: true, designation: "Manager", salary: 8100000}),
(p8:Person{name: "Sudheer", age: 24, city: "Chennai", male: true, designation: "Manager", salary: 7900000}),
(p9:Person{name: "Naveen", age: 37, city: "Bangalore", male: true, designation: "Employee", salary: 4500000}),
(p10:Person{name: "Sailaja", age: 31, city: "Hyderabad", female: true, designation: "Employee", salary: 5300000}),
(p11:Person{name: "Harika", age: 36, city: "Bangalore", female: true, designation: "Employee", salary: 3400000}),
(p12:Person{name: "Brahmam", age: 41, city: "Bangalore", male: true, designation: "Employee", salary: 4560000}),
(office1:Location{name: "Pluto", city: "Bangalore"}),
(office2:Location{name: "Mars", city: "Chennai"})
MERGE (p2)-[:REPORT_TO{from: 2014, description: "reports to"}]->(p1)
MERGE (p3)-[:REPORT_TO{from: 2016, description: "reports to"}]->(p1)
MERGE (p4)-[:REPORT_TO{from: 1997, description: "reports to"}]->(p2)
MERGE (p5)-[:REPORT_TO{from: 1997, description: "reports to"}]->(p3)
MERGE (p6)-[:REPORT_TO{from: 2001, description: "reports to"}]->(p4)
MERGE (p7)-[:REPORT_TO{from: 2009, description: "reports to"}]->(p4)
MERGE (p8)-[:REPORT_TO{from: 1998, description: "reports to"}]->(p5)
MERGE (p9)-[:REPORT_TO{from: 2003, description: "reports to"}]->(p6)
MERGE (p10)-[:REPORT_TO{from: 2004, description: "reports to"}]->(p6)
MERGE (p11)-[:REPORT_TO{from: 2008, description: "reports to"}]->(p7)
MERGE (p12)-[:REPORT_TO{from: 2019, description: "reports to"}]->(p8)
MERGE (p2)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p1)
MERGE (p10)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p8)
MERGE (p7)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p9)
MERGE (p3)-[:RELATIVE_TO{from: 2014, description: "relative to"}]->(p4)
RETURN p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, office1, office2





Get the person details by their salary in ascending order.

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary

 

 


Get the person details by their salary in descending order.

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC

 

Get the person details by their salary in descending order.

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC




Previous                                                    Next                                                    Home

Junit5: Test Case: @ExcludePackages

@ExcludePackages specifies the packages to be excluded when running a test suite on the JUnit Platform.

 


 

For example, below snippet runs all the test cases in the package "com.sample.app.dummy" and its subpackages.

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
public class IncludePackagesDemo {

}

 

What if you want to selectively run the packages?

You can use @IncludePackages and @ExcludePackages to explicitly include and exclude the packages.

 

Example

 

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
@ExcludePackages({"com.sample.app.dummy.dummy1"})
public class IncludePackagesDemo {

}

 

Above snippet execute below test classes.

 

a.   DummyTest

b.   Dummy2D1Test

 

Find the below working application.

 

DummyTest.java

package com.sample.app.dummy;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class DummyTest {

    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

Dummy1Test.java

package com.sample.app.dummy.dummy1;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy1Test {
    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

Dummy1D1Test.java

package com.sample.app.dummy.dummy1.d1;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy1D1Test {

    @Test
    public void test1() {
        assertTrue(true);
    }

}

 

Dummy1D2Test.java

package com.sample.app.dummy.dummy1.d2;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy1D2Test {
    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

Dummy2D1Test.java

package com.sample.app.dummy.dummy2.d1;

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Test;

public class Dummy2D1Test {
    @Test
    public void test1() {
        assertTrue(true);
    }
}

 

ExcludePackagesDemo

package com.sample.app.suite;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.ExcludePackages;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy"})
@ExcludePackages({"com.sample.app.dummy.dummy1"})
public class ExcludePackagesDemo {

}

 

Run ‘ExcludePackagesDemo’ class, you will see the executed test methods result in junit report window.

 


Note

By default, @SelectPackages will only include test classes whose names either begin with Test or end with Test or Tests.


 

 

 

 

 

 

 

 

 

 

 


Previous                                                    Next                                                    Home