Tuesday, 3 August 2021

Neo4j: Exercise 1: Create likes relationship

Create three persons Rama, Krishna, Naveen.

Create Likes relationship like below.

a.   Rama likes Krishna

b.   Krishna likes Naveen

c.    Naveen likes Krishna

 

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

http://localhost:7474/browser/

 


Step 2: Create three persons by executing below command.

CREATE (p1:Person{name: "Rama"}), (p2:Person{name: "Krishna"}), (p3:Person{name: "Naveen"}) RETURN p1, p2, p3




Step 3: Create LIKES relationships among persons

MATCH (p1:Person{name: "Rama"}), (p2:Person{name: "Krishna"}), (p3:Person{name: "Naveen"})

MERGE (p1)-[:LIKES]->(p2)

MERGE (p2)-[:LIKES]->(p3)

MERGE (p2)<-[:LIKES]-(p3)

RETURN p1, p2, p3

 


 

Previous                                                    Next                                                    Home

ArangoDB: Convert the cursor to array

toArray() method is used to get the information from cursor in array format.

127.0.0.1:8529@abc_org> usersCursor = userCollection.all()
SimpleQueryAll(user)

127.0.0.1:8529@abc_org> usersCursor.toArray()
[ 
  { 
    "_key" : "12977", 
    "_id" : "user/12977", 
    "_rev" : "_cRdSijO---", 
    "id" : 1, 
    "firstName" : "Ram" 
  }, 
  { 
    "_key" : "13028", 
    "_id" : "user/13028", 
    "_rev" : "_cRdT1TK---", 
    "id" : 514, 
    "firstName" : "Sailu" 
  }, 
  { 
    "_key" : "14390", 
    "_id" : "user/14390", 
    "_rev" : "_cRd12pm---", 
    "id" : 1, 
    "firstName" : "Ram" 
  }, 
  { 
    "_key" : "14434", 
    "_id" : "user/14434", 
    "_rev" : "_cRd5IAm---", 
    "lastName" : "Battu", 
    "firstName" : "Ram" 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

Monday, 2 August 2021

ArangoDB: all(): Get all the documents in a collection

'collection.all()' method fetches all the documents in a collection and return the cursor.

 

How to travers a cursor?

Using next(), hasNext() methods you can access the cursor elements.

 

Example

usersCursor = userCollection.all()

while(usersCursor.hasNext()) print(usersCursor.next())

127.0.0.1:8529@abc_org> usersCursor = userCollection.all()
SimpleQueryAll(user)

127.0.0.1:8529@abc_org> while(usersCursor.hasNext()) print(usersCursor.next())
{ 
  "_key" : "12977", 
  "_id" : "user/12977", 
  "_rev" : "_cRdSijO---", 
  "id" : 1, 
  "firstName" : "Ram" 
}
{ 
  "_key" : "13028", 
  "_id" : "user/13028", 
  "_rev" : "_cRdT1TK---", 
  "id" : 514, 
  "firstName" : "Sailu" 
}
{ 
  "_key" : "14390", 
  "_id" : "user/14390", 
  "_rev" : "_cRd12pm---", 
  "id" : 1, 
  "firstName" : "Ram" 
}
{ 
  "_key" : "14434", 
  "_id" : "user/14434", 
  "_rev" : "_cRd5IAm---", 
  "lastName" : "Battu", 
  "firstName" : "Ram" 
}

127.0.0.1:8529@abc_org>

 

 

 

Previous                                                    Next                                                    Home

Neo4j: Add relationship to existing nodes

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

http://localhost:7474/browser/

 


Step 2: Create nodes by executing below command.

CREATE (r:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true}), (m:Person{name: "Murali Krishna", age: 34, city: "Hyderabad", male: true}) RETURN r, m




Step 3: Add LIKES relationship between Rama Krishna and Murali Krishna by executing below query.

MATCH (p1:Person{name: "Rama Krishna"}), (p2:Person{name: "Murali Krishna"}) CREATE (p1)-[:LIKES]->(p2)

 

 


Step 4: Get the nodes to see the relationship.

MATCH (n:Person) RETURN n




We can even add even specify the same relationship between p2 and p1 by executing below command.

 

MATCH (p1:Person{name: "Rama Krishna"}), (p2:Person{name: "Murali Krishna"}) CREATE (p1)<-[:LIKES]-(p2)

 

 


Get the nodes

MATCH (n:Person) RETURN n

 

 


Problem of creating relationship using CREATE clause

CREATE clause create new relationship even though there is already same relationship exist.

 

For example, execute below command.

MATCH (p1:Person{name: "Rama Krishna"}), (p2:Person{name: "Murali Krishna"}) CREATE (p1)-[:LIKES]->(p2) CREATE (p1)<-[:LIKES]-(p2) RETURN p1, p2




Rerun the same command again.




How to solve above problem?

Use MERGE clause to create relationships. MERGE clause creates a relationship if it not exists.

 

Delete all the nodes by executing below command.

 

Create nodes by executing below command.

CREATE (r:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true}), (m:Person{name: "Murali Krishna", age: 34, city: "Hyderabad", male: true}) RETURN r, m

 

 


 

Execute below command to create LIKES relation between two nodes.

 

MATCH (p1:Person{name: "Rama Krishna"}), (p2:Person{name: "Murali Krishna"})

MERGE (p1)-[:LIKES]->(p2)

MERGE (p1)<-[:LIKES]-(p2)

RETURN p1, p2




Rerun the same command any number of times, it do not create any new relationship.

Previous                                                    Next                                                    Home

Junit5: ParameterizedTest: String to Object conversion

Junit provides a mechanism to convert from strings to target types. Following conditions should met by the target type or class.

 

Target type must declares exactly one suitable factory method or a factory constructor as defined below.

 

a.   factory method: a non-private, static method declared in the target type that accepts a single String argument and returns an instance of the target type. The name of the method can be arbitrary and need not follow any particular convention.

 

b.   factory constructor: a non-private constructor in the target type that accepts a single String argument. Note that the target type must be declared as either a top-level class or as a static nested class.

 

What if target type has multiple factory methods in the target class?

If multiple factory methods are discovered, they will be ignored.

 

What if target type has both factory method and factory constructor?

Factory method will be used instead of the constructor.

 

Student.java

package com.sample.app.model;

public class Student {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static Student getStudent(String name) {
		Student student = new Student();
		student.name = name;

		return student;
	}

}

 

StringToObjectConverterTest.java

package com.sample.app;

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

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.sample.app.model.Student;

public class StringToObjectConverterTest {
	
	@ParameterizedTest
	@ValueSource(strings = "Gopi")
	void test1(Student student) {
	    assertEquals("Gopi", student.getName());
	}
	
}

 

 

 

  

Previous                                                    Next                                                    Home