import org.neo4j.graphdb.*; public class Neo4jDemo { public static void main(String[] args) { GraphDatabaseService graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder("path/to/neo4j/database") .newGraphDatabase(); try (Transaction tx = graphDb.beginTx()) { Node node1 = graphDb.createNode(); node1.setProperty("name", "Alice"); Node node2 = graphDb.createNode(); node2.setProperty("name", "Bob"); tx.success(); } try (Transaction tx = graphDb.beginTx()) { Node node1 = graphDb.getNodeById(1); Node node2 = graphDb.getNodeById(2); Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName("FRIEND")); rel.setProperty("since", 2020); tx.success(); } try (Transaction tx = graphDb.beginTx()) { Node node1 = graphDb.getNodeById(1); System.out.println("Name: " + node1.getProperty("name")); for (Relationship rel : node1.getRelationships()) { System.out.println("Relation: " + rel.getType().name()); System.out.println("Since: " + rel.getProperty("since")); } tx.success(); } graphDb.shutdown(); } }


上一篇:
下一篇:
切换中文