Neo4j is a graph database, mainly including nodes and relationships. Both nodes and relationships can contain properties.
Install
Download Neo4j: https://neo4j.com/artifact.php?name=neo4j-community-3.3.5-windows.zip
Unzip it and run:
D:\Tools\neo4j-community-3.3.5\bin>neo4j.bat console
2018-04-13 06:03:08.826+0000 INFO ======== Neo4j 3.3.5 ========
2018-04-13 06:03:08.853+0000 INFO Starting...
2018-04-13 06:03:09.879+0000 INFO Bolt enabled on 127.0.0.1:7687.
2018-04-13 06:03:14.553+0000 INFO Started.
2018-04-13 06:03:15.612+0000 INFO Remote interface available at http://localhost:7474/
Visit Neoj Browser: http://127.0.0.1:7474/browser/ , the first visit will prompt you to change the password.
Click on Database in the left column,
Node Labels represents the existing node
Relationship Types represents the existing relationship
Property Keys represents the properties of the node or relationship
Above the middle part you can enter the command to be executed.
Next, create a node through the command, the name of the node is Message, and there are two attributes title and text.
CREATE (:Message { title:'Welcome',text:'Hello world!' });
You can see that the Message node appears under the Node Labels in the left column.
The node just created can now be queried with the following command:
MATCH (n:Message) RETURN n LIMIT 25;
Create another Language node:
CREATE (:Language { name:'Java',version:'1.8' });
With the Message node and Language node, you can add the relationship between the two nodes.
MATCH (m:Message),(c:Language)
WHERE m.title = 'Welcome' AND c.name = 'Java'
CREATE (m)-[:ACCESSED_FROM]->(c);
Now you can see the relationship you just created under Relationship Types on the left column, the name is ACCESSED_FROM. Click ACCESSED_FROM to view the relationship between two nodes graphically.
Let's query the relationship we just created:
MATCH (m:Message)-[:ACCESSED_FROM]->(l:Language)
RETURN m.title,l.name;
Let's access neo4j through the Java API.
Create a new project and add maven dependencies:
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.2.1</version>
</dependency>
Here is the Java test code:
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Values;
public class Neo4jTest {
public static void main(String[] args) {
Driver driver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "000000"));
Session session = driver.session();
session.run("CREATE (:Message {title: 'Hello', text: 'hello world'})");
session.run("CREATE (:Language {name: 'Java8', version: '1.8'})");
String createRelationship = "MATCH (m:Message),(l:Language) WHERE m.title = 'Hello' AND l.name='Java8'"
+ " CREATE (m)-[:HelloJava8]->(l);";
session.run(createRelationship);
String queryRelationship = "MATCH (m:Message)-[:HelloJava8]->" + "(l:Language {name:{language}}) "
+ "RETURN m.title,l.name;";
StatementResult resultSet = session.run(queryRelationship, Values.parameters("language", "Java8"));
while (resultSet.hasNext()) {
Record result = resultSet.next();
System.out.println(result.get("m.title") + " from " + result.get("l.name"));
}
session.close();
}
}
Running result:
"Hello" from "Java8"
Nodes and relationships created through the Java API can now be seen in the neo4j browser: