Advertisement
Jakzon123

Mod 12 Advanced Graph Queries

May 3rd, 2023
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. # Basic Query
  2.  
  3. from neo4j import GraphDatabase
  4. import pandas as pd
  5. import numpy as np
  6.  
  7. ##Create local db with password "test"
  8.  
  9. uri = "neo4j://localhost:7687"
  10. driver = GraphDatabase.driver(uri,
  11.                               auth=("neo4j", "test"))
  12.  
  13. def query_graph(tx):
  14.  
  15.     queryGraph = ("MATCH\n" +
  16.                        "(c:Customer)\n" +
  17.                        " return DISTINCT c.ContactFirstname \n")
  18.     print(queryGraph)
  19.     results = tx.run(queryGraph)
  20.     #print("Name", "Phone")
  21.     for r in results:
  22.         print(r)
  23.         #print(r[0], r[1])
  24.  
  25.  
  26. with driver.session() as session:
  27.     session.execute_read(query_graph)
  28.  
  29. driver.close()
  30.  
  31. ##############################
  32.  
  33. # Query Node Relationships
  34.  
  35. from neo4j import GraphDatabase
  36. import pandas as pd
  37. import numpy as np
  38.  
  39. ##Create local db with password "test"
  40.  
  41. uri = "neo4j://localhost:7687"
  42. driver = GraphDatabase.driver(uri,
  43.                               auth=("neo4j", "test"))
  44.  
  45. def query_graph(tx):
  46.  
  47.     queryGraph = ("MATCH \n" +
  48.                   "(c:Customer:USA)- [r ] -> (p:Product) \n"+
  49.                     " return TYPE(r), r.YR, r.Quantity  order by r.YR, r.Quantity \n")
  50.     print(queryGraph)
  51.     results = tx.run(queryGraph)
  52.     print("Type", "Yr", "Qty")
  53.     for r in results:
  54.         print(r[0], r[1], r[2])
  55.  
  56.  
  57. with driver.session() as session:
  58.     session.execute_read(query_graph)
  59.  
  60. driver.close()
  61.  
  62. ##############################
  63.  
  64. # Query Number of Edges
  65.  
  66. from neo4j import GraphDatabase
  67. import pandas as pd
  68. import numpy as np
  69.  
  70. ##Create local db with password "test"
  71.  
  72. uri = "neo4j://localhost:7687"
  73. driver = GraphDatabase.driver(uri,
  74.                               auth=("neo4j", "test"))
  75.  
  76. def query_graph(tx):
  77.  
  78.     queryGraph = ("MATCH \n" +
  79.                   "(p:Product) ,(p)-[r]-(:Customer)\n"+
  80.                   "  Where p.ProductCode = 'S10_1678' \n"+
  81.                   " return count(r) \n")
  82.     print(queryGraph)
  83.     result = tx.run(queryGraph)
  84.     for r in result:
  85.         print("Num edges:", r[0])
  86.  
  87.  
  88.  
  89. with driver.session() as session:
  90.     session.execute_read(query_graph)
  91.  
  92. driver.close()
  93.  
  94. ##############################
  95.  
  96. # Query Aggregates
  97.  
  98. from neo4j import GraphDatabase
  99. import pandas as pd
  100. import numpy as np
  101.  
  102. ##Create local db with password "test"
  103.  
  104. uri = "neo4j://localhost:7687"
  105. driver = GraphDatabase.driver(uri,
  106.                               auth=("neo4j", "test"))
  107.  
  108. def query_graph(tx):
  109.  
  110.     queryGraph = ("MATCH \n" +
  111.                   "(c:Customer) -[r]-> (p:Product) \n"+
  112.                   "  Where p.ProductCode = 'S10_1678' \n"+
  113.                   " return sum(r.Quantity) \n")
  114.     print(queryGraph)
  115.     result = tx.run(queryGraph)
  116.     for r in result:
  117.         print("Avg Year of Orders that include S10_1678:", r[0])
  118.  
  119.  
  120. with driver.session() as session:
  121.     session.execute_read(query_graph)
  122.  
  123. driver.close()
  124.  
  125. ##############################
  126.  
  127. # Query Number ofEdges with...
  128.  
  129. from neo4j import GraphDatabase
  130. import pandas as pd
  131. import numpy as np
  132.  
  133. ##Create local db with password "test"
  134.  
  135. uri = "neo4j://localhost:7687"
  136. driver = GraphDatabase.driver(uri,
  137.                               auth=("neo4j", "test"))
  138.  
  139. def query_graph(tx):
  140.  
  141.     queryGraph = ("MATCH \n" +
  142.                   "(c:Customer) - [r] ->(p:Product) \n"+
  143.                   "  Where p.ProductCode = 'S10_1678' \n"+
  144.                   "  and r.YR = 2004 \n"+
  145.                   " return count(r) \n")
  146.     print(queryGraph)
  147.     result = tx.run(queryGraph)
  148.     for r in result:
  149.         print("Num of orders that included S10_1678 in 2004:", r[0])
  150.  
  151.  
  152. with driver.session() as session:
  153.     session.execute_read(query_graph)
  154.  
  155. driver.close()
  156.  
  157. ##############################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement