操作
- 节点的操作
- 创建节点
Node node1=graphdb.createNode();
- 节点添加属性
node1.setProperty("message", "Hello");
- 获取属性
node1.getProperty("message");
- 节点删除
node1.delete()
- 关系的操作
- 创建
relationship = node1.createRelationshipTo(node2, RelTypes.KNOWS);
- 添加属性
relationship.setProperty("message", "brave neo4j");
Cypher
-
ExecutionEngine engine = new ExecutionEngine(graphdb); //构造引擎
ExecutionResult result = engine.execute("start n=node(1) return n,n.message"); //执行cypher语句
返回结果如下:
- 模板如下
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 )
{
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
public static void main(String[] args) {
graphdb = new GraphDatabaseFactory().newEmbeddedDatabase("db");
nodeIndex = graphdb.index().forNodes("nodex");
Transaction tx=graphdb.beginTx();
try{
tx.success();
System.out.println("successfully");
}
finally{
tx.finish();
}
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);
}
}