【发布时间】:2015-07-13 11:31:42
【问题描述】:
我正在创建 100 万个节点。我必须以较小的批次创建这些以避免内存不足。现在我正在尝试删除现有节点,但我再次遇到内存不足异常。我真的应该能够删除这个级别的数据而不会耗尽内存并且不必围绕这个限制编写代码。我在这里做错了吗?
我知道我可以增加 Java 堆大小,但我觉得这只是将真正的问题推迟到稍后我有更多数据要创建/删除的时间点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Neo4jClient;
namespace Neo4JWontDelete
{
class Program
{
private const int MaximumNumberOfWordsToCreate = 1000*1000;
private const int BatchSize = 5 * 1000;
static void Main(string[] args)
{
var client = new GraphClient(new Uri("http://neo4j:Xxxxxx1@localhost:7474/db/data"));
client.Connect();
Console.WriteLine("Starting with a clean database");
DeleteAllObjects(client);
Console.WriteLine("Creating data");
int currentWordNumber = 1;
while (currentWordNumber < MaximumNumberOfWordsToCreate)
{
int numberOfWordsToCreate = MaximumNumberOfWordsToCreate - currentWordNumber;
if (numberOfWordsToCreate > BatchSize)
numberOfWordsToCreate = BatchSize;
var words = Enumerable.Range(currentWordNumber, BatchSize).Select(x => new Word {Value = x.ToString()});
client.Cypher
.Create("(w:Word {words})")
.WithParam("words", words)
.ExecuteWithoutResults();
currentWordNumber += numberOfWordsToCreate;
Console.WriteLine(currentWordNumber - 1);
}
Console.WriteLine("Deleting data");
DeleteAllObjects(client);
}
private static void DeleteAllObjects(GraphClient client)
{
client.Cypher
.Match("(w :Word)")
.Delete("w")
.ExecuteWithoutResults();
}
}
class Word
{
public string Value { get; set; }
}
}
【问题讨论】:
-
为什么要将 BatchSize 和 MaximumNumberOfWordsToCreate 设置为乘法的结果,而不仅仅是 1000000 和 5000?
-
嗨彼得,我注意到了 - 你想要一个干净的数据库还是只是删除“单词”?干净的数据库将是
.Match("n").OptionalMatch("n-[r]-()").Delete(n,r).ExecuteWithoutResults()
标签: neo4j neo4jclient