neo4j入门教程-java中使用Cypher操作数据库

时间:2022-10-12 18:04:07

操作

  1. 节点的操作
    1. 创建节点Node node1=graphdb.createNode();
    2. 节点添加属性node1.setProperty("message", "Hello");
    3. 获取属性 node1.getProperty("message");
    4. 节点删除node1.delete()
  2. 关系的操作
    1. 创建 relationship = node1.createRelationshipTo(node2, RelTypes.KNOWS);
    2. 添加属性relationship.setProperty("message", "brave neo4j");

Cypher

  1. ExecutionEngine engine = new ExecutionEngine(graphdb); //构造引擎
    ExecutionResult result = engine.execute("start n=node(1) return n,n.message"); //执行cypher语句

    返回结果如下:
    neo4j入门教程-java中使用Cypher操作数据库
  2. 模板如下
package testneo;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.relation.Relation;

import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotInTransactionException;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ReturnableEvaluator;
import org.neo4j.graphdb.StopEvaluator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.Traverser.Order;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.traversal.Traverser;

public class test {

private static GraphDatabaseService graphdb;
private static Index<Node> nodeIndex;
private static Relationship relationship;
public enum RelTypes implements RelationshipType{
USERS_REFERENCE,
USER,
KNOWS
}

private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running example before it's completed)
/*为了确保neo4j数据库的正确关闭,我们可以添加一个关闭钩子方法
* registerShutdownHook。这个方法的意思就是在jvm中增加一个关闭的
* 钩子,当jvm关闭的时候,会执行系统中已经设置的所有通过方法
* addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。
* 所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。*/

Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}

public static void main(String[] args) {

graphdb = new GraphDatabaseFactory().newEmbeddedDatabase("db");
//your codes
//***************************************************
nodeIndex = graphdb.index().forNodes("nodex");
Transaction tx=graphdb.beginTx();
try{
// neo4j statement
tx.success();
System.out.println("successfully");
}
finally{
tx.finish();
}
//cyper statement
ExecutionEngine engine = new ExecutionEngine(graphdb);
Map<String, Object> params = new HashMap<String, Object>();
params.put( "id", Arrays.asList(1,2) );
ExecutionResult result = engine.execute( "xxx");

System.out.println(result);

List<String> columns = result.columns();
System.out.println( columns );

//***************************************************
registerShutdownHook(graphdb);
}

}