Giter Club home page Giter Club logo

neo4s's Introduction

neo4s

Neo4j Scala driver.

Features:

  • Non-blocking
  • Streaming
  • Transactions

The goal of this driver is to allow you everything Neo4j transactional endpoint allows you to do, but in Scala.

Setup

libraryDependencies += "ru.dgolubets" %% "neo4s" % "0.1.1"

Simple query

Create a single driver and connection instance per application.

val driver = NeoDriver()
val connection = driver.connection("localhost", 7476)

Run query.

connection.run(cypher"""MATCH (p: Person) -[:ACTED_IN]-> (m: Movie) RETURN p, m.title LIMIT 2""").map { result =>
  result.data.flatMap(_.row).foreach {
    case CypherRow(CyObject(person), CyString(title)) =>
      println(s"${person("name").as[String]} acted in '$title'")
  }
}

Query parameters

Neo4j encourages you to use parametrized queries, so it can cache query plans.

val year = 2008
val statementWithParameters = cypher"""MATCH (m: Movie) WHERE m.released > $year RETURN m"""

Query graphs

connection.run(cypher"""MATCH (p: Person) -[r:ACTED_IN]-> (m: Movie) RETURN p, r, m LIMIT 2""".withGraph).map { result =>
	result.data.flatMap(_.graph).map { graph =>
	  graph.nodes.foreach { r =>
			println(r)
	  }
	  graph.relationships.foreach { r =>
			println(r)
	  }
	}
}

Query stats

connection.run(cypher"""CREATE (m: Movie{ title: "Unknown", released: "2007"})""".withStats).map { result =>
  result.stats.map { stats =>
    println(s"Created ${stats.nodesCreated} nodes.")
  }
}

Transactions

val t = connection.beginTransaction()
t.run(cypher"""CREATE (m: Movie{ title: "Unknown", released: "2007"})""".withStats).map { _ =>
  t.commit()
}

Streaming

Neo4j results can be processed as a stream.

import driver.materializer
connection.stream(cypher"""MATCH (p: Person) -[:ACTED_IN]-> (m: Movie) RETURN p, m.title""").runForeach {
  case CypherStreamResultData(_, Some(row), _) => println(row)
  case CypherStreamError(code, message) =>
  case _ =>
}

neo4s's People

Contributors

dgolubets avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.