Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is...

24
© 2012, 101companies & Software Languages Team (University of Koblenz-Landau) Neo4j: graph-oriented database Software Languages Team University of Koblenz-Landau Ralf Lämmel and Andrei Varanovich Freitag, 14. September 2012

Transcript of Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is...

Page 1: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Neo4j: graph-oriented databaseSoftware Languages Team

University of Koblenz-LandauRalf Lämmel and Andrei Varanovich

Freitag, 14. September 2012

Page 2: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Motivation

highly-connected data

Freitag, 14. September 2012

Page 3: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Use cases

Connected Data (e.g., social graph)

• Routing, Dispatch, and Location-Based Services (delivery services, logistics)

• Recommendation Engines (with product A you want to buy product B)

Freitag, 14. September 2012

Page 4: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Graph

“A Graph —records data in→ Nodes —which have→ Properties”

http://docs.neo4j.org/chunked/milestone/images/graphdb-GVE.svg

Freitag, 14. September 2012

Page 5: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Nodes

http://docs.neo4j.org/chunked/stable/images/graphdb-nodes-overview.svg

“Nodes —are organized by→ Relationships —which also have→ Properties”

Freitag, 14. September 2012

Page 6: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Relationships

http://docs.neo4j.org/chunked/stable/images/graphdb-rels-overview.svg

Freitag, 14. September 2012

Page 7: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Properties

http://docs.neo4j.org/chunked/stable/images/graphdb-properties.svg

Freitag, 14. September 2012

Page 8: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Paths

http://docs.neo4j.org/chunked/stable/images/graphdb-path.svg

Freitag, 14. September 2012

Page 9: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Traversal

“A Traversal —navigates→ a Graph; it —identifies→ Paths —which order→ Nodes”

http://docs.neo4j.org/chunked/milestone/images/graphdb-traversal.svg

Freitag, 14. September 2012

Page 10: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Working with neo4j

• REST

• Web admin tool

• neo4j shell

• Gremlin -- a domain-specific language for graph traversals

• Java, Ruby, etc.

Freitag, 14. September 2012

Page 11: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Basic operation: create

Assign property (shell):cd 8set -t String name “Andrei

Uses REST (POST request)

Freitag, 14. September 2012

Page 12: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Adding relationships

Freitag, 14. September 2012

Page 13: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Basic operation: read

gremlin> g.V==> v[2]==> v[3]==> v[4]==> v[5]==> v[6]==> v[7]==> v[8]

gremlin> g.E==> e[2][8-RELATED_TO->7]

Using gremlin:

All vertices

All edges

Type of the relationship

gremlin> g.v(8).map()==> name=Andrei

Freitag, 14. September 2012

Page 14: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Basic operation: update

node = g.v(8)node.name = “Ralf”node.save

Find the nodeChange the propertySave changes

g.v(8).map()==> name=Ralf

Freitag, 14. September 2012

Page 15: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Basic operation: delete

DELETE http://debeka.uni-koblenz.de:7474/db/data/node/21

node id

HTTP Request

Freitag, 14. September 2012

Page 16: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

DEMO101implementation:gremlin-neo4j

Freitag, 14. September 2012

Page 18: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Total salariesg.V.outE('EMPLOYED').collect{it.salary}.flatten().sum()

all vertices with ‘Employed’ relationship

get ‘salary’ value

to array

sum of the array elements

Freitag, 14. September 2012

Page 19: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Cut salaries

g.V.outE('EMPLOYED').sideEffect{it.salary = (double)it.salary / 2.0}

outbound relation

Freitag, 14. September 2012

Page 20: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

TransactionsTransaction transaction = database.beginTx();try { Node node = database.createNode(); node.setProperty("name", "NoSQL Distilled"); node.setProperty("published", "2012"); transaction.success();} finally { transaction.finish();}

Supported on the API level

Freitag, 14. September 2012

Page 21: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Consistency• Distributing the nodes on different servers -- not

supported.

• Within a single server, data is always consistent (Neo4J is fully ACID-compliant)

• Ensure consistency through transactions.

• In a cluster: a write to the master is eventually synchronized to the slaves, while slaves are always available for read. Writes to slaves are allowed and are immediately synchronized to the master; other slaves will not be synchronized immediately.

Freitag, 14. September 2012

Page 22: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Availability

• Replicated slaves: horizontally scaling read-mostly architecture that enables the system to handle more read load than a single Neo4j database instance can handle.

Freitag, 14. September 2012

Page 23: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Summary

You learned about ...

• principles of graph-oriented databases,

• key properties of neo4j,

• how to use neo4j with web admin console, REST, and Gremlin.

Freitag, 14. September 2012

Page 24: Neo4j: graph-oriented database · • Within a single server, data is always consistent (Neo4J is fully ACID-compliant) • Ensure consistency through transactions. • In a cluster:

© 2012, 101companies & Software Languages Team (University of Koblenz-Landau)

Resources

• neo4j documentation:

http://docs.neo4j.org/

• Gremlin wiki:

https://github.com/tinkerpop/gremlin/wiki

Freitag, 14. September 2012