def import2neo(nodes): graph = Graph(password="******") # Greate Node print('Write nodes to Neo4j...\n') for node in tqdm(nodes): if not graph.data('MATCH (n{url:"%s"}) RETURN n' % node['url']): graph.data(make_CREATE(node))
def test_neo4j_remote(self, mock_get): from py2neo.database.status import GraphError from py2neo import Graph test_context = 'PYBEL_TEST_CTX' neo_path = os.environ['NEO_PATH'] try: neo = Graph(neo_path) neo.data( 'match (n)-[r]->() where r.{}="{}" detach delete n'.format( PYBEL_CONTEXT_TAG, test_context)) except GraphError: self.skipTest("Can't query Neo4J ") except Exception: self.skipTest("Can't connect to Neo4J server") else: with self.runner.isolated_filesystem(): args = [ 'convert', '--path', test_bel_simple, '--connection', self.connection, '--neo', neo_path, '--neo-context', test_context ] self.runner.invoke(cli.main, args) q = 'match (n)-[r]->() where r.{}="{}" return count(n) as count'.format( PYBEL_CONTEXT_TAG, test_context) count = neo.data(q)[0]['count'] self.assertEqual(14, count)
def access_graphdb(): #Verifies that an existing Neo4j database exists and that it is #populated. Does not modify the database. if not is_service_running('neo4j'): print("Starting neo4j service....") os.system("sudo service neo4j start") time.sleep(5) authenticate("localhost:7474", "neo4j", "pining") try: #Just access graph and retrieve stats g = Graph("http://localhost:7474/db/data/") interactions = (g.data("MATCH p=()-[r:interacts_with]->() RETURN p")) member_ofs = (g.data("MATCH p=()-[r:member_of]->() RETURN p")) if not interactions or not member_ofs: sys.exit("Graph is empty or is missing expected relationships. " "Exiting.") else: print( "Graph contains %s protein interactions and %s OG memberships." % (len(interactions), len(member_ofs))) except (py2neo.packages.httpstream.http.SocketError, py2neo.database.status.Unauthorized) as e: print("**Error accessing the Neo4j database: %s" % e) print("**Please try accessing the server at http://localhost:7474/") sys.exit()
class Neo4jClient(object): def __init__(self, user='******', password='******', host='localhost', port=7687): # self.db = Database("bolt://camelot.example.com:7687") self.graph = Graph(bolt=True, host=host, bolt_port=port, user=user, password=password) self.selector = NodeSelector(self.graph) def get_graph(self, cypher): data = self.graph.data(cypher) return list(data) def get_data_frame(self, cypher): data = self.graph.data(cypher) return DataFrame(data) def select(self, labels, props): data = self.selector.select(labels, props) return list(data) def select_where(self, label, constraint): selected = self.selector.select(label).where(constraint) return list(selected) def cypher_execute(self, cypher): result = self.graph.cypher.execute(cypher) return result
class DatabaseDriver(object): """ Class connecting to the Neo4J database """ def __init__(self, host: str, usrname: str, passwd: str, port: int): """ CONSTRUCTOR :param host: The IP address of the host where the Neo4J server runs on :param port: The port we're connecting to :param usrname: The username used for authentication - None if no authentication required :param passwd: The password used for authentication - None if no authentication required """ self._graph = Graph(usrname=usrname, password=passwd, host=host, http_port=port) self._host = host self._port = port def execute_query(self, query: str): """ :param query: The query we want to execute :return: The query results, as a list """ results = list(self._graph.data(query)) return results def connection_active(self): """ Method that checks if the connection to the database is still active or not :return: True - if the connection is active False - otherwise """ if self._graph is None: return False try: a = list(self._graph.data('match (n) return n limit 1')) if len(a) == 1: return True else: return False except: return False def __str__(self): return "Neo4J connection to host {:s} on port {:d}".format( self._host, self._port)
class NeoConn(object): """Connect neo4j""" def __init__(self): self._graph = Graph(host=settings.NEO4J_HOST, http_port=settings.NEO4J_PORT, user=settings.NEO4J_USERNAME, password=settings.NEO4J_PASSWORD) def read_data(self, cql, *data_list): """读取数据""" return self._graph.data(cql, data_list) def read_df_data(self, cql, *data_list): """读取dataframe""" df = DataFrame(self._graph.data(cql, data_list)) return df
def connect_entities(rel_file): authenticate("localhost:7474", "neo4j", "admin") graph = Graph("http://localhost:7474/db/data/") with open(rel_file, encoding='utf-8') as f: lines = [line for line in f] for line in tqdm_notebook(lines): line = unicodedata.normalize('NFKC', line) line = line.replace('\\', '\\\\').replace('"', '\\"') h, r, t = line[:-1].split("\t") if not graph.data( 'MATCH (h{`uid`:"%s"})-[r:%s]->(t{`uid`:"%s"}) RETURN r' % (h, r, t)): graph.data( 'MATCH (h{`uid`:"%s"}),(t{`uid`:"%s"}) CREATE (h)-[r:%s]->(t)' % (h, t, r))
def connect_nodes(nodes): graph = Graph(password='******') print('Connect nodes to Neo4j...\n') for node in tqdm(nodes): rels = node['rels'] for rel, tail_entity_link in rels: link = 'http://baike.baidu.com' + tail_entity_link.attrs['href'] tail_entity = graph.data('MATCH (n{url:"%s"})RETURN n' % link) if tail_entity: if not graph.data( 'MATCH (h{url:"%s"})-[r:%s]->(t{url:"%s"}) RETURN r' % (node['url'], rel, link)): graph.data( 'MATCH (h{url:"%s"}),(t{url:"%s"}) CREATE (h)-[r:%s]->(t)' % (node['url'], link, rel))
class Graph_DB(): graph = None def __init__(self): pass def getInstance(self): if (self.graph is None): PORT = 9000 BOLT_PORT = 9001 PASSWORD = "******" USERNAME = "******" HOST = "localhost" DBPATH = "/db/data" #authenticate("{host}:{port}".format(host=HOST,port=PORT), USERNAME, PASSWORD) self.graph = Graph(host=HOST,http_port=9000,bolt_port=9001,bolt=False) print("Graph --------") print(self.graph.data("match (n) where n.name STARTS WITH 'To' return n")) return self.graph
class EmailAddress_Handler(Logger): def __init__(self, output_path, config_file='perspectives/EMAIL.yaml', neo4j_connection_string="bolt://localhost:7687", neo4j_user="******", neo4j_password="******"): super(self.__class__, self).__init__(self.__class__.__name__) self.graph = Graph(neo4j_connection_string, user=neo4j_user, password=neo4j_password) self.output_path = os.path.abspath(output_path) if config_file: with open(config_file, 'r') as f: try: self.conf = yaml.load(f) except Exception as e: raise e sys.exit(1) @property def all_emails(self): query = "MATCH (d:EmailAddress) RETURN DISTINCT(d.name) as email_address" return [x['email_address'] for x in self.graph.data(query)] def process(self, verbose): if verbose: for i in ['Worker']: logging.getLogger(i).setLevel(logging.INFO) for email_address in self.all_emails: d = EmailAddress(email_address, self.graph, self.output_path, self.conf['tabs'])
class Header_Handler(Logger): def __init__(self, output_path, config_file='perspectives/HEADER.yaml', neo4j_connection_string="bolt://localhost:7687", neo4j_user="******", neo4j_password="******"): super(self.__class__, self).__init__(self.__class__.__name__) self.graph = Graph(neo4j_connection_string, user=neo4j_user, password=neo4j_password) self.output_path = os.path.abspath(output_path) if config_file: with open(config_file, 'r') as f: try: self.conf = yaml.load(f) except Exception as e: raise e sys.exit(1) @property def all_message_id(self): query = "MATCH (m:Message) RETURN DISTINCT(m.message_id) as message_id" return [x['message_id'] for x in self.graph.data(query)] def process(self, verbose, transport): if verbose: for i in ['Worker']: logging.getLogger(i).setLevel(logging.INFO) for message_id in self.all_message_id: if transport == 'FILE': d = Header_to_XLSX(message_id, self.graph, self.output_path, self.conf['tabs']) # elif transport == 'QUEUE': # d = Domain_to_Kafka(message_id, self.graph, 'localhost:9092', self.conf['tabs'])
class Neo4j(): graph = None def __init__(self): print("create neo4j class ...") def connectDB(self): self.graph = Graph("http://localhost:7474", username="******", password="******") def matchItembyTitle(self, value): answer = self.graph.find_one(label="Item", property_key="title", property_value=value) return answer # 根据title值返回互动百科item def matchHudongItembyTitle(self, value): answer = self.graph.find_one(label="HudongItem", property_key="title", property_value=value) return answer # 根据entity的名称返回关系 def getEntityRelationbyEntity(self, value): answer = self.graph.data( "MATCH (entity1) - [rel] -> (entity2) WHERE entity1.title = \"" + value + "\" RETURN rel,entity2") return answer
class KnowledgeGraph(object): def __init__(self): conn_neo4j = ConnectionNeo4j() self._graph = Graph(host=conn_neo4j.ip, auth=(conn_neo4j.username, conn_neo4j.password)) def __repr__(self): self._object = "[INFO] The neo4j version is {}.".format( py2neo.__version__) def load_file(self, cypher): self._graph.run(cypher) def add_node(self, labels, **kwargs): node = Node(labels, **kwargs) self._graph.create(node) def delete_node(self): self._graph.delete_all() # def find(self, label): # return self._graph.find_one(label=label) def find(self): data = self._graph.data('MATCH (p:F**K) return p') df = pd.DataFrame(data) print(df) def match(self): pass
def query8(): hashtag = request.form['query8'] authenticate("localhost:7474", "neo4j", "qwertyuiop") g = Graph() a = g.data("""match(u:User)-[:POSTS]->(t:Tweet)-[:HAS_HASH]->(h:Hashtag) where h.hashtag=\'%s\' return h.hashtag as Hashtag,u.screen_name as User,Collect(t.tid) as Tid,count(*) as Tweet_count order by Tweet_count Desc LIMIT 3 """ % hashtag) page = " <style> table, th, td { border: 1px solid black; } </style>" page = page + "<table> <tr> <th> Hashtag </th> <th> User </th> <th> Tid's </th> <th> Tweet_Count </th> </tr> " for row in a: page = page + "<tr> " page += "<td> %s </td>" % repr(json.dumps(row['Hashtag'])) page += "<td> %s </td>" % repr(json.dumps(row['User'])) page += "<td> %s </td>" % repr(json.dumps(row['Tid'])) page += "<td> %s </td>" % repr(json.dumps(row['Tweet_count'])) page += "</tr>" page += "</table>" page = page + "<img src=\"/static/graph_query8.png\" width=1000px height=700px >" return page
def query1(): user = request.form['query1'] authenticate("localhost:7474", "neo4j", "qwertyuiop") g = Graph() a = g.data("""match (u1:User)<-[:MENTIONS]-(t:Tweet)-[:MENTIONS]-(u2:User) where u1.screen_name='%s' return u1.screen_name as User1,u2.screen_name as User2, Collect(t.tid) as Tid,count(*) as co_mentioncount Order by co_mentioncount DESC""" % user) page = " <style> table, th, td { border: 1px solid black; } </style>" page = page + "<table> <tr> <th> Mentioned_User1 </th> <th> Mentioned_User2 </th> <th> Tid's </th> <th> Total_Mentions </th> </tr> " for row in a: page = page + "<tr> " page += "<td> %s </td>" % repr(json.dumps(row['User1'])) page += "<td> %s </td>" % repr(json.dumps(row['User2'])) page += "<td> %s </td>" % repr(json.dumps(row['Tid'])) page += "<td> %s </td>" % repr(json.dumps(row['co_mentioncount'])) page += "</tr>" page += "</table>" page = page + "<img src=\"/static/graph_query1.png\" width=1000px height=700px >" return page
def update_related_bills(self): # Returns a list of related bills graph = Graph(**settings.GRAPH_DATABASE) query = """ MATCH (bill:Bill {remote_id: $remote_id})- [rels:SIMILAR_TO*1..2]-(related_bills:Bill) WHERE ALL (rel in rels WHERE rel.content_similarity > $similarity_score) WITH related_bills.uuid as coll UNWIND coll as x WITH DISTINCT x RETURN collect(x) AS related_ids """ response = graph.data( query, parameters={ "remote_id": self.instance.remote_id, # TODO need to figure out a way to dynamically identify this # similarity score as it may be too strict for some nodes # and too open for others. Causing large lists, small lists, and # no lists even though the bill may have some bills that relate to # it. "similarity_score": 0.125 }) try: result = response[0]['related_ids'] except IndexError: result = [] self.instance.related_bill_ids = result self.instance.save() return result
def lambda_handler(event, context): graph = Graph(host=os.environ["NAME_NEO_DOMAIN"], user=os.environ["USER"], password=os.environ["PASSWORD"]) dynamodb = boto3.resource('dynamodb') table = dynamodb.Table(os.environ["DYNAMODB"]) query = "MATCH (User {id:"+str(event['id'])+"})<-[FOLLOW]-(b) RETURN b.id, b.fcm, b.lang" results = graph.data(query) if results: if "tags" in event['ask']: del event['ask']['tags'] uid = str(event['ask']['id']) + "q" timestamp = int(time.time()) event['user']['id'] = event['id'] with table.batch_writer() as batch: for item in results: batch.put_item( Item={ 'id': str(item['b.id']), 'uid' : uid, 'user' : event['user'], 'type': 3, 'timestamp': timestamp, 'obj' : event['ask'], 'fcm': item['b.fcm'] if item['b.fcm'] else False, 'lang': item['b.lang'] if 'lang' in item else "FR", } )
def test_005_picklefiles(self): graph = Graph("http://ec2-34-207-175-160.compute-1.amazonaws.com:80", user=neo4juser, password=neo4jpass, bolt=False) #if conn(): server = BioSeqDatabase.open_database(driver="pymysql", user=biosqluser, passwd=biosqlpass, host=biosqlhost, db=biosqldb, port=biosqlport) seqann = BioSeqAnn(server=server, verbose=False) gfe = GFE() #cached_feats = gfe.all_feats # print("Finished loading cached_feats") # pickle_service = "feature-service.pickle" # with open(pickle_service, 'wb') as handle2: # pickle.dump(cached_feats, handle2, protocol=pickle.HIGHEST_PROTOCOL) feat_df = pd.DataFrame(graph.data(all_feats())) feat_df['ID'] = feat_df.apply(lambda row: ":".join([row['DB'], row['LOC'], str(row['RANK']), row['TERM'], row['SEQ']]), axis=1) feats = feat_df[['ID', 'ACCESSION']].set_index('ID').to_dict()['ACCESSION'] print("Finished loading feats") pickle_feats = "unique_db-feats.pickle" with open(pickle_feats, 'wb') as handle1: pickle.dump(feats, handle1, protocol=pickle.HIGHEST_PROTOCOL) gfedb = GfeDB(graph=graph, persist=False, verbose=False) act = ACT(gfedb=gfedb, seqann=seqann, load_gfe2hla=True, load_gfe2feat=True, load_seq2hla=True, gfe=gfe) print("Finished loading all!!") gfe2hla = act.gfe2hla seq2hla = act.seq2hla gfe2feat = act.gfe_feats pickle_gfe2feat = "gfe2feat.pickle" with open(pickle_gfe2feat, 'wb') as handle5: pickle.dump(gfe2feat, handle5, protocol=pickle.HIGHEST_PROTOCOL) pickle_gfe2hla = "gfe2hla.pickle" with open(pickle_gfe2hla, 'wb') as handle3: pickle.dump(gfe2hla, handle3, protocol=pickle.HIGHEST_PROTOCOL) pickle_seq2hla = "seq2hla.pickle" with open(pickle_seq2hla, 'wb') as handle4: pickle.dump(seq2hla, handle4, protocol=pickle.HIGHEST_PROTOCOL) pass
def neo4j(user,password,hostname,data): try: # Authenticate for server and connect it authenticate (hostname, user, password) graph=Graph() # If server is not connected : except Exception: print ("Unable to reach server.") sys.exit() graph.data("MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r") #start graph operations start=graph.begin() # Create node for Movies for movie in data.movies: movie_node=Node("Movies", mov_id=movie.ID, title=movie.title, released_year=movie.year, rating=movie.rating, genre=movie.genre) start.merge(movie_node) # Create node for every director in data.directors for director in data.directors: director_node=Node("Directors",userid=director.ID, fullname=director.name) start.merge(director_node) # Create node for every actor in data.actors for actor in data.actors: actor_node=Node("Actors", userid=actor.ID, fullname=actor.name) start.merge(actor_node) # Create node for every collector in data.collectors for collector in data.collectors: collector_node = Node("Collectors",userid=collector.ID, fullname=collector.name, email=collector.email) start.merge(collector_node) start.commit() relation(data,graph) queries(data,graph)
class GraphMaker(object): ''' neo4j: (https://10-0-1-111-33931.neo4jsandbox.com/browser/) Entire triple: CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})-[:ACTED_IN {roles:['Neo']}]->(TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'}) MATCH(N) RETURN N Create node: CREATE (n:Page {title:'Finance', url:'https://en.wikipedia.org/wiki/Finance'}) Get node (as "n") match(n:Page {title: "Finance"}) node = self.graph.evaluate("match (n:Section) where n.title='See also' return n") ''' def __init__(self): authenticate("localhost:7474", "neo4j", "ece406") self.graph = Graph("http://localhost:7474/db/data/") self.graph.delete_all() def appendNode(self, node): self.graph.create(node) def appendNodes(self, *nodes): for node in nodes: self.graph.create(node) def makeRelationship(self, subjectnode, propertystring, objectnode): self.graph.create(Relationship(subjectnode, propertystring, objectnode)) def drawGraph(self): options = {"Page": "title", "Section": "title"} draw(self.graph, options) def getData(self, querystring=None): if querystring is None: querystring = "match (n) return n" return self.graph.data(querystring) def printData(self, querystring=None): data = self.getData(querystring) for d in data: print(d) def getNodeByTitle(self, nodeTitle): node = self.graph.evaluate("match (n:Section) where n.title='" + nodeTitle + "' return n") if node: return node else: print("No node by that title") return
class GeneInteractionManager(): def __init__(self): self.graph = Graph() self.base_file_name = 'BIOGRID-MV-Physical-3.4.144.tab2' self.dir = os.path.dirname(os.path.abspath(self.base_file_name)) self.base_with_path = os.path.join(self.dir,self.base_file_name) self.create_interaction_graph(self.base_with_path) def get_absolute_path(self,file_name): return os.path.join(self.dir,file_name) def main_method(self): val = int(input('what do you want do?\n1.Load File\n2.Query for interacting genes\n')) if val ==1: graph_file = input('path of file?: ') file_name = self.get_absolute_path(graph_file) self.reate_interaction_graph(file_name) self.get_node_count(graph) if val == 2: n = int(input('get interacting genes of degree: ')) gene = input('Gene ID: ') self.get_nth_degree_genes(gene,n) #df = pd.read_csv(file_name, sep='\t') def create_interaction_graph(self,graph_file): try: self.graph.schema.create_index('Gene', 'gene_id') except: print('Graph is already indexed') query = '''lOAD CSV FROM "file:///{}" AS csvLine FIELDTERMINATOR '\\t' MERGE (a:Gene {{gene_id:csvLine[1]}}) MERGE (b:Gene {{gene_id:csvLine[2]}}) MERGE ((a)-[:INTERACTS_WITH]-(b)) '''.format(graph_file) self.graph.run(query) def get_node_count(self): query = '''start n=node(*) match (n) return count(n)''' print(self.graph.data(query)) def get_nth_degree_genes(self,gene,n): query = '''match (:Gene {{gene_id:"{}"}})-[:INTERACTS_WITH*..{}]->(a:Gene) return distinct properties(a)''' query = query.format(gene,n) interacting_genes = self.graph.run(query).data() genes = [g['properties(a)']['gene_id'] for g in interacting_genes] print(len(genes)) for gene in genes: print(gene)
def nodeget(): # 从知识库中取出excel示例实体中存在但网页信息中没有的实体信息 graph = Graph("http://127.0.0.1:7474", username="******", password="******") with codecs.open('new_nodes_links_4.json', 'r', encoding='utf-8') as foo: nodes = json.load(foo)['nodes'] with codecs.open('new_nodes_links_4.json', 'r', encoding='utf-8') as foo: links = json.load(foo)['links'] for node in nodes: # onebyone建立节点 建立新节点样例:graph.data("create(n:pers{name:'guo',gender:'male',age:'25',id:8}) ") # neo4j实体查询语句,此处查询实体名为word的实体 CQL = "create(n:Instancebracket" for key in node: value = node[key].replace("'", '').replace("\\", "\\\\") for char in key: if ord(char) == 32: key = key.replace(' ', '') # print(node['id'],key,32) break # if ' ' in key: #ascii为160空格 # #key = key.replace(' ','') # print(node['id'],key,160) # if '—' in key: # #key = "`"+key+"`" # print(node['id'],key,'—') # if ord(key[0]) >=48 and ord(key[0])<=57: key = "`" + key + "`" CQL = CQL + key + ":" CQL = CQL + "'{}'".format(value) + ',' CQL = CQL[0:-1] + '})' CQL = CQL.replace('bracket', '{') graph.data(CQL) for link in links: #onebyone建立关系 source_id = str(link['source']) target_id = str(link['target']) relation = str(link['name']) link_id = '{linkid:' + str(link['id']) + '}' query = '''match(n)where n.id='{}' match(m)where m.id='{}' create (n)-[r:{}{}]->(m) '''.format( source_id, target_id, relation, link_id) graph.data(query)
class Neo4jModel(): graph = None def __init__(self): print("create neo4j class....") def connectDB(self): print("connect...") self.graph = Graph('http://localhost:7474/db/noe4jDatabase/',username='******',password='******') print(self.graph) def query_brother_node(self): rel = self.graph.data("MATCH (source)-[r:`兄弟姐妹`]->(target) RETURN source.name,target.name") line_list = [] for line in rel: line_list.append(json.loads(str(line).replace("'","\""),object_hook=RelationNode.dict2relation)) return line_list def query_super_node(self): rel = self.graph.data("MATCH (source)-[r:`上下级`]->(target) RETURN source.name,target.name") line_list = [] for line in rel: line_list.append(json.loads(str(line).replace("'","\""),object_hook=RelationNode.dict2relation)) return line_list def query_cooperation_node(self): rel = self.graph.data("MATCH (source)-[r:`合作`]->(target) RETURN source.name,target.name") line_list = [] print(rel) for line in rel: line_list.append(json.loads(str(line).replace("'","\""),object_hook=RelationNode.dict2relation)) return line_list def query_node(self,name,relStr): rel = self.graph.data("MATCH (source{name:\""+name+"\"})-[r:`"+relStr+"`]-(target) RETURN source.name,target.name") line_list = [] for line in rel: line_list.append(json.loads(str(line),object_hook=RelationNode.dict2relation)) return line_list
def get_nodes(self): # Senha do Neo4j graph = Graph(password="******") filename = 'nodes.json' with open(filename, 'w') as outfile: outfile.write('{"nodes": ') json.dump(graph.data('MATCH (node) RETURN (node),ID(node)'), outfile) outfile.write('}') tweets = '{"nodes": [' try: nodes = json.load(open(filename)) if type(self.mode) is not int: try: node_weights = json.dumps(graph.data(self.mode)) except ClientError: node_weights = 1 else: node_weights = 1 for idx, node in enumerate(nodes['nodes']): node['node']['nodeId'] = str(node['ID(node)']) if node_weights != 1: weight = self.get_node_weight(int(node['node']['nodeId']), node_weights) else: weight = 1 node['node']['weight'] = str(weight) tweets += json.dumps(node['node']) if (idx + 1) < len(nodes['nodes']): tweets += ',' tweets += ']' return tweets except JSONDecodeError: print('Decoding JSON falhou') except IndexError: print('Decoding JSON falhou')
def get_regulatory_network(): #Connection to Neo4j graph = Graph("http://localhost:7474/db/data/cypher", password="******") #Extracting Source / Destination query = """ MATCH (n:Gene)-[:REGULATES]->(m:Gene) RETURN n.primaryIdentifier,m.primaryIdentifier """ regulations = graph.data(query) #Conversion into correct format graph = [] for edge in regulations: graph.append( (edge['n.primaryIdentifier'], edge['m.primaryIdentifier'])) return graph
def get_nodes_back(self, daysBack): day_of_year = int(self.get_day_of_year()) - daysBack query = "MATCH (n) WHERE (n.day_of_year={} AND n.`x-src-param` IS NOT NULL) RETURN n.Subject, n.day_of_year, n.short_date, n.FromEmail, n.`x-src-param`".format(day_of_year) graph = Graph(password=self.password) df = DataFrame(graph.data(query)) d = df.to_dict() rez = [] for k, v in d['n.Subject'].items(): subject = v date = d['n.short_date'][k] _day_of_year= d['n.day_of_year'][k] _from = d['n.FromEmail'][k] _src = d['n.`x-src-param`'][k] _body = d['n.Body'][k] rez.append({ 'day_of_year': _day_of_year, 'date': date, 'src': _src, 'subject':subject, 'from':_from, 'body':_body}) return rez
def topo_Sort(program_name): graph = Graph("http://localhost:7474/db/data/") adjPair = graph.data("MATCH (m)-[:HasPrerequisite]->(n) \ WHERE (m)-[:UnderProgram]->({name:" + program_name + "}) \ and (n)-[:UnderProgram]->({name:" + program_name + "}) \ RETURN m.id, n.id") courselist = [] for i in adjPair: courselist += [i["m.id"], i["n.id"]] courselist = list(set(courselist)) g = Prerequisite(courselist) for edge in adjPair: g.addEdge(edge["m.id"], edge["n.id"]) # MATCH (n:Course{id:"BUAD 280"}) RETURN n toposort = g.tps() return toposort[::-1]
def Hamming(passw, query, variable): # Call Database graph = Graph(password=passw) # Import results from query to pandas Dataframe df = pd.DataFrame(graph.data(query)) # Build combinations of the first 30 digits of each string combs = list(itertools.combinations(df[variable].str[:30],2)) # Apply Function distance = [hamming_distance(x[0], x[1]) for x in combs] # Plot and Save Figure ax = sns.boxplot(distance) ax.set(xlabel='Hamming Distance') plt.savefig('HammingDistance.png', dpi=300, tight_layout=True) plt.close()
def get_links(self): graph = Graph(password="******") filename = "relationships.json" with open(filename, 'w') as outfile: outfile.write('{"links": ') json.dump(graph.data('MATCH (n)-[r]->(m) RETURN n,r,m;'), outfile) outfile.write('}') links = ', "links": [' try: relationships = json.load(open(filename)) for idx, relationship in enumerate(relationships['links']): links += '{"source": ' + str( relationship['n']['id']) + ', "target": ' + str( relationship['m']['id']) + ', "value": 1}' if (idx + 1) < len(relationships['links']): links += ',' links += ']}' return links except JSONDecodeError: print('Decoding JSON falhou')
class help(object): def __init__(self): self.graph = Graph('127.0.0.1:7474', user='******', password='******') self.data_process() def data_process(self): out = defaultdict(list) result = self.graph.data('match (s)-[r]->(e) return s.name,e.name') for item in result: out[item['s.name']].append(item['e.name']) with open('../data/adjlist.txt', 'w', encoding='utf-8') as f: for key, values in out.items(): num = 0 for value in values: if key != value: num += 1 f.write('{} {}'.format(key, value)) if num != 0: f.write('\n')
class Neo4j(): graph = None def __init__(self): print("create neo4j class ...") def connectDB(self): self.graph = Graph("http://localhost:7474", username="******", password="******") def matchItembyTitle(self,value): answer = self.graph.find_one(label="Item",property_key="title",property_value=value) return answer # 根据title值返回互动百科item def matchHudongItembyTitle(self,value): answer = self.graph.find_one(label="HudongItem",property_key="title",property_value=value) return answer # 根据entity的名称返回关系 def getEntityRelationbyEntity(self,value): answer = self.graph.data("MATCH (entity1) - [rel] -> (entity2) WHERE entity1.title = \"" +value +"\" RETURN rel,entity2") return answer
class Neo4j(): graph = None def __init__(self): print("create neo4j class ...") def connectDB(self): self.graph = Graph("http://localhost:7474", username="******", password="******") def matchItembyTitle(self,value): answer = self.graph.find_one(label="Item",property_key="title",property_value=value) return answer # 根据title值返回互动百科item def matchHudongItembyTitle(self,value): answer = self.graph.find_one(label="HudongItem",property_key="title",property_value=value) return answer # 根据entity的名称返回关系 def getEntityRelationbyEntity(self,value): answer = self.graph.data("MATCH (entity1) - [rel] -> (entity2) WHERE entity1.title = \"" +value +"\" RETURN rel,entity2") return answer #查找entity1及其对应的关系(与getEntityRelationbyEntity的差别就是返回值不一样) def findRelationByEntity(self,entity1): answer = self.graph.data("MATCH (n1:HudongItem {title:\""+entity1+"\"})- [rel] -> (n2) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:NewNode {title:\""+entity1+"\"})- [rel] -> (n2) RETURN n1,rel,n2" ) return answer #查找entity2及其对应的关系 def findRelationByEntity2(self,entity1): answer = self.graph.data("MATCH (n1)- [rel] -> (n2:HudongItem {title:\""+entity1+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1)- [rel] -> (n2:NewNode {title:\""+entity1+"\"}) RETURN n1,rel,n2" ) return answer #根据entity1和关系查找enitty2 def findOtherEntities(self,entity,relation): answer = self.graph.data("MATCH (n1:HudongItem {title:\"" + entity + "\"})- [rel:RELATION {type:\""+relation+"\"}] -> (n2) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:NewNode {title:\"" + entity + "\"})- [rel:RELATION {type:\""+relation+"\"}] -> (n2) RETURN n1,rel,n2" ) return answer #根据entity2和关系查找enitty1 def findOtherEntities2(self,entity,relation): answer = self.graph.data("MATCH (n1)- [rel:RELATION {type:\""+relation+"\"}] -> (n2:HudongItem {title:\"" + entity + "\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1)- [rel:RELATION {type:\""+relation+"\"}] -> (n2:NewNode {title:\"" + entity + "\"}) RETURN n1,rel,n2" ) return answer #根据两个实体查询它们之间的关系 def findRelationByEntities(self,entity1,entity2): answer = self.graph.data("MATCH (n1:HudongItem {title:\"" + entity1 + "\"})- [rel] -> (n2:HudongItem{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:HudongItem {title:\"" + entity1 + "\"})- [rel] -> (n2:NewNode{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:NewNode {title:\"" + entity1 + "\"})- [rel] -> (n2:HudongItem{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:NewNode {title:\"" + entity1 + "\"})- [rel] -> (n2:NewNode{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) return answer #查询数据库中是否有对应的实体-关系匹配 def findEntityRelation(self,entity1,relation,entity2): answer = self.graph.data("MATCH (n1:HudongItem {title:\"" + entity1 + "\"})- [rel:RELATION {type:\""+relation+"\"}] -> (n2:HudongItem{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:HudongItem {title:\"" + entity1 + "\"})- [rel:RELATION {type:\""+relation+"\"}] -> (n2:NewNode{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:NewNode {title:\"" + entity1 + "\"})- [rel:RELATION {type:\""+relation+"\"}] -> (n2:HudongItem{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) if(len(answer) == 0): answer = self.graph.data("MATCH (n1:NewNode {title:\"" + entity1 + "\"})- [rel:RELATION {type:\""+relation+"\"}] -> (n2:NewNode{title:\""+entity2+"\"}) RETURN n1,rel,n2" ) return answer
def py2neo(): from py2neo import Graph, Path # graph = Graph() graph = Graph("http://localhost:7474/db/data/") print graph.data("MATCH (o:Organisation) RETURN o LIMIT 4")
""" # 方式1: g = Graph(host="localhost", password='******',bolt=True, bolt_port=7689) print g.data('match (n) return count(*)') sys.exit(1) """ # 方式2: *****访问被代理或docker 容器中的 neo4j server的话,只能用这种方式 ********* # set up authentication parameters http_port = "7476" authenticate("localhost:"+http_port, "username", "password") # connect to authenticated graph database g = Graph("http://localhost:"+http_port+"/db/data/", bolt_port=7689) g.data('match (n) return count(*)') g.run('match (n) return count(*)').dump() # import data in one transaction tx = g.begin() a = Node("Person", name="Alice") b = Node("Person", name="Bob") tx.create(a) ab = Relationship(a, "KNOWS", b) tx.create(ab) #tx.commit() print g.exists(ab) # get nodes in one autocommit transaction g.run("MATCH (a:Person) RETURN a.name, a.born LIMIT 4").data()