Neo4j3.5学习笔记——Cypher查询语句

tech2022-08-02  151

跟着官网代码学习.jpg–jdk 1.8.0 & neo4j 3.5 https://neo4j.com/docs/java-reference/current/java-embedded/

1. 先建立一个database并存入点data

GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( databaseDirectory ); try ( Transaction tx = db.beginTx()) { Node myNode = db.createNode(); myNode.setProperty( "name", "my node" ); Node myNode1 = db.createNode(); myNode1.setProperty( "name", "my node1" ); tx.success(); }

2. 执行查询 1)使用entrySet()查询key和value:

db.execute():在指定的数据库连接中执行数据库MATCH语法. try ( Transaction ignored = db.beginTx(); Result result = db.execute( "MATCH (n {name: 'my node'}) RETURN n, n.name" ) ) { while ( result.hasNext() ) { Map<String,Object> row = result.next(); for ( Entry<String,Object> column : row.entrySet() ) { rows += column.getKey() + ": " + column.getValue() + "; "; } rows += "\n"; } }

2)使用Iterator<>查询key和value:

db.execute():在指定的数据库连接中执行数据库MATCH语法,result的值是MATCH语句中查询得到的n;result存储的是数据库中的my node对应的node;List columns() The exact names used to represent each column in the result set. Returns: List of the column names; org.neo4j.graphdb.ResourceIterator columnAs(String name) Returns an iterator with the result objects from a single column of the result set. This method is best used for single column results. To ensure that any resources, including transactions bound to it, are properly closed, the iterator must either be fully exhausted, or the close() method must be called. Type Parameters: T - desired type cast for the result objects Parameters: name - exact name of the column, as it appeared in the original query Returns: an iterator of the result objects, possibly empty 详情见https://docs.spring.io/spring-data/neo4j/docs/3.4.0.M1/api/org/neo4j/graphdb/Result.html try ( Transaction ignored = db.beginTx(); Result result = db.execute( "MATCH (n1 {name: 'my node'}) RETURN n1, n1.name" ) ) { Iterator<Node> n_column = result.columnAs( "n1" ); for ( Node node : Iterators.asIterable( n_column ) ) { nodeResult = node + ": " + node.getProperty( "name" ); System.out.println(nodeResult); } List<String> columns = result.columns(); columnsString = columns.toString(); resultString = db.execute( "MATCH (n {name: 'my node'}) RETURN n, n.name" ).resultAsString(); }
最新回复(0)