Neo4j 简单例子脚本(Groovy 脚本)

Neo4j 是图结构NoSQL的杰出代表,下面一个例子是用 Groovy 写的Neo4j的简单入门脚本,分享在此:

// don't run in Groovy Web Console - run locally (e.g. paste in GroovyConsole)

@GrabResolver(name='neo4j-public-repo', root='http://m2.neo4j.org')
@Grab('org.neo4j:neo4j-kernel:1.1.1')
import org.neo4j.kernel.EmbeddedGraphDatabase
import org.neo4j.graphdb.*

// an enum helper
enum MyRelationshipTypes implements RelationshipType { knows }

// some optional metaclass syntactic sugar
Node.metaClass {
propertyMissing { String name, val -> delegate.setProperty(name, val) }
propertyMissing { String name -> delegate.getProperty(name) }
methodMissing { String name, args -> delegate.createRelationshipTo(args[0], MyRelationshipTypes."$name") }
}
Relationship.metaClass {
propertyMissing { String name, val -> delegate.setProperty(name, val) }
propertyMissing { String name -> delegate.getProperty(name) }
}

// real example starts here
def graphDb = new EmbeddedGraphDatabase("graphdb")
def tx = graphDb.beginTx()
def firstNode, secondNode, relationship
try {
firstNode = graphDb.createNode()
secondNode = graphDb.createNode()
relationship = firstNode.knows(secondNode)
firstNode.message = "Hello,"
secondNode.message = "world!"
relationship.message = "brave Neo4j"
tx.success()
} finally {
tx.finish()
println "$firstNode.message $relationship.message $secondNode.message"
// => Hello, brave Neo4j world!
graphDb.shutdown()
}

anyShare据说看到好文章不转的人,服务器容易宕机!
          

无觅相关文章插件,快速提升流量

分类 未分类 · tag ,