如何用.NET的BoltGraphClient导出完整Neo4j数据库
使用C#(Neo4jClient/BoltGraphClient)导出Neo4j数据库到JSON/CSV
1. 准备工作
- 安装Neo4jClient NuGet包:
Install-Package Neo4jClient - 确保Neo4j服务正常运行,Bolt端口(默认7687)可访问
2. 核心实现:导出为JSON(流式写入)
2.1 定义数据模型
先创建与Neo4j数据结构对应的类,方便序列化:
public class Neo4jNode { public string Id { get; set; } public List<string> Labels { get; set; } public Dictionary<string, object> Properties { get; set; } } public class Neo4jRelationship { public string Id { get; set; } public string Type { get; set; } public string StartNodeId { get; set; } public string EndNodeId { get; set; } public Dictionary<string, object> Properties { get; set; } } public class Neo4jPath { public List<Neo4jNode> Nodes { get; set; } public List<Neo4jRelationship> Relationships { get; set; } }
2.2 BoltGraphClient连接与流式导出
通过流式写入避免内存占用过高,适合大数据库导出:
using Neo4jClient; using System.Text.Json; using System.IO; // 初始化Bolt连接 var boltUri = new Uri("bolt://localhost:7687"); var client = new BoltGraphClient(boltUri, "neo4j", "your-password"); client.ConnectAsync().Wait(); // 流式写入JSON文件 using var stream = new FileStream("neo4j_full_export.json", FileMode.Create, FileAccess.Write); using var jsonWriter = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }); jsonWriter.WriteStartObject(); // 导出所有节点 jsonWriter.WritePropertyName("nodes"); jsonWriter.WriteStartArray(); var nodesQuery = client.Cypher.Match("(n)") .Return(n => new Neo4jNode { Id = n.Id(), Labels = n.Labels(), Properties = n.As<Dictionary<string, object>>() }); foreach (var node in nodesQuery.Results) { JsonSerializer.Serialize(jsonWriter, node); } jsonWriter.WriteEndArray(); // 导出所有关系 jsonWriter.WritePropertyName("relationships"); jsonWriter.WriteStartArray(); var relsQuery = client.Cypher.Match("()-[r]->()") .Return(r => new Neo4jRelationship { Id = r.Id(), Type = r.Type(), StartNodeId = r.StartNodeId(), EndNodeId = r.EndNodeId(), Properties = r.As<Dictionary<string, object>>() }); foreach (var rel in relsQuery.Results) { JsonSerializer.Serialize(jsonWriter, rel); } jsonWriter.WriteEndArray(); // 导出路径(注意:全量路径数据量极大,建议限制长度或添加过滤条件) jsonWriter.WritePropertyName("paths"); jsonWriter.WriteStartArray(); var pathsQuery = client.Cypher.Match("p=()-[*1..3]->()") // 限制路径长度为1-3 .Return(p => new Neo4jPath { Nodes = p.Nodes<Neo4jNode>(), Relationships = p.Relationships<Neo4jRelationship>() }); foreach (var path in pathsQuery.Results) { JsonSerializer.Serialize(jsonWriter, path); } jsonWriter.WriteEndArray(); jsonWriter.WriteEndObject(); jsonWriter.Flush(); client.Dispose();
3. 导出为CSV格式
节点和关系结构不同,建议分开导出为两个独立CSV文件:
3.1 导出节点到CSV
using (var csvWriter = new StreamWriter("nodes.csv")) { // 写入表头 csvWriter.WriteLine("id,labels,properties"); foreach (var node in nodesQuery.Results) { var labels = string.Join(":", node.Labels); var propsJson = JsonSerializer.Serialize(node.Properties).Replace("\"", "\"\""); // CSV转义引号 csvWriter.WriteLine($"\"{node.Id}\",\"{labels}\",\"{propsJson}\""); } }
3.2 导出关系到CSV
using (var csvWriter = new StreamWriter("relationships.csv")) { csvWriter.WriteLine("id,type,startNodeId,endNodeId,properties"); foreach (var rel in relsQuery.Results) { var propsJson = JsonSerializer.Serialize(rel.Properties).Replace("\"", "\"\""); csvWriter.WriteLine($"\"{rel.Id}\",\"{rel.Type}\",\"{rel.StartNodeId}\",\"{rel.EndNodeId}\",\"{propsJson}\""); } }
4. 关键注意事项
- 性能优化:全量路径查询极易导致数据爆炸,必须通过
MATCH p=()-[*1..N]->()限制路径长度,或添加标签过滤条件 - BoltGraphClient局限处理:若遇到查询限制,可直接使用Neo4j官方驱动(Neo4jClient基于此封装),示例片段:
using Neo4j.Driver; var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "your-password")); using var session = driver.Session(); var nodesResult = session.Run("MATCH (n) RETURN id(n) as id, labels(n) as labels, properties(n) as properties"); foreach (var record in nodesResult) { var node = new Neo4jNode { Id = record["id"].As<string>(), Labels = record["labels"].As<List<string>>(), Properties = record["properties"].As<Dictionary<string, object>>() }; // 写入逻辑同上 } driver.Dispose();
- 流式写入必要性:直接写入文件而非加载所有数据到内存,避免大数据库导出时的内存溢出问题
内容的提问来源于stack exchange,提问作者Babnik
相关产品推荐
相关产品推荐

