Date post: | 15-Apr-2017 |
Category: |
Data & Analytics |
Upload: | neo4j-the-fastest-and-most-scalable-native-graph-database |
View: | 2,364 times |
Download: | 0 times |
(Michael)-[:WORKS_FOR]->(Neo4j)
[email protected] | @mesirii | github.com/jexp | jexp.de/blog
Michael Hunger - Community Caretaker @Neo4j
• From Pain to Graph• Graphs Are Everywhere• For once no Java Haters• Demo(s)!• Q&A
Today‘s Entertainment
Once Upon A Time in SwedenOnce Upon a Time in Sweden
Solution
History of Neo4j
• 0.x ... small embeddable persistent graph library
• 1.x ... adding indexes, server, first stab of Cypher
• 2.x ... ease of use, data-model, optional schema, cost based optimizer, import, Neo4j-Browser
• 3.x …binary protocol, bytecode compiled queries,sharding
(graphs)-[:ARE]->(everywhere)
Value from Data RelationshipsCommon Use Cases
Internal ApplicationsMaster Data Management
Network and IT Operations
Fraud Detection
Customer-Facing ApplicationsReal-Time Recommendations
Graph-Based SearchIdentity and
Access Management
http://neo4j.com/use-cases
The Whiteboard Model Is the Physical Model
CAR
DRIVES
name: "Dan"born: May 29, 1970
twitter: "@dan"name: "Ann"
born: Dec 5, 1975
since: Jan 10, 2011
brand: "Volvo"model: "V70"
Property Graph Model
Nodes• The objects in the graph• Can have name-value properties• Can be labeled
Relationships• Relate nodes by type and direction• Can have name-value properties
LOVES
LOVES
LIVES WITH
OWN
S
PERSON PERSON
http://neo4j.com/developer/graph-database/#property-graph
Relational to Graph
Relational Graph
KNOWS
KNOWS
KNOWS
ANDREAS
TOBIAS
MICA
DELIA
Person FriendPerson-Friend
ANDREASDELIA
TOBIAS
MICA
http://neo4j.com/developer/graph-db-vs-rdbms/
Neo4j: All About Patterns
(:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} )
LOVES
Dan Ann
LABEL PROPERTY
NODE NODE
LABEL PROPERTY
http://neo4j.com/developer/cypher
Cypher: Find Patterns
MATCH (:Person { name:"Dan"} ) -[:LOVES]-> (love:Person) RETURN love
LOVES
Dan ?
LABEL
NODE NODE
LABEL PROPERTY ALIAS ALIAS
http://neo4j.com/developer/cypher
Demo
http://neo4j.com/developer/guide-neo4j-browser
Neo4j from Java
Good Old Days
<dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>2.2.5</version></dependency>
Neo4j‘s Java API
GraphDatabaseService db = ...Node dan= db.createNode(Person);dan.setProperty("name","Dan");
Relationship rel = dan.createRelationshipTo(ann,LOVES);rel.setProperty("how","very");db.shutdown();
http://neo4j.com/docs/stable/tutorials-java-embedded.html
Neo4j is Transactional
GraphDatabaseService db = ...try (Transaction tx = db.beginTx()) { Node dan = ... Node ann = ... Relationship rel = ... tx.success();}db.shutdown();
Heavy Lifting - Demo
https://gist.github.com/jexp/a54cfa99a1028743858e
Extend Neo4j Server
Extend Neo4j Server
@Path( "/meta" )public class MetaInfoResource { @GET @Produces( MediaType.APPLICATION_JSON ) @Path( "/{node}" ) public Response meta(@PathParam("node") long id, @Context GraphDatabaseService db) {
Iterable labels = db.getNodeById(id).getLabels(); return Response.status( OK ).entity( labels ).build(); }}
http://neo4j.com/docs/stable/server-unmanaged-extensions.html
Cypher from Java
Running Cypher from Javaquery = "MATCH (:Person {name:{name}})-[:LOVES]->(p) RETURN p.name AS loved";params = map("name", "Dan");
try (result = db.execute(query, params)) { for (Map row : result) { row.get("loved"); }} http://neo4j.com/docs/stable/tutorials-cypher-java.html
Running Cypher from Java - Remoteurl = "http://.../db/data/transaction/commit";query = "MATCH ... RETURN loved";params = map("name", "Dan");
r = HTTP.POST(url,"{statements: [{statement:query, parameters:params}]}")r.status() == 200r.content().get("errors") == []r.content().get("results").get(0) ==[{"columns":["loved"],"data": [{"row": ["Ann"]}]}] http://neo4j.com/docs/stable/rest-api-transactional.html
Connect via JDBCconn = driver.connect("jdbc:neo4j://localhost:7474");PreparedStatement ps = conn.prepareStatement(" MATCH (:Person {name:{1}})-[:LOVES]->(loved) RETURN loved.name as loved");
ps.setLong(1,"Dan");ResultSet rs = ps.executeQuery();
while (rs.next()) { rs.getString("loved");} http://neo4j.com/developer/java/#neo4j-jdbc
Database-Tools, ETL, BI-Tools
JVM Languages
Clojure - neocons(ns neocons.docs.examples (:require [clojurewerkz.neocons.rest :as nr] [clojurewerkz.neocons.rest.cypher :as cy]))(defn -main [& args] (nr/connect! "http://host:port/db/data/") (let [query "MATCH (:Person {name:{name}})-[:LOVES]->(loved) RETURN lover.name as loved" res (cy/tquery query {:name "Dan"})] (println res))) http://neo4j.com/developer/clojure
Scala (AnormCypher – Spark)
import org.anormcypher._import org.apache.spark.graphx._
val dbConn = Neo4jREST("localhost", 7474, "/db/data/")val q = """MATCH (p1:Page)-[:Link]->(p2) RETURN id(p1) AS from, id(p2) AS to LIMIT 100000000"""val r = Cypher(q).apply()(dbConn)val links = sc.parallelize(r,100).map( Edge(row[Int]("from").toLong,row[Int]("to").toLong, None)) links.count
http://neo4j.com/developer/scala
Groovy – Batch-Import@Grab('org.neo4j:neo4j:2.2.5')import org.neo4j.graphdb.*
batch = BatchInserters.inserter(store,config)for (line in parseCsv(csv)) { author = batch.createNode([name:line.author],Labels.Author) article= batch.createNode( [title:line.title, date:date],Labels.Article) batch.createRelationship(author,article, WROTE, NO_PROPS)}batch.createDeferredSchemaIndex(Labels.Article) .on("title").create()http://jexp.de/blog/2014/10/flexible-neo4j-batch-import-with-groovy
Convenient Object Graph Mapping
Spring Data Neo4j@NodeEntity class Person { @GraphId Long id; String name; @Relationship(type="LOVES") Person loved;}
interface PersonRepository extends GraphRepository<Person> { @Query("MATCH ... RETURN loved") Set<Person> findLoved(String person)}
https://neo4j.com/developer/spring-data-neo4j
Spring Data Neo4j@EnableNeo4jRepositories(basePackages="sample.repositories")public class MyNeo4jConfiguration extends Neo4jConfiguration {
@Bean public Neo4jServer neo4jServer() { return new RemoteServer(System.getenv("NEO4J_URL")); }
@Bean public SessionFactory getSessionFactory() { return new SessionFactory("sample.domain"); }}
Spring Data Neo4j
@RelationshipEntity(type="LOVES")class Love { @GraphId Long id; @StartNode Person lover; @EndNode Person loved; Date since;}
https://github.com/spring-projects/spring-boot/pull/3899
Demo(s)
The Sky is the Limit
Learn More
http://neo4j.com/developer/resources