入门Neo4j,在官网案例的基础上添加了两个方法,下文程序的功能分别是:获取图数据库连接驱动、往标签添加节点、打印节点信息、获取所有节点数据、获取节点和关系、关闭连接。
执行以下程序需要的lib,分别是Neo4j安装包下的lib,以及neo4j-java-driver-x.x.x.jar
import org.neo4j.driver.v1.*;
import org.neo4j.driver.v1.types.Node;
import org.neo4j.driver.v1.types.Path;
import org.neo4j.driver.v1.types.Relationship;
import java.util.Iterator;
import java.util.List;
import static org.neo4j.driver.v1.Values.parameters;
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(uri, AuthTokens.basic(user, password));
}
/**
* 添加节点
* @param name
*/
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())
{
// x 表示一个占位符