• No results found

Graph Databases: Neo4j

N/A
N/A
Protected

Academic year: 2021

Share "Graph Databases: Neo4j"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

Course NDBI040: Big Data Management and NoSQL Databases

Graph Databases: Neo4j

Martin Svoboda

5. 1. 2016

Practice 05:

(2)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 2

Outline

Graph databases

Neo4j tutorial

Assignment 5

(3)

Neo4j

(4)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 4

Installation

Download

http://neo4j.com/download/other-releases/

Neo4j 2.3.1

Community edition

Windows 32 bit

ZIP distribution

(5)

Embedded Neo4j

NetBeans project

Create a new project

Java application

Add all Neo4j libraries

(6)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 6

Connection

Create an embedded database

org.neo4j.graphdb.factory.GraphDatabaseFactory

org.neo4j.graphdb.GraphDatabaseService

GraphDatabaseService db =

new GraphDatabaseFactory().

newEmbeddedDatabase("db-dir-name");

Close connection

db.shutdown();

(7)

Transactions

Prepare and use transactions

org.neo4j.graphdb.Transaction

Transaction tx = db.beginTx();

try {

tx.success();

} catch (Exception e) {

tx.failure();

} finally {

tx.close();

}

(8)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 8

Nodes

Create new nodes and add properties

org.neo4j.graphdb.Node

Node n1 = db.createNode();

n1.setProperty("name", "Irena");

Node n2 = db.createNode();

n2.setProperty("name", "Martin");

Access existing nodes

db.getNodeById(1);

(9)

Relationships

Define relationship types

org.neo4j.graphdb.RelationshipType

enum RelTypes implements RelationshipType {

KNOWS, …

}

Create a new relationship

org.neo4j.graphdb.Relationship

Relationship r1 =

n1.createRelationshipTo(n2, RelTypes.KNOWS);

r1.setProperty("from", 2008);

Access relationships

(10)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 10

Traversals

Traversal framework

Description

Expanders – relationship types and directions to follow

Order – depth-first / breadth-first traversal algorithm

Uniqueness – visit nodes / relationships / paths only once

Evaluator – traversal continuation and result inclusion

Starting nodes

(11)

Traversals

Find all friends of Irena

org.neo4j.graphdb.traversal.TraversalDescription

TraversalDescription td =

db.traversalDescription();

td.breadthFirst();

td.evaluator(…);

td.relationships(…);

td.uniqueness(…);

Traverser t = td.traverse(n1);

for (Path p : t) { ... }

(12)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 12

Cypher

Cypher query clauses

START – starting points

MATCH – graph pattern to match

WHERE – filtering criteria

RETURN – result construction

CREATE – insert new data

DELETE – remove existing data

(13)

Queries

Find all persons and their names

org.neo4j.graphdb.Result

Result result = db.execute(

"MATCH (n) RETURN n, n.name"

);

while (result.hasNext()) {

Map<String, Object> row = result.next();

for (Entry<String, Object> column :

row.entrySet()) {

}

}

(14)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 14

References

Neo4j

http://neo4j.com/

Cypher Query Language

http://neo4j.com/docs/stable/cypher-query-lang.html

Java and embedded Neo4j

http://neo4j.com/docs/stable/

(15)

Assignment 5

(16)

NDBI040: Big Data Management and NoSQL Databases | Practice 05: Graph Databases: Neo4j | 5. 1. 2016 16

Assignment 5

Create a non-trivial real-world application

that will work with Neo4j embedded in Java

Choose any database domain

E.g. social network, geographical maps, …

Create a meaningful application

Comment your source code and queries

Submit your solution by e-mail

[email protected]

(17)

Assignment 5

Requirements

Create a database with at least 2 kinds of nodes

and 2 types of relationships

Insert about 10 nodes and 10 relationships,

both with a few meaningful properties

Define and process results of at least 2 traversals

Use various expanders, evaluators, …

Execute at least 5 Cypher queries

References

Related documents

Drug IV PO Methadone and buprenorphine equivalents are controversial. Do not make adjustments without consulting CPS or APS. Consult CPS for patients on Methadone, Suboxone, Subutex,

In scenario 2, the results of the proposed workload consolidation schemes SA and GA were presented and results have shown that both the proposed schemes compared to other

These face-to- face interviews revealed five major themes related to peer support: mental health literacy; stigma reduction; the effects of police culture; the need for

As it is stated by the informants, it is considered that the most beneficial combination model of working capital funding sources for farmers is the combination between

(1) a historical introduction to world city networks, focusing on Amsterdam, (2) the contemporary connectivity and competition of Amsterdam, Utrecht, Rotterdam and The Hague, to

This reaction can be used for distinguishing and separating 1 o  , 2 o  and 3 o amines (i) Primary amine: The amide formed is soluble in alkali because the hydrogen attached

The following areas for further research in the fashion and apparel design industry were proposed by the university FAD staff: The future of clothing and textiles in

Pauline Oliveros Archive, New York Public Library: JPB 94–5 Pauline Oliveros Archive, Houston Public Library: MSS 1308 Elinor Barron Private Collection, Oakland, California Kay