Skip to content

Instantly share code, notes, and snippets.

@ReedJessen
Created December 30, 2016 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReedJessen/a39151bb1f47b19cf1b37bad6f7e42a7 to your computer and use it in GitHub Desktop.
Save ReedJessen/a39151bb1f47b19cf1b37bad6f7e42a7 to your computer and use it in GitHub Desktop.
Methods for writing relationships between two nodes in Neo4j
class Neo4jWriter():
    def __init__(self):
        config = configparser.ConfigParser()
        config.read('config.ini')

        user_name = config.get('neo4j credentials', 'user_name')
        password = config.get('neo4j credentials', 'password')
        bolt_host = config.get('neo4j credentials', 'bolt_host')

        self.driver = GraphDatabase.driver(bolt_host,
                              auth=basic_auth( user_name, password))
                              
   def write_person_to_patent(self, Person_Node, Patent_Node):
        print('Creating Invention Relationship:' + str(Person_Node.__dict__))
        with self.driver.session() as session:
            with session.begin_transaction() as write_tx:
                query = "MATCH(a:Person {full_name: \'" + str(Person_Node.full_name) + "\'}), (b:Patent {grant_number: \'" + str(
                    Patent_Node.grant_number) + "\'}) MERGE(a)-[:Invented {priority_date: \'" + Patent_Node.application_date + "\'}]->(b)"
                # print(query)
                write_tx.run(query)
                write_tx.success = True

    def write_company_to_patent(self, Company_Node, Patent_Node):
        print('Creating Ownership Relationship:' + str(Person.__dict__))
        with self.driver.session() as session:
            with session.begin_transaction() as write_tx:
                query = "MATCH(a:Company {full_name: \'" + str(
                    Company_Node.full_name) + "\'}), (b:Patent {grant_number: \'" + str(
                    Patent_Node.grant_number) + "\'}) MERGE(a)-[:Owns]->(b)"
                # print(query)
                write_tx.run(query)
                write_tx.success = True
                ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment