python景点的基本信息导入到neo4j中代码
时间: 2023-07-23 19:26:54 浏览: 50
以下是将Python中的景点基本信息导入到Neo4j中的代码示例:
```python
from neo4j import GraphDatabase
# Neo4j数据库连接信息
uri = "bolt://localhost:7687"
user = "neo4j"
password = "password"
# 景点基本信息数据
nodes = [
{"name": "景点A", "address": "地址A", "latitude": 1.0, "longitude": 2.0},
{"name": "景点B", "address": "地址B", "latitude": 3.0, "longitude": 4.0},
{"name": "景点C", "address": "地址C", "latitude": 5.0, "longitude": 6.0}
]
# Neo4j数据库操作
class Neo4jDB:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def add_node(self, name, address, latitude, longitude):
with self.driver.session() as session:
session.write_transaction(self._create_node, name, address, latitude, longitude)
@staticmethod
def _create_node(tx, name, address, latitude, longitude):
tx.run("CREATE (:Node {name: $name, address: $address, latitude: $latitude, longitude: $longitude})",
name=name, address=address, latitude=latitude, longitude=longitude)
# 执行导入
if __name__ == "__main__":
db = Neo4jDB(uri, user, password)
for node in nodes:
db.add_node(node["name"], node["address"], node["latitude"], node["longitude"])
db.close()
```
其中,`nodes`变量存储了景点的基本信息,每个景点以字典的形式表示,包含名称、地址、经度和纬度等信息。`Neo4jDB`类封装了Neo4j数据库的连接和操作,其中`add_node`方法用于将一个景点节点添加到Neo4j数据库中。在`__main__`中,循环遍历`nodes`变量,并通过`add_node`方法将每个景点节点添加到Neo4j数据库中。