Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Basic Query
- from neo4j import GraphDatabase
- import pandas as pd
- import numpy as np
- ##Create local db with password "test"
- uri = "neo4j://localhost:7687"
- driver = GraphDatabase.driver(uri,
- auth=("neo4j", "test"))
- def query_graph(tx):
- queryGraph = ("MATCH\n" +
- "(c:Customer)\n" +
- " return DISTINCT c.ContactFirstname \n")
- print(queryGraph)
- results = tx.run(queryGraph)
- #print("Name", "Phone")
- for r in results:
- print(r)
- #print(r[0], r[1])
- with driver.session() as session:
- session.execute_read(query_graph)
- driver.close()
- ##############################
- # Query Node Relationships
- from neo4j import GraphDatabase
- import pandas as pd
- import numpy as np
- ##Create local db with password "test"
- uri = "neo4j://localhost:7687"
- driver = GraphDatabase.driver(uri,
- auth=("neo4j", "test"))
- def query_graph(tx):
- queryGraph = ("MATCH \n" +
- "(c:Customer:USA)- [r ] -> (p:Product) \n"+
- " return TYPE(r), r.YR, r.Quantity order by r.YR, r.Quantity \n")
- print(queryGraph)
- results = tx.run(queryGraph)
- print("Type", "Yr", "Qty")
- for r in results:
- print(r[0], r[1], r[2])
- with driver.session() as session:
- session.execute_read(query_graph)
- driver.close()
- ##############################
- # Query Number of Edges
- from neo4j import GraphDatabase
- import pandas as pd
- import numpy as np
- ##Create local db with password "test"
- uri = "neo4j://localhost:7687"
- driver = GraphDatabase.driver(uri,
- auth=("neo4j", "test"))
- def query_graph(tx):
- queryGraph = ("MATCH \n" +
- "(p:Product) ,(p)-[r]-(:Customer)\n"+
- " Where p.ProductCode = 'S10_1678' \n"+
- " return count(r) \n")
- print(queryGraph)
- result = tx.run(queryGraph)
- for r in result:
- print("Num edges:", r[0])
- with driver.session() as session:
- session.execute_read(query_graph)
- driver.close()
- ##############################
- # Query Aggregates
- from neo4j import GraphDatabase
- import pandas as pd
- import numpy as np
- ##Create local db with password "test"
- uri = "neo4j://localhost:7687"
- driver = GraphDatabase.driver(uri,
- auth=("neo4j", "test"))
- def query_graph(tx):
- queryGraph = ("MATCH \n" +
- "(c:Customer) -[r]-> (p:Product) \n"+
- " Where p.ProductCode = 'S10_1678' \n"+
- " return sum(r.Quantity) \n")
- print(queryGraph)
- result = tx.run(queryGraph)
- for r in result:
- print("Avg Year of Orders that include S10_1678:", r[0])
- with driver.session() as session:
- session.execute_read(query_graph)
- driver.close()
- ##############################
- # Query Number ofEdges with...
- from neo4j import GraphDatabase
- import pandas as pd
- import numpy as np
- ##Create local db with password "test"
- uri = "neo4j://localhost:7687"
- driver = GraphDatabase.driver(uri,
- auth=("neo4j", "test"))
- def query_graph(tx):
- queryGraph = ("MATCH \n" +
- "(c:Customer) - [r] ->(p:Product) \n"+
- " Where p.ProductCode = 'S10_1678' \n"+
- " and r.YR = 2004 \n"+
- " return count(r) \n")
- print(queryGraph)
- result = tx.run(queryGraph)
- for r in result:
- print("Num of orders that included S10_1678 in 2004:", r[0])
- with driver.session() as session:
- session.execute_read(query_graph)
- driver.close()
- ##############################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement