neo4j入門教程-java中使用Cypher運算元據庫

neo4j入門教程-java中使用Cypher運算元據庫

操作

  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語句

    返回結果如下:
    這裡寫圖片描述
  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);
}
}