Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

driver factsheet (WIP)

mperdeck edited this page Jul 11, 2013 · 20 revisions

Driver Fact-Sheet Template

This is work in progress, the goal is to have one fact sheet template that is used across all drivers and the fact-sheet.md is part of each drivers repository and can be read and inlined from neo4j.org

Driver Name, Language, Author

Elevator pitch

(one sentence)

Code-Teaser

(powerful but simple)

Versions, Compatibility with Neo4j versions

Download, Configuration, Setup

General usage

Neo4j Introduction

Neo4j is a transactional, open-source graph database. A graph consists of labeled nodes connected by directed, typed relationships. Both can have arbitrary key-value properties. Graphs are explored by finding patterns starting at some initial set of nodes. A graph query language like Cypher helps expressing complex operations in a readable way. Neo4j makes it easy to store and query complex, connected domains and support use-cases that require non-obvious answers. Of course it also runs your typical graph algorithms.

Startup, Shutdown of the database

How to use Transactions

How to run a Cypher statement with parameters

Example "social movie database"

(user)-[rating:RATED]->(movie)<-[role:ACTED_IN]-(actor)

How to create a actor playing in a movie

How to find a movie by title

How to index a movie by title and use the index for search

How to find actors and roles

How to update properties on actor and role

How to delete an actor (and his roles)

How to do a complex query (actors of a movie, co-actors)

How to test

Next steps

  • more examples
  • documentation
  • source-code
  • how-to-contribute

Resources


Java-Rest-Binding by the Neo4j-Team

The Java-Rest-Binding offers a Java-API to access the REST-API of the Neo4j-Server and provides also a drop-in replacement for the GraphDatabaseService interface for simple use-cases that are not performance critical. Both Cypher queries as well as traversals are executed on the server side.

Code-Teaser

RestGraphDatabase gdb = new RestGraphDatabase(rest);
RestNode node = gdb.createNode(map("title","The Matrix"),"Movie");
Result<Node> matrix = gdb.query("match m:Movie where m.title={title} return m",map("title","The Matrix")).to(Node.class).getSingle();

Versions, Compatibility with Neo4j versions

  • Version 1.9.RC1 is compatible with Neo4j 1.9.GA
  • Version 2.0.M02 is compatible with Neo4j 2.0.M02

Download, Configuration, Setup

It is available on GitHub and in the Neo4j Maven Repository.

Maven dependency, accordingly for other build systems

<dependency>
	<groupId>org.neo4j</groupId>
	<artifactId>java-rest-binding</artifactId>
	<version>1.9.RC1</version>
</dependency>

General usage

String url="http://localhost:PORT/db/data";
RestApi rest = new RestApiFacade(url,[user],[pass]);

QueryExecutor executor = new RestCypherQueryExecutor(rest);
executor.query("start n=node:node_auto_index(title={title}) return n",map("title","Forrest Gump"));

GraphDatabaseService rdb = new RestGraphDatabase(rest);
RestNode node = rdb.findNodeById(100);

Neo4j Introduction

Neo4j is a transactional, open-source graph database. A graph consists of labeled nodes connected by directed, typed relationships. Both can have arbitrary key-value properties. Graphs are explored by finding patterns starting at some initial set of nodes. A graph query language like Cypher helps expressing complex operations in a readable way. Neo4j makes it easy to store and query complex, connected domains and support use-cases that require non-obvious answers. Of course it also runs your typical graph algorithms.

Startup, Shutdown of the database

As the Neo4j server hosts the database that is connected to, the lifecycle lies in starting and stoppign the server bin/neo4j start|stop.

How to use Transactions

Each http request to the server runs in its own transaction. The only exception is the batch-REST-Api which can run several operations in one single transaction

How to run a Cypher statement with parameters

QueryExecutor executor = new RestCypherQueryExecutor(rest); Iterator titles = executor.query("start n=node:node_auto_index(title={title}) return n.title",map("title","Forrest Gump")).to(String.class);

Example "social movie database"

(user)-[rating:RATED]->(movie)<-[role:ACTED_IN]-(actor)

How to create a actor playing in a movie

Node keanu = rdb.createNode(map("name","Keanu Reeves"),"Actor");
Node matrix = rdb.createNode(map("title","The Matrix"),"Movie");
keanu.createRelationshipTo(matrix, "ACTED_IN", map("role","Neo"));

How to find a movie by title

only possible with Cypher, client-side iteration is not sensible

  Node matrix=executor.query("start n=node(*) where n.title! = {title} return n;",map("title","The Matrix")).to(Node.class).getSingle();

How to index a movie by title and use the index for search

rdb.index().forNodes("Movie").add(matrix,"title",matrix.getProperty("title"));

Node foundMovie=rdb.index().forNodes("Movie").get("title","The Matrix").getSingle();

How to find actors and roles

  for (Relationship role : matrix.getRelationships(Types.ACTED_IN,Direction.INCOMING)) {
     String roleName = (String)role.getProperty("role");
     Node actor = role.getStartNode();
     String actorName = (String)actor.getProperty("name");
  }

How to update properties on actor and role

actor.setProperty("name","Tom Hanks");
role.setProperty("difficulty","easy");

How to delete an actor (and his roles)

for (Relationship r : actor.getRelationships()) {
	r.delete();
}
actor.delete();

How to do a complex query (actors of a movie, co-actors)

How to test

You can test with an embedded Neo4j Server by using the WrappingNeoServer around a database of your choice, preferably an ImpermanentGraphDatabase. Then you connect your RestApiFacade to http://localhost:PORT/db/data

private WebServer startWebServer(GraphDatabaseAPI gdb, int port, boolean auth) {
    final ServerConfigurator config = new ServerConfigurator(gdb);
    config.configuration().setProperty(Configurator.WEBSERVER_PORT_PROPERTY_KEY,port);
    final WrappingNeoServer wrappingNeoServer = new WrappingNeoServer(gdb, config);
    final WebServer webServer = wrappingNeoServer.getWebServer();
    if (auth) webServer.addFilter(new TestAuthenticationFilter(), "/*");
    wrappingNeoServer.start();
    return webServer;
}

Next steps

  • more examples // Gensen, Community Graph, Spring Data Neo4j
  • documentation // github
  • source-code // github
  • how-to-contribute // github

Resources

  • Authors personal page
  • neo4j.org
  • neo4j.org/tracks/cypher
  • neo4j.org/drivers
  • neo4j.org/q_and_a

Spring Data Neo4j 2.2.1.RELEASE, by the Neo4j and Spring-Data Team

Elevator Pitch

Spring Data Neo4j enables simpel object-graph mapping using annotated entities. As part of the Spring Data project it leverages the powerful template, mapping and repository abstractions.

Code-Teaser

@NodeEntity
class Movie {
    @GraphId Long id;
	@Indexed String title;
	@RelatedToVia Set<Role> cast;
}

Iterable<Movie> movies = movieRepository.findByCastActorName("Keanu Reeves");

Versions, Compatibility with Neo4j versions

  • 2.2.1.RELASE is compatible with Neo4j 1.8.x and Neo4j 1.9.x
  • 3.0.RELEASE will be compatible with Neo4j 2.0

Download, Configuration, Setup

It is available on Maven Central and the SpringSource Maven repositories as well as a download from springsource.org. Depending on the used modules, use different artifact-id's.

<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-neo4j[-aspects|-rest|-cross-store]</artifactId>
	<version>2.2.1.RELEASE</version>
</dependency>

General usage

Neo4j Introduction

Neo4j is a transactional, open-source graph database. A graph consists of labeled nodes connected by directed, typed relationships. Both can have arbitrary key-value properties. Graphs are explored by finding patterns starting at some initial set of nodes. A graph query language like Cypher helps expressing complex operations in a readable way. Neo4j makes it easy to store and query complex, connected domains and support use-cases that require non-obvious answers. Of course it also runs your typical graph algorithms.

Startup, Shutdown of the database

If you run embedded, you declare either the path to the graph database in your spring config or provide a bean reference to a GraphDatabaseService bean. Running against Neo4j server requires a bean reference to SpringRestGraphdatabase with the appropriate URL configured. There are other options for HA or running as Server Extension, please refer to the documentation for this.

The graph database lifecycle will be handled by the spring container, a appCtx.close() shuts the database down.

How to use Transactions

With the Spring Framework you usually use declarative transaction management with annotations, make sure that your config enables the transaction managment and the annotation scanning for @Transactional. There is also @Neo4jTransactional for co-existence with a relational transaction manager.

How to run a Cypher statement with parameters

Example "social movie database"

(user)-[rating:RATED]->(movie)<-[role:ACTED_IN]-(actor)

How to create a actor playing in a movie

How to find a movie by title

How to index a movie by title and use the index for search

How to find actors and roles

How to update properties on actor and role

How to delete an actor (and his roles)

How to do a complex query (actors of a movie, co-actors)

How to test

Next steps

Resources

  • Authors: Michael Hunger, Lasse West-Nielsen, Oliver Gierke, David Montag
  • neo4j.org
  • neo4j.org/tracks/cypher
  • neo4j.org/drivers
  • neo4j.org/q_and_a

Neo4jClient, for .NET, by Readify (Wiki section is work in progress)

A .NET client for Neo4j. Supports basic CRUD operations, Cypher and Gremlin queries via fluent interfaces, and some indexing operations.

Code-Teaser

var booksQuery = client
    .Cypher
    .Start(new { root = client.RootNode })
    .Match("root-[:HAS_BOOK]->book")
    .Where((Book bk) => bk.Pages > 5)
    .Return<Node<Book>>("book");

Versions, Compatibility with Neo4j versions

Versions are released very frequently via NuGet: http://nuget.org/packages/Neo4jClient/

Neo4jClient is compatible with Neo4j 1.8.x and Neo4j 1.9.x. As of Neo4jClient 1.0.0.589, it works with Neo4j 2.0, but 2.0 specific features are not yet supported. (For example, labels.)

Download, Configuration, Setup

Neo4jClient ships exclusively as a NuGet package.

To install it:

  • Open the package manager console in Visual Studio: Tools | Library Package Manager | Package Manager Console
  • Run Install-Package Neo4jClient

This only installs the client. To install a Neo4j database on a Windows server, visit http://www.neo4j.org/download/windows

General usage

Neo4j Introduction

Neo4j is a transactional, open-source graph database. A graph consists of labeled nodes connected by directed, typed relationships. Both can have arbitrary key-value properties. Graphs are explored by finding patterns starting at some initial set of nodes. A graph query language like Cypher helps expressing complex operations in a readable way. Neo4j makes it easy to store and query complex, connected domains and support use-cases that require non-obvious answers. Of course it also runs your typical graph algorithms.

Startup, Shutdown of the database

Not applicable

How to use Transactions

Each http request to the server runs in its own transaction.

Support for batching multiple requests in the one transaction (via REST batch) is not yet included.

How to run a Cypher statement with parameters

Neo4jClient's fluent Cypher interface leverages Cypher parameters automatically.

For example, a fluent query like this:

var booksQuery = client
    .Cypher
    .Start(new { root = client.RootNode })
    .Match("root-[:HAS_BOOK]->book")
    .Where((Book bk) => bk.Pages > 5)
    .Return<Node<Book>>("book");

Results in a JSON payload like this:

{
    "query": "START root=node({p0}) MATCH root-[:HAS_BOOK]->book WHERE bk.Pages > {p1} RETURN book",
    "parameters": {
        "p0": 0,
        "p1": 5
    }
}

Example "social movie database"

(user)-[rating:RATED]->(movie)<-[role:ACTED_IN]-(actor)

How to create a actor playing in a movie

// Create relationship with root node, so we can access the movie.
// Create movie and relationship in one go, so there is only one access to the database
var movie = client.Create(new Movie { Title = "The Matrix" }, new HasMovie(client.RootNode));

var actor = client.Create(new Actor { Name = "Keanu Reeves" }, new ActedIn(movie, new ActedInProperties { Role = "Neo" }));

// ---------------
// Declarations

public class Movie
{
    public string Title { get; set; }
}

public class Actor
{
    public string Name { get; set; }
}

public class ActedIn : Relationship, IRelationshipAllowingSourceNode<Actor>, IRelationshipAllowingTargetNode<Movie>
{
    public ActedIn(NodeReference<Movie> targetNode, ActedInProperties actedInProperties) : base(targetNode, actedInProperties) { }
    public override string RelationshipTypeKey { get { return "ACTED_IN"; } }
}

public class ActedInProperties
{
    public string Role { get; set; }
}

public class HasMovie : Relationship, IRelationshipAllowingSourceNode<RootNode>, IRelationshipAllowingTargetNode<Movie>
{
    public HasMovie(NodeReference<Movie> targetNode): base(targetNode) {}
    public HasMovie(NodeReference<RootNode> rootNode) : base(rootNode) { } 
    public override string RelationshipTypeKey { get { return "HAS_MOVIE"; } }
}

How to find a movie by title

var movies = client
            .Cypher
            .Start(new { root = client.RootNode })
            .Match("root-[:HAS_MOVIE]->movie")
            .Where((Movie movie) => movie.Title == "The Matrix")
            .Return<Node<Movie>>("movie")
            .Results;

How to index a movie by title and use the index for search

// index a movie by title

var theMatrix = new Movie { Title = "The Matrix" };

client.Create(
    theMatrix,
    new IRelationshipAllowingParticipantNode<Movie>[0],
    new[]
    {
        new IndexEntry("Movie")
        {
            { "Title", theMatrix.Title }
        }
    });

//  use the index for search

IEnumerable<Node<Movie>> movies = client
                                  .Cypher
                                  .Start(new { movie = Node.ByIndexLookup("Movie", "Title", "The Matrix" )})
                                  .Return<Node<Movie>>("movie")
                                  .Results;

How to find actors and roles

// Find all actors playing in the movie The Matrix, and their roles
var actorsAndRoles = client
    .Cypher
    .Start(new { theMatrix = theMatrix })
    .Match("actor-[r:ACTED_IN]->theMatrix")
    .Return((actor, r) => new
    {
        Actor = actor.As<Node<Actor>>(),
        // ActedInProperties contains all properties of the relationship, such as Role
        ActedInProperties = r.As<Node<ActedInProperties>>()
    })
    .Results;

How to update properties on actor and role

client.Update(actor, node => { node.Name = "Hugo Weaving"; });
client.Update(movie, node => { node.Title = "The Matrix Reloaded"; });

How to delete an actor (and his roles)

client.Delete(actor, DeleteMode.NodeAndRelationships);

How to do a complex query (actors of a movie, co-actors)

// Find all movies that Keano Reeves played in, and his co-actors in each movie
// Cypher pattern:
// keanoReeves-[:ACTED_IN]->movie<-[?:ACTED_IN]-coActor

var moviesAndCoactors = client
    .Cypher
    .Start(new { keanoReeves = keanoReeves })
    .Match("keanoReeves-[:ACTED_IN]->movie<-[?:ACTED_IN]-coActor")
    .Where((Actor coActor) => coActor.Name != "Keanu Reeves")
    .Return((movie, coActor) => new
    {
        Movie = movie.As<Node<Movie>>(),
        CoActor = coActor.As<Node<Actor>>()
    })
    .Results;

How to test

Not applicable

Next steps

Resources

Clone this wiki locally