Guest User

Untitled

a guest
Feb 4th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import pandas as pd
  2. from neo4j import GraphDatabase
  3. from typing import List
  4.  
  5. class GraphBuilder(object):
  6.  
  7. def __init__(self,
  8. uri: str = "bolt://localhost:7687",
  9. user: str = "neo4j",
  10. password: str = "neo4j1"):
  11. """
  12. Args:
  13. uri: uri to access the Bolt DirectDriver.
  14. user: neo4j user.
  15. password: neo4j password.
  16. """
  17. self._driver = GraphDatabase.driver(uri, auth=(user, password))
  18.  
  19. def close(self) -> None:
  20. self._driver.close()
  21.  
  22. def create_nodes(self,
  23. df : pd.DataFrame,
  24. node_props: List[str],
  25. label: str) -> None:
  26. """Creates nodes with a fixed set properties based on the dataframe provided.
  27.  
  28. Args:
  29. df: Dataframe in which each column and each row corresponds to a different node.
  30. node_props: names of the properties of each node.
  31. label: neo4j label for each node created.
  32.  
  33. """
  34. cypher_statement = f"CREATE (new_node: {label})"
  35. for prop in node_props:
  36. cypher_statement += f"\n SET new_node.{prop} = ${prop}"
  37.  
  38. with self._driver.session() as session:
  39. props_dict = {}
  40. for index, row in df.iterrows():
  41. for prop in node_props:
  42. props_dict[prop] = row[prop]
  43. session.run(
  44. cypher_statement,
  45. props_dict)
  46.  
  47. def create_edges(self,
  48. df: pd.DataFrame,
  49. predecessor_prop: str,
  50. predecessor_label: str,
  51. successor_prop: str,
  52. successor_label: str,
  53. edge_props: List[str],
  54. edge_label: str) -> None:
  55. """Creates edges with a fixed set properties based on the dataframe provided.
  56.  
  57. Args:
  58. df: Dataframe in which each column and each row corresponds to a different node.
  59. predecessor_prop: name of the matching property in the source node of the edge.
  60. predecessor_label: label of the source node of the edge.
  61. sucessor_prop: name of the matching property in the target node of the edge.
  62. sucessor_label: label of the target node of the edge.
  63. edge_props: names of the properties of each edge.
  64. edge_label: neo4j label for each edge created.
  65.  
  66. """
  67. cypher_statement = f"""
  68. MATCH
  69. (predecessor_node: {predecessor_label} {{ id: ${predecessor_prop} }}),
  70. (successor_node: {successor_label} {{ id: ${successor_prop} }})
  71. CREATE (predecessor_node)-[new_edge: {edge_label}]->(successor_node)"""
  72. for prop in edge_props:
  73. cypher_statement += f"\n SET new_edge.{prop} = ${prop}"
  74.  
  75. with self._driver.session() as session:
  76. props_dict = {}
  77. for index, row in df.iterrows():
  78. for prop in edge_props + [successor_prop, predecessor_prop]:
  79. props_dict[prop] = row[prop]
  80. session.run(
  81. cypher_statement,
  82. props_dict)
Add Comment
Please, Sign In to add comment