0、概述
本文以Neo4j 3.3.9为基础,采用https://neo4j.com/docs/api/java-driver/current/给出的官方例子。下面大概记录下工程建立的过程,因为版本差异代码和官方给出的稍有差别。
1、使用eclipse,用自己安装的mvn新建一个mvn工程;
2、pox.xml
# 只复制Neo4j dependency即可,其它的东西不用变
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.neo4j.study</groupId><artifactId>sample</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>sample</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-jdbc-driver --><dependency><groupId>org.neo4j.driver</groupId><artifactId>neo4j-java-driver</artifactId><version>1.7.2</version></dependency></dependencies>
</project>
3、SmallExample.java
package com.neo4j.study.sample;import java.util.Iterator;
import java.util.List;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.Transaction;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.Values;
import org.neo4j.driver.v1.types.Node;
import org.neo4j.driver.v1.types.Path;
import org.neo4j.driver.v1.types.Relationship;/*** Hello world!**/
public class SmallExample {// Driver objects are thread-safe and are typically made available// application-wide.Driver driver;public SmallExample(String uri, String user, String password) {driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "5tgbhu8"));}private void addPerson(String name) {// Sessions are lightweight and disposable connection wrappers.try (Session session = driver.session()) {// Wrapping Cypher in an explicit transaction provides atomicity// and makes handling errors much easier.try (Transaction tx = session.beginTransaction()) {tx.run("Merge (a:People {name: {name}})", Values.parameters("name", name));tx.success(); // Mark this write as successful.}}}private void printPeople(String name) {Session session = driver.session();// Auto-commit transactions are a quick and easy way to wrap a read.StatementResult result = session.run("MATCH (a:People) WHERE a.name STARTS WITH {name} RETURN a.name as Name",Values.parameters("name", name));// Each Cypher execution returns a stream of records.while (result.hasNext()) {// Record 是一行记录,内容是什么取决于你return的东西Record record = result.next();System.out.println(record);// Values can be extracted from a record by index or name.
// System.out.println(record.get("PeopleName").asString());System.out.println(String.format("%s", record.get("Name").asString()));}}private void getPeoples() {Session session = driver.session();// Auto-commit transactions are a quick and easy way to wrap a read.StatementResult result = session.run("MATCH (b:People) RETURN b");// Each Cypher execution returns a stream of records.while (result.hasNext()) {// Record 是一行记录,内容是什么取决于你return的东西Record record = result.next();System.out.println(record);List<Value> list = record.values();for (Value v : list) {Node n = v.asNode();System.out.println(n.labels().iterator().next() + "--" + n.id());for (String k : n.keys()) {System.out.println(k + "---" + n.get(k));}System.out.println("==========================");}// Values can be extracted from a record by index or name.
// System.out.println(record.get("b").asString());}}private void getPeoplesAndRelation(){Session session = driver.session();// Auto-commit transactions are a quick and easy way to wrap a read.StatementResult result = session.run("MATCH p=(b:People)-[]-(c) RETURN p");
// "MATCH (b:People)-[]-(c) RETURN b,c");// Each Cypher execution returns a stream of records.while (result.hasNext()){//Record 是一行记录,内容是什么取决于你return的东西Record record = result.next();System.out.println(record);List<Value> list = record.values();for(Value v : list){Path p = v.asPath();Node start = p.start();for(String k:start.keys()){System.out.println(k+"---"+start.get(k) );}System.out.println("==========================");Iterator i = p.relationships().iterator();while(i.hasNext()){
// System.out.println(i.next()+"--------------------------");Relationship r = (Relationship)i.next() ;System.out.println(r.type());System.out.println(r.startNodeId() + "->"+r.endNodeId());System.out.println(r.id());}Node end = p.end();for(String k:end.keys()){System.out.println(k+"---"+end.get(k) );}System.out.println("==========================");// Node n = v.asNode();
// System.out.println(n.labels().iterator().next()+"--"+n.id());
//
// for(String k:n.keys())
// {
// System.out.println(k+"---"+n.get(k) );
// }
// System.out.println("==========================");}// Values can be extracted from a record by index or name.
// System.out.println(record.get("b").asString());}}public void close() {// Closing a driver immediately shuts down all open connections.driver.close();}public static void main(String[] args) {// very usefully from https://www.cnblogs.com/April1995/p/10526152.html
// Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "5tgbhu8"));
// try (Session session = driver.session()) {
// try (Transaction transaction = session.beginTransaction()) {
// transaction.run("create(n:A1{NAME:{NAME},TITLE:{TITLE}})",
// Values.parameters("NAME", "xiaoming", "TITLE", "Wang"));
// transaction.success();
// }
// try (Transaction tx = session.beginTransaction()) {
// StatementResult result = tx.run(
// "match(a:A1) WHERE a.NAME = {NAME} RETURN a.NAME AS NAME,a.TITLE AS TITLE",
// Values.parameters("NAME", "xiaoming"));
// while (result.hasNext()) {
// Record record = result.next();
// System.out.println(
// String.format("%s %s", record.get("TITLE").asString(), record.get("NAME").asString()));
// }
// }
// }
// driver.close();SmallExample example = new SmallExample("bolt://master:7687", "neo4j", "123456");
// example.addPerson("Ada");
// example.addPerson("Alice");
// example.addPerson("Bob");
// example.printPeople("A");
// example.getPeoples();example.getPeoplesAndRelation();example.close();}
}
4、Neo4j可视化使用的技术
Neo4j的brower 展现是用:D3.js,查Neo4j,转为D3需要的格式,传给D3进行展示。