Publicité

Neo4 j

Assistant Professor (S.Gr) à PSG College of Technology
6 Feb 2020
Publicité

Contenu connexe

Publicité
Publicité

Neo4 j

  1. NEO4J Prepared By, Dr.Saranya.K.G, Assistant Professor(S.Gr), Department of CSE, PSG College of Technology, Coimbatore.
  2. Neo4j - Building Blocks Neo4j Graph Database has the following building blocks − Nodes Properties Relationships Labels Data Browser
  3. STUDENT Student_ID DOB DoorNo Street City State Course_ID Mark Rank TEACHER Teacher_ID Teacher_name Course_ID COURSE Course_ID Course_Name Batch STUD_HOBBY Student_ID Hobby Studying Teaches Has_a_hobby Example
  4. Node • Node is a fundamental unit of a Graph. It contains properties with key-value pairs as shown in the following image.
  5. Properties • Property is a key-value pair to describe Graph Nodes and Relationships. Key = Value Key is a String and Value may be represented using any Neo4j Data types.
  6. Relationships • Relationships are another major building block of a Graph Database. It connects two nodes as depicted in the following figure. Here, Emp and Dept are two different nodes. "WORKS_FOR" is a relationship between Emp and Dept nodes. Here, "Emp" is a start node, and "Dept" is an end node. As this relationship arrow mark represents a relationship from "Emp" node to "Dept" node, this relationship is known as an "Incoming Relationship" to "Dept" Node and "Outgoing Relationship" to "Emp" node.
  7. Like nodes, relationships also can contain properties as key-value pairs. Here, "WORKS_FOR" relationship has one property as key-value pair. Id = 123 It represents an Id of this relationship.
  8. Labels • Label associates a common name to a set of nodes or relationships. A node or relationship can contain one or more labels. We can create new labels to existing nodes or relationships. We can remove the existing labels from the existing nodes or relationships.
  9. Neo4j CQL - Introduction Neo4j CQL • Is a query language for Neo4j Graph Database. • Is a declarative pattern-matching language. • Follows SQL like syntax. • Syntax is very simple and in human readable format.
  10. Neo4j CQL Clauses Sr.No Read Clauses Usage 1 MATCH This clause is used to search the data with a specified pattern. 2 OPTIONA L MATCH This is the same as match, the only difference being it can use nulls in case of missing parts of the pattern. 3 WHERE This clause id is used to add contents to the CQL queries. 4 START This clause is used to find the starting points through the legacy indexes. 5 LOAD CSV This clause is used to import data from CSV files.
  11. Sr.No Write Clause Usage 1 CREATE This clause is used to create nodes, relationships, and properties. 2 MERGE This clause verifies whether the specified pattern exists in the graph. If not, it creates the pattern. 3 SET This clause is used to update labels on nodes, properties on nodes and relationships. 4 DELETE This clause is used to delete nodes and relationships or paths etc. from the graph. 5 REMOVE This clause is used to remove properties and elements from nodes and relationships. 6 FOREACH This class is used to update the data within a list. 7 CREATE UNIQUE Using the clauses CREATE and MATCH, you can get a unique pattern by matching the existing pattern and creating the missing one. 8 Importing CSV files with Cypher Using Load CSV you can import data from .csv files.
  12. Sr.No General Clauses Usage 1 RETURN This clause is used to define what to include in the query result set. 2 ORDER BY This clause is used to arrange the output of a query in order. It is used along with the clauses RETURN or WITH. 3 LIMIT This clause is used to limit the rows in the result to a specific value. 4 SKIP This clause is used to define from which row to start including the rows in the output. 5 WITH This clause is used to chain the query parts together. 6 UNWIND This clause is used to expand a list into a sequence of rows. 7 UNION This clause is used to combine the result of multiple queries. 8 CALL This clause is used to invoke a procedure deployed in the database.
  13. Neo4j CQL Functions Sr.No CQL Functions Usage 1 String They are used to work with String literals. 2 Aggregation They are used to perform some aggregation operations on CQL Query results. 3 Relationship They are used to get details of relationships such as startnode, endnode, etc.
  14. Neo4j CQL Data Types Sr.No CQL Data Type Usage 1 Boolean It is used to represent Boolean literals: true, false. 2 byte It is used to represent 8-bit integers. 3 short It is used to represent 16-bit integers. 4 int It is used to represent 32-bit integers. 5 long It is used to represent 64-bit integers. 6 float It is used to represent 32-bit floating-point numbers. 7 double It is used to represent 64-bit floating-point numbers. 8 char It is used to represent 16-bit characters. 9 String It is used to represent Strings.
  15. CQL Operators Sr.No Type Operators 1 Mathematical +, -, *, /, %, ^ 2 Comparison +, <>, <, >, <=, >= 3 Boolean AND, OR, XOR, NOT 4 String + 5 List +, IN, [X], [X…..Y] 6 Regular Expression =- 7 String matching STARTS WITH, ENDS WITH, CONSTRAINTS
  16. Boolean Operators in Neo4j CQL Sr.No Boolean Operators Description 1 AND It is a Neo4j CQL keyword to support AND operation. It is like SQL AND operator. 2 OR It is a Neo4j CQL keyword to support OR operation. It is like SQL AND operator. 3 NOT It is a Neo4j CQL keyword to support NOT operation. It is like SQL AND operator. 4 XOR It is a Neo4j CQL keyword to support XOR operation. It is like SQL AND operator.
  17. Comparison Operators in Neo4j CQL Sr.No Boolean Operator s Description 1 = It is a Neo4j CQL "Equal To" operator. 2 < > It is a Neo4j CQL "Not Equal To" operator. 3 < It is a Neo4j CQL "Less Than" operator. 4 > It is a Neo4j CQL "Greater Than" operator. 5 <= It is a Neo4j CQL "Less Than Or Equal To" operator. 6 > = It is a Neo4j CQL "Greater Than Or Equal To" operator.
  18. Neo4j CQL - Creating Nodes Creating a Single node CREATE (node_name); Creating Multiple Nodes CREATE (node1),(node2) Creating a Node with a Label CREATE (node:label)
  19. Creating a Node with Multiple Labels CREATE (node:label1:label2:. . . . labeln) Create Node with Properties CREATE (node:label { key1: value, key2: value, . . . . . . . . . }) Returning the Created Node CREATE (Node:Label{properties. . . . }) RETURN Node
  20. Neo4j CQL - Creating a Relationship Creating Relationships CREATE (node1)-[:RelationshipType]->(node2) create (Akshaya:student {name:"K.Akshaya", ID:101, DOB:"21.05.1997", DoorNo: 38, street:"Pound Strret", city:"coimbatore", state:"TamilNadu", courseID: "18ZS52"}) create (ID:stud_hobby {student_ID:101,hobbyname:"Reading"}) create (Akshaya) - [r:has_a_Hobby] ->(ID)
  21. Creating a Relationship Between the Existing Nodes MATCH (a:LabeofNode1), (b:LabeofNode2) WHERE a.name = "nameofnode1" AND b.name = " nameofnode2" CREATE (a)-[: Relation]->(b) RETURN a,b MATCH (a:student), (b:stud_hobby) where a.name="K.Akshaya" AND b.hobbyname="Reading" create (a) -[r:has_a_Hobby] -> (b) RETURN a,b
  22. Creating a Relationship with Label and Properties CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2) MATCH (a:student), (b:stud_hobby) where a.name="K.Akshaya" AND b.hobbyname="Reading" create (a)- [r:has_a_Hobby{book:"Story Book", writtenby: "henry"}] ->(b) RETURN a,b
  23. Creating a Complete Path CREATE p = (Node1 {properties})-[:Relationship_Type]-> (Node2 {properties})[:Relationship_Type]->(Node3 {properties}) RETURN p create p = (Akshaya { name: "K.Akshaya"}) - [:has_a_Hobby] -> (ID {name:"Reading"}) - [:studying] -> (cou {name:"DIC LAB"}) RETURN p
  24. Neo4j CQL Write Clauses Neo4j - Merge Command Merge a node with label MERGE (node: label {properties . . . . . . . }) MERGE (Ramya:student) return Ramya MERGE command is a combination of CREATE command and MATCH command. Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists, then it returns the results.
  25. Merging a Node with a Label MERGE (node:label) RETURN node Merging a Node with Properties MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . }) MERGE (Sowmya:student {name: "S.Sowmya", ID:102, DOB: "12.06.1986", DoorNo:152, street:"VOCStreet", city:"Coimbatore", state:"TamilNadu", course_ID:"18ZS52"}) RETURN Sowmya
  26. OnCreate and OnMatch MERGE (node:label {properties . . . . . . . . . . .}) ON CREATE SET property.isCreated ="true" ON MATCH SET property.isFound ="true" MERGE (Sowmya:student {name: "S.Sowmya", ID:102, DOB: "12.06.1986", DoorNo:152, street:"VOCStreet", city:"Coimbatore", state:"TamilNadu", course_ID:"18ZS52"}) ON CREATE SET Sowmya.isCreated="true" ON MATCH SET Sowmya.isFound="true" RETURN Sowmya Using on create and on match, you can set properties for indicating whether the node is created or matched.
  27. Merge a Relationship MATCH (a:Teacher), (c:course) where a.name="Lakshmi" AND c.name="DIC LAB" MERGE (a) - [r:teaches] -> (c) RETURN a, c
  28. Neo4j - Set Clause Setting a Property MATCH (node:label{properties . . . . . . . . . . . . . . }) SET node.property = value RETURN node create (Akshaya:student {name:"K.Akshaya", ID:101, DOB:"21.05.1997", DoorNo: 38, street:"Pound Strret", city:"coimbatore", state:"TamilNadu", courseID: "18ZS52"}) RETURN Akshaya
  29. Removing a Property MATCH (node:label {properties}) SET node.property = NULL RETURN node match (Akshaya:student {name:"K.Akshaya", ID:101, DOB:"21.05.1997", DoorNo: 38, street:"Pound Strret", city:"coimbatore", state:"TamilNadu", courseID: "18ZS52"}) SET Akshaya. mark= NULL RETURN Akshaya
  30. Setting Multiple Properties MATCH (node:label {properties}) SET node.property1 = value, node.property2 = value RETURN node MATCH (Sowmya:student {name: "S.Sowmya", ID:102, DOB: "12.06.1986", DoorNo:152, street:"VOCStreet", city:"Coimbatore", state:"TamilNadu", course_ID:"18ZS52"}) SET Sowmya.pob="salem", Sowmya.mark= 92
  31. Setting a Label on a Node MATCH (n {properties . . . . . . . }) SET n :label RETURN n CREATE (Krishnan {name:"R.Krishnan", ID:"C1001", Course_ID:"18ZS52"}) RETURN Krishnan MATCH (Krishnan {name:"R.Krishnan", ID:"C1001", Course_ID:"18ZS52"}) SET Krishnan:Teacher Return Krishnan
  32. Setting Multiple Labels on a Node MATCH (n {properties . . . . . . . }) SET n :label1:label2 RETURN n create (Seetha:Teacher {name:"R.Seetha", ID:"C1002", Course_ID: "18ZS59"}) RETURN Seetha create (Seetha:Teacher {name:"R.Seetha", ID:"C1002", Course_ID: "18ZS59"}) SET Seetha:person:female RETURN Seetha
  33. Neo4j - Delete Clause Deleting All Nodes and Relationships MATCH (n) DETACH DELETE n MATCH (n) DETACH DELETE n
  34. MATCH (node:label {properties . . . . . . . . . . }) DETACH DELETE node CREATE (Krishnan {name:"R.Krishnan", ID:"C1001", Course_ID:"18ZS52"}) DETACH DELETE Krishnan
  35. Neo4j - Remove Clause Removing a Property MATCH (node:label{properties . . . . . . . }) REMOVE node.property RETURN node CREATE (anand:Teacher {name:"K.Anand", ID:"C1004", Course_ID:"18ZS52"}) MATCH (anand:Teacher {name:"K.Anand",ID: "C1004", Course_ID:"18ZS52"}) REMOVE anand.ID RETURN anand
  36. Removing a Label From a Node MATCH (node:label {properties . . . . . . . . . . . }) REMOVE node:label RETURN node MATCH (anand:Teacher {name:"K.Anand",Course_ID:"18ZS52"}) REMOVE anand:Teacher RETURN anand
  37. Removing Multiple Labels MATCH (node:label1:label2 {properties . . . . . . . . }) REMOVE node:label1:label2 RETURN node CREATE (banu:Teacher:person {name:"R.banu", ID:"C1006", Course_ID:"18ZS52"}) CREATE (banu:Teacher:person {name:"R.banu", ID:"C1006", Course_ID:"18ZS52"}) REMOVE banu:Teacher:person RETURN banu
  38. Neo4j - Foreach Clause MATCH p = (start node)-[*]->(end node) WHERE start.node = "node_name" AND end.node = "node_name" FOREACH (n IN nodes(p)| SET n.marked = TRUE) CREATE p =(Akshaya {name:"K.Akshaya"}) - [:TOPSCORRER_OF] -> (COU {name:"DICLAB"}) - [:has_a_Hobby] -> (ID{name:"Reading"}) Return p MATCH p=(Akshaya) -[*] -> (ID) where Akshaya.name="K.Akshaya" AND ID.name="Reading" FOREACH (n IN nodes(p) | SET n.marked = TRUE) The FOREACH clause is used to update data within a path.
  39. Neo4j CQL Read Clause Get All Nodes Using Match MATCH (n) RETURN n
  40. Getting All Nodes Under a Specific Label MATCH (node:label) RETURN node
  41. Match by Relationship MATCH (node:label)<-[: Relationship]-(n) RETURN n
  42. Delete All Nodes MATCH (n) detach delete n
  43. Neo4j - Optional Match Clause MATCH (node:label {properties. . . . . . . . . . . . . .}) OPTIONAL MATCH (node)-->(x) RETURN x
  44. Neo4j - Where Clause MATCH (label) WHERE label.node = "property" RETURN label MATCH (Student) WHERE Student.Akshaya = “ID:101” RETURN Student
  45. Neo4j - Count Function MATCH (n { name: 'A' })-->(x) RETURN n, count(*) The count() function is used to count the number of rows MATCH (n {Course_ID : "18ZS52", Coursename:"DIC LAB"}) -- (x) RETURN n, count(*)
  46. Group Count The COUNT clause is also used to count the groups of relationship types. Match(n{name: "India", result: "Winners"})-[r]-(x) RETURN type (r), count(*) MATCH (n {Course_ID : "18ZS52", Coursename:"DIC LAB"}) - [r] -(x) RETURN type (r), count(*)
  47. Neo4j CQL General Clauses Neo4j - Return Clause Create (node:label {properties}) RETURN node
  48. Returning Multiple Nodes create (Rahul:student {name:"V.Rahul", ID:105, DOB:"10.05.1997", DoorNo: 242, street:"VOC Strret", city:"coimbatore", state:"TamilNadu", courseID: "18ZS52", mark:82, rank:6}) CREATE (id:stud_hobby {student_ID:105, Hobby:"Reading"}) CREATE (COU:Course {Course_ID:"18ZS52", Coursename:"DIC LAB"}) CREATE (ID) - [r1:has_a_hobby{hobby:"Reading", Book:"CSE Books", writtenby: "Henry"}] -> (COU) CREATE (Rahul) - [r2:studying] -> (ID) RETURN Rahul,id
  49. Returning Relationships CREATE (node1)-[Relationship:Relationship_type]->(node2) RETURN Relationship
  50. Returning Properties Match (node:label {properties . . . . . . . . . . }) Return node.property
  51. Returning All Elements Match p = (Anitha {name: “M.Anitha", ID:103})-[r]-(x) RETURN *
  52. Neo4j - Order By Clause MATCH (n) RETURN n.property1, n.property2 . . . . . . . . ORDER BY n.property MATCH (n) RETURN n.name, n.mark order by n.mark MATCH (n) RETURN n.name, n.rank order by n.rank
  53. Ordering Nodes by Multiple Properties MATCH (n) RETURN n ORDER BY n.age, n.name MATCH (n) RETURN n.name, n.mark, n,rank ORDER BY n.name, n.mark
  54. Ordering Nodes by Descending Order MATCH (n) RETURN n ORDER BY n.name DESC MATCH (n) RETURN n.name, n.rank order by n.rank DESC
  55. Neo4j - Limit Clause MATCH (n) RETURN n ORDER BY n.name LIMIT 3 MATCH (n) RETURN n.name, n.rank order by n.rank ASC LIMIT 2
  56. Neo4j - Skip Clause MATCH (n) RETURN n.name, n.runs ORDER BY n.runs DESC SKIP 3 MATCH (n) RETURN n.name, n.rank order by n.rank ASC SKIP 3
  57. Neo4j - With Clause MATCH (n) WITH n ORDER BY n.property RETURN collect(n.property) MATCH (n) WITH n ORDER BY n.name DESC LIMIT 3 RETURN collect(n.name) MATCH (n) WITH n order by n.rank ASC LIMIT 2 RETURN collect (n.name)
  58. Neo4j - String Functions Neo4j - Upper Function UPPER (<input-string>) MATCH (n:student) RETURN UPPER(n.name), n.street
  59. Neo4j - Lower Function LOWER (<input-string>) MATCH (n:student) RETURN LOWER (n.name), n.street
  60. Neo4j - Aggregation Function Neo4j - Count Function COUNT(<value>) MATCH (n:student) WHERE n.mark>85 RETURN COUNT(n)
  61. Neo4j - Max Function MAX(<property-name>) MATCH (n:student) RETURN MAX (n.mark)
  62. Neo4j - Min Function MIN(<property-name>) MATCH (n:student) RETURN MIN (n.mark)
  63. Neo4j - Sum Function SUM(<property-name>) MATCH (n:student) RETURN SUM (n.mark)
  64. Neo4j - Avg Function AVG(<property-name> ) MATCH (n:student) RETURN AVG (n.mark)
Publicité