LoginSignup
1
1

More than 5 years have passed since last update.

[Neo4J]  ⑥ 組み込みサーバーモードでCypherを実行

Last updated at Posted at 2015-11-16

Scalaで学習している特典(?)として、先人の取り組みを参考に、組み込みサーバーモードでNeo4Jを立ち上げ、内部からCypherを実行してみた。要するに、これだけ:
https://github.com/kmry/neo4j-embedded

Neo4Jのクエリ言語Cypherをコードで追いかけたい人向けに、書捨てコードをさらしておく
(参考 : http://www.creationline.com/lab/7685)。

package ex1
import org.neo4j.graphdb.factory._
import org.neo4j.graphdb.{DynamicRelationshipType, GraphDatabaseService, Node}
import org.neo4j.cypher.ExecutionEngine

object App {
  val db: GraphDatabaseService = new GraphDatabaseFactory().newEmbeddedDatabase("tmp/neo_yours");
  val engine: ExecutionEngine = new ExecutionEngine(db)

  val cyphers= Array(
    """CREATE (movie:Movie { title:"The Matrix",released:1997 })""",
    """CREATE (movie:Movie { title:"The Matrix",revised:2003 })""",
    """
    MATCH (movie:Movie { title:"The Matrix"})
    RETURN movie
    """,
    """
    MATCH (movie:Movie {title:"The Matrix"})
    SET movie.japanese="マトリックス"
    RETURN movie.title, movie.japanese, movie.released
    """,
    """ MATCH (movie:Movie { title:"The Matrix"}) RETURN movie"""
  )

  def op ={
    val ns = Stream.from(1).iterator //counter
    cyphers.foreach{ c =>
      println (s"コマンド${ns.next} :")
      println (engine.execute(c).dumpToString())
    }
  }
  def main(args: Array[String]): Unit = {
    println("db running")
    op
    db.shutdown()
    println("db shutdown")
  }

}

実行結果はこんな感じ。

$ sbt run
[info] Set current project to neo4j-emb-scala-cypher (in build file:/neo4j-embedded/)
[info] Compiling 1 Scala source to /neo4j-embedded/target/scala-2.11/classes...
[warn] there was one deprecation warning; re-run with -deprecation for details
[warn] one warning found
[info] Running ex1.App 
db running
コマンド1 :
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1

コマンド2 :
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1

コマンド3 :
+-------------------------------------------+
| movie                                     |
+-------------------------------------------+
| Node[0]{title:"The Matrix",released:1997} |
| Node[1]{title:"The Matrix",revised:2003}  |
+-------------------------------------------+
2 rows

コマンド4 :
+------------------------------------------------+
| movie.title  | movie.japanese | movie.released |
+------------------------------------------------+
| "The Matrix" | "マトリックス"       | 1997           |
| "The Matrix" | "マトリックス"       | <null>         |
+------------------------------------------------+
2 rows
Properties set: 2

コマンド5 :
+-------------------------------------------------------------+
| movie                                                       |
+-------------------------------------------------------------+
| Node[0]{japanese:"マトリックス",title:"The Matrix",released:1997} |
| Node[1]{japanese:"マトリックス",title:"The Matrix",revised:2003}  |
+-------------------------------------------------------------+
2 rows

db shutdown
[success] Total time: 13 s, completed 2015/11/17 1:38:28

Enjoy ;)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1