from neo4j import GraphDatabase # pip install neo4j
import pandas as pd #pip install pandas
AuraDBId = 'ENTER DB ID HERE'
dbUsername = 'ENTER USERNAME HERE' #Default neo4j for Aura
password = 'ENTER YOUR PASSWORD HERE'
boltUrl = f"neo4j+ssc://{AuraDBId}.databases.neo4j.io:7687"
graphDBDriver = GraphDatabase.driver(boltUrl,auth=(dbUsername, password))
graphDBDriver.verify_connectivity()
# Fetching properties and generating the List of columns
nodeV = 'n'
yourLabel = 'Movie' #Replace this with your actual label
columnsQuery=f"MATCH({nodeV}:{yourLabel}) RETURN keys(n) LIMIT 1" # Add a WHERE clause to filter this node if required. The properties of this node are going to be used as a reference for the columns
with graphDBDriver.session() as session:
propertiesRes = session.run(columnsQuery)
propertiesList = propertiesRes.data() # Returs a List with 1 dictionary
cols = propertiesList[0].get(f'keys({nodeV})')
returnString = ', '.join([f'{nodeV}.{col} as {col}' for col in cols]) #Generating a return statement with aliases
nodesQuery = f"MATCH({nodeV}:{yourLabel}) RETURN {returnString}"
with graphDBDriver.session() as session:
nodesRes = session.run(nodesQuery)
nodeResList = nodesRes.data()
graphDF = pd.DataFrame(nodeResList)
graphDBDriver.close()
1条答案
按热度按时间whlutmcx1#
Aura DS和Aura DB支持python驱动程序中的Cypher。你可以获取节点的属性并从中生成一个 Dataframe 。
这里的小挑战是,当涉及到模式时,Pandas DF不像Neo4j Graphs那样宽容。
因此,从已知节点获取属性列表,然后基于结果生成RETURN Cypher查询是一种更好的方法。
或者,您可以在'nodesQuery' Cypher语句中硬编码特定的参数和别名。但是如果您要获取太多的列/属性,这可能会变得很乏味。
确保你修改了AuraDBId,dbUsername,password和yourLabel。一旦你有了Python中的 Dataframe ,它应该和平常一样。
注意事项:
1.这种在代码中硬编码数据库凭证的方法简单而简短,但不推荐使用。始终单独存储凭证,并将其加载到实际代码中使用。在.env文件中并使用loadenv(),或存储为json文件并使用json.load读取内容。