Presentation is loading. Please wait.

Presentation is loading. Please wait.

NoSQL Slides by Roni Licher and Shoval Lagziel , Spring 2017

Similar presentations


Presentation on theme: "NoSQL Slides by Roni Licher and Shoval Lagziel , Spring 2017"— Presentation transcript:

1 NoSQL Slides by Roni Licher and Shoval Lagziel , Spring 2017
הדגמות במקביל להצגת שקופיות Slides by Roni Licher and Shoval Lagziel , Spring 2017 DBMS

2 מסד נתונים גרפי, לחשוב אחרת!
Connected information is everywhere in our world. Neo4j was build to efficiently store, handle and query highly connected elements in your data model. With a powerful and flexible data model you can represent your real-world, variably structured information without a loss of richness. The property graph model is easy to understand and handle, especially for object oriented and relational developers.

3 מסד נתונים גרפי, לחשוב אחרת!
Node- יש לו labelים ותכונות שהם key:value Edge- קשת, יכולה להיות מכוונת או לא יש לה סוג, ויכולה להכיל תכונות Connected information is everywhere in our world. Neo4j was build to efficiently store, handle and query highly connected elements in your data model. With a powerful and flexible data model you can represent your real-world, variably structured information without a loss of richness. The property graph model is easy to understand and handle, especially for object oriented and relational developers.

4 236363- DBMS, שפות שאילתא: Datalog
Neo4j מסד נתונים גרפי ממומש בJava ובScala Open –Source הפיתוח החל ב2003 נפוץ מאוד כיום מתאים מאוד כאשר המידע הוא מקושר DBMS, שפות שאילתא: Datalog

5 ebay

6 Neo4j and Cypher 4j Neo4j מסד הנתונים הגרפי שפת שאילתא עבור Neo4j
(במסד נתונים רלציוני יש לנו את SQL)

7 236363- DBMS, שפות שאילתא: Datalog
יצירת תוכן בCypher על ידי הפקודה CREATE יצירת Node עם property, והחזרה שלו CREATE (n { name: 'Andres' }) יצירת Node עם Label: CREATE (n:Person) לדוגמא יצירת Node עם label וproperties: CREATE (n:Person { name: 'Andres', title: 'Developer' }) כדי להחזיר את הNode שנוצר ניתן להוסיף RETURN n אביב 2016 DBMS, שפות שאילתא: Datalog

8 236363- DBMS, שפות שאילתא: Datalog
יצירת תוכן בCypher ניתן ליצור Nodes עם Relationships: CREATE (database:Database {name:"Neo4j"})-[r:SAYS]->(message:Message {name:"Hello World!"}) return database,message,r ניתן גם ליצור קשר בין שני Node שכבר קיימים, נראה זאת מאוחר יותר. אביב 2016 DBMS, שפות שאילתא: Datalog

9 מבנה שאילתא בCypher MATCH [Nodes and relationships] WHERE [Boolean filter statement] RETURN [DISTINCT] [statements [AS alias]] ORDER BY [Properties] [ASC\DESC] SKIP [Number] LIMIT [Number]

10 דוגמא לשאילתא שליפת כל הקודקודים מסוגDatabase עם השם Neo4j.
Type = Database Name = 'Neo4j' MATCH (a : Database) WHERE a.name = 'Neo4j' RETURN a

11 שליפת קשרים שליפת כל הקשרים מסוג SAYS שמחברים בין Database לבין Message. Type = Database Type = Message SAYS MATCH (a:Database)-[r:SAYS]->(b:Message) RETURN a,r,b

12 שימושים אפשריים בMatch
עבור Nodes: (a) (a:Ntype) (a { prop:’value’ } ) (a:Ntype { prop:’value’ } )

13 שימושים אפשריים בMatch
עבור Relationships: (a)--(b) (a)-->(b) (a)<--(b) (a)-->() (a)-[r]->(b) (a)-[:Rtype]->(b) (a)-[:R1|:R2]->(b) (a)-[r:Rtype]->(b) יכולים להיות גם יותר משני Nodes (a)-->(b)<--(c), (a)-->(b)-->(c) עבור מסלולים p = (a)-->(b) MATCH (a:Database)-->(b:Message) RETURN a,b MATCH (a:Database)--() RETURN a

14 שימושים אפשריים בMatch
עבור Relationships לפי מרחק: (a)-[:Rtype*2]->(b) – 2 hops of type Rtype. (a)-[:Rtype* ]->(b) – any number of hops of type Rtype. (a)-[:Rtype*2..10]-> (b) – 2-10 hops of Rtype. (a)-[:Rtype* ..10]-> (b) – 1-10 hops of Rtype. (a)-[:Rtype*2.. ]-> (b) – at least 2 hops of Rtype

15 יצירת קשר בין Nodeים קיימים
ניתן ליצור Relationship בין Nodeים שקיימים כבר: MATCH (a:Person),(b:Person) WHERE a.name = 'Node A' AND b.name = 'Node B' CREATE (a)-[r:RELTYPE]->(b) RETURN r MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r CREATE (a:Person {name:'Shoval'}), (b:Person {name:'Dvir'}), (c:Person {name:'Benny'}) return a,b,c MATCH (a:Person),(b:Person) WHERE a.name = 'Shoval' AND b.name = 'Dvir' CREATE (a)-[r:Workswith]->(b),(b)-[r2:Workswith]->(a) RETURN a,b MATCH (a:Person),(b:Person) WHERE a.name = 'Shoval' AND b.name = 'Benny' CREATE (a)-[r:Workswith]->(b),(b)-[r2:Workswith]->(a) RETURN a,b Match (a) return a Match (a:Person {name:'Dvir'})-[:Workswith]-(b) Return DISTINCT b Match (a:Person {name:'Dvir'})-[:Workswith*..2]-(b) Return DISTINCT b אביב 2016 DBMS, שפות שאילתא: Datalog

16 =,<>,<,>,>=,<=, =~ (Regex),
אופרטורים בCypher אופרטורים מתמטיים: +, -, *, /,%, ^ (power) השוואה: =,<>,<,>,>=,<=, =~ (Regex), IS NULL , IS NOT NULL אופרטורים לוגיים: AND, OR, XOR, NOT עבור מחרוזות: STARTS WITH, ENDS WITH, CONTAINS שרשור עם + עבור אוספים: IN לבדיקה האם איבר נמצא באוסף

17 WHERE WHERE others.name IN ['Andres', 'Peter']
WHERE user.age IN range (18,30) WHERE n.name =~ 'Tob.*‘ WHERE n.name =~ '(?i)ANDR.*‘ - (case insensitive) WHERE (n)-->() WHERE NOT (n)-->() WHERE exists(a.name) WHERE b.name? = 'Bob' (Returns all nodes where name = 'Bob' plus all nodes without a name property) Match (a) WHERE a.name=~'Sho[a-z]*' return a Match (a) WHERE exists(a.name) return a

18 פעולות על אוספים MATCH (user) RETURN count(user)
RETURN count(DISTINCT user.name) RETURN collect(user.name) Collection from the values, ignores NULL. RETURN avg(user.age) Average numerical values. Similar functions are sum, min, max. Match (a:Person{name:'Dvir'})-->() RETURN a, count(*) Match (a:Person{name:'Shoval'})-->()

19 פונקציות בCypher On paths: On collections:
MATCH shortestPath( (a)-[*]-(b) ) MATCH allShortestPath( (a)-[*]-(b) ) Length(path) – The path length or 0 if not exists. RETURN relationships(p) - Returns all relationships in a path. On collections: WHERE ANY (x IN a.array WHERE x = "one“ ) – at least one WHERE ALL (x IN nodes(p) WHERE x.age > 30) – all elements WHERE SINGLE (x IN nodes(p) WHERE var.eyes = "blue") – Only one * nodes(p) – nodes of the path p MATCH p=shortestPath( (a{name:'Dvir'})-[*]-(b{name:'Benny'}) ) RETURN length(p) On collections: RETURN a.array, filter(x IN a.array WHERE length(x)= 3) FILTER - returns the elements in a collection that comply to a predicate. WHERE ANY (x IN a.array WHERE x = "one“ ) – at least one WHERE ALL (x IN nodes(p) WHERE x.age > 30) – all elements WHERE SINGLE (x IN nodes(p) WHERE var.eyes = "blue") – Only one * nodes(p) – nodes of the path p

20 236363- DBMS, שפות שאילתא: Datalog
SET מאפשר לנו לשנות או לתת properties MATCH (n { name: 'Andres' }) SET n.position = 'Developer', n.surname = 'Taylor‘ מאפשר לנו להעתיק את כל התכונות מNode אחר לאחר MATCH (at { name: 'Andres' }),(pn { name: 'Peter' }) SET at = pn אביב 2016 DBMS, שפות שאילתא: Datalog

21 236363- DBMS, שפות שאילתא: Datalog
SET- המשך מאפשר לנו לתת labelים MATCH (n { name: 'Emil' }) SET n :Swedish:Israeli RETURN n אביב 2016 DBMS, שפות שאילתא: Datalog

22 With מאפשר לנו לבצע מניפולציה על המידע לפני שהוא עובר לביצוע בהמשך חלקי השאילתא שימושים: הגבלת מספר הרשומות שמועברת הלאה לMATCH אחרים – על ידי שימוש בORDER BY וLIMIT פילטור על ערכים מקובצים על ידי ביצועWhere לאחר מכן

23 With - דוגמא מה מוחזר משאילתא זו?
MATCH (david { name: "David" })--(otherPerson)-->() WITH otherPerson, count(*) AS foaf WHERE foaf > 1 RETURN otherPerson מה מוחזר משאילתא זו? האנשים שמחוברים לדוד שיש להם יותר מקשת אחת יוצאת Similar to SQL GROUP BY

24 236363- DBMS, שפות שאילתא: Datalog
DELETE ניתן למחוק Nodes/Relationships על ידי DELETE. MATCH (n:Person) DELETE n מחיקת Node עם הRelationships המחוברים אליו: MATCH (n { name: 'Andres' }) DETACH DELETE n באותו האופן ניתן למחוק גם Relationships על ידי שימוש מתאים בMATCH. אביב 2016 DBMS, שפות שאילתא: Datalog

25 Vintage tutorials questions
dname dcity pname pcity frequents drinker pub likes serves beer bname btype

26 Vintage tutorials questions
באלו ערים מגישים בירה שיוסי אוהב? MATCH (:drinker { dname: “Yossi" })-[:likes]->(:beer)<-[:serves]-(p :pub) RETURN p.pcity מי הם השתיינים שבכל פאב יש בירה שהם אוהבים? MATCH (p : pub) WITH collect(p) as Pubs MATCH (d : drinker) WHERE ALL (p in Pubs WHERE (p)-[:serves]->(:beer)<-[:likes]-(d) ) RETURN d drinker pub frequents serves beer likes dname dcity pcity pname btype bname

27 Exam questions - Win13-14 A MATCH (c:Course)
Studies: מחבר בין Student לביןCourse ומכיל את התכונותsemester ו-grade. Teaches: מחבר בין Lecturer לביןCourse ומכיל את התכונות semester ו-classroom. א. כתבו שאילתת Cypher שמחזירה: שמות כל הסטודנטים שלמדו את כל המקצועות. MATCH (c:Course) WITH collect(c) AS courses MATCH (s:Student) WHERE ALL (x in courses WHERE (s)-[:Studies]->(x)) RETURN s.name

28 Exam questions - Win13-14 A Studies: מחבר בין Student לביןCourse ומכיל את התכונותsemester ו-grade. Teaches: מחבר ביןLecturer לביןCourse ומכיל את התכונותsemester ו-classroom. ב. כתבו שאילתת Cypher שמחזירה: שמות כל הסטודנטים שלמדו קורס כלשהו שלמד גם סטודנט בשם "Roy" או שאת הקורס למד גם סטודנט כלשהו שלמד קורס כלשהו שלמד גם סטודנט ששמו “Roy”. MATCH (s:Student)-[:Studies*2..4]- (:Student{Name:"Roy"}) RETURN DISTINCT s.name

29 Exam questions - Win13-14 B Studies: מחבר בין Student לביןCourse ומכיל את התכונותsemester ו-grade. Teaches: מחבר ביןLecturer לביןCourse ומכיל את התכונותsemester ו-classroom. א. נגדיר פונקציית מרחק בין שני סטודנטים שונים כדלקמן: 1. הסטודנטיםA ו-B הם במרחק 1 אם הם למדו קורס משותף. 2. הסטודנטיםA ו-B הם במרחק n>1 אם n הוא המספר הקטן ביותר כך שקיים סטודנט C כך ש-A במרחק n-1 מ-C ו-C במרחק 1 מ-B. 3. אם לא קיים n כזה נגדיר את המרחק להיות 0. כתבו שאילתתCypher שמחזירה: המרחק בין שני סטודנטים שתעודת הזהות שלהם היא ו-67890 MATCH p=shortestPath((s1:Student {ID:'12345'})-[:Studies*]-(s2:Student {ID:'67890'})) RETURN length(p)/2

30 Exam questions - Win13-14 B Studies: מחבר בין Student לביןCourse ומכיל את התכונותsemester ו-grade. Teaches: מחבר ביןLecturer לביןCourse ומכיל את התכונותsemester ו-classroom. ב. שמות כל המרצים שלימדו לפחות 3 מקצועות. (ניתן להניח שאין כפילויות בגרף) MATCH (l:Lecturer)-[:Teaches]->(c:Course) WITH l, count(c) as numcourses WHERE numcourses >= 3 RETURN l.name

31 Learn more… Check Neo4j online version:

32 Learn more… Download Neo4j for free:

33 Learn more… Read the Neo4j manual: Cypher tutorials: More Neo4j developers tutorials:


Download ppt "NoSQL Slides by Roni Licher and Shoval Lagziel , Spring 2017"

Similar presentations


Ads by Google