我正在用C#编写一个Windows控制台应用程序,该应用程序应该从XML文件导入15k个节点,并使用neo4jclient在Neo4j(2.0.0)中构建图形数据库。 在应用程序的最开始,我试图从数据库中删除所有节点和关系,以便在每次运行应用程序时数据库都是新鲜和干净的:
Console.WriteLine("Deleting nodes and relationships...");
graphClient.Cypher.OptionalMatch("n-[r]-()").Delete("r").ExecuteWithoutResults();
graphClient.Cypher.Match("n").Delete("n").ExecuteWithoutResults();
Console.WriteLine("...done!");
目前,数据库有大约16k个节点(它们之间没有关系),这些节点是由之前运行的应用程序本身创建的。当上面的第二个Delete语句运行时,在大约30秒后抛出此异常:
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
--- End of inner exception stack trace ---
at Neo4jClient.GraphClient.SendHttpRequest(HttpRequestMessage request, String commandDescription, HttpStatusCode[] expectedStatusCodes) in c:\TeamCity\buildAgent\work\5bae2aa9bce99f44\Neo4jClient\GraphClient.cs:line 138
at Neo4jClient.GraphClient.Neo4jClient.IRawGraphClient.ExecuteCypher(CypherQuery query) in c:\TeamCity\buildAgent\work\5bae2aa9bce99f44\Neo4jClient\GraphClient.cs:line 843
at Neo4jClient.Cypher.CypherFluentQuery.ExecuteWithoutResults() in c:\TeamCity\buildAgent\work\5bae2aa9bce99f44\Neo4jClient\Cypher\CypherFluentQuery.cs:line 322
at Xml2Cypher.Program.Main(String[] args) in c:\_PrivateProjects\KanjiDoc2Neo4J\Xml2Cypher\Program.cs:line 25
我也尝试使用Limit语句对delete语句进行批处理,但是我得到了相同的异常。有任何想法吗?我认为请求已超时,但即使是批处理似乎也无法解决问题。
graphClient.Cypher.Match("n").With("n").Limit(100).Delete("n").ExecuteWithoutResults();
我尝试从浏览器运行以下语句:
match (n:Characters) with n limit 100 delete n
但即使在那里我也会得到一个"未知错误"。
答案 0 :(得分:1)
只需增加neo4jclient中的http超时即可。
您在浏览器中遇到的错误是错误的。它应该说:"节点仍有关系"
删除的查询是:
MATCH (n)
OPTIONAL MATCH (n)-[r]->()
DELETE n,r
如果您有许多要删除的rel,您可能想要批量删除它。不幸的是,有希望的PERIODIC COMMIT
仅限于2.1-M01之后的LOAD CSV :(
所以你要回到自己的批处理中(删除一块5k节点及其rels)
MATCH (n)
LIMIT 5000
OPTIONAL MATCH (n)-[r]->()
DELETE n,r
RETURN count(*)
重复,直到它返回0.
答案 1 :(得分:0)
您也可以停止Windows服务,删除graph.db文件夹,然后重新启动该服务。我用它来完全“刷新”数据库,因为它将创建一个新的数据库。这样的事情应该有效:
public static void Main(string[] args)
{
RefreshDatabase();
}
private static void RefreshDatabase()
{
ServiceController sc = new ServiceController("Neo4j Graph Database", "computername");
if (sc.Status != ServiceControllerStatus.Stopped)
sc.Stop();
Console.WriteLine("Stopping Neo4j Graph Database service...");
//sc.WaitForStatus(ServiceControllerStatus.Stopped);
Console.WriteLine("Neo4j Graph Database service stopped.\n");
Console.WriteLine("Deleting graph.db files and folders...\n");
RecursiveDelete(@"C:\neo4j-community-2.0.1\data\graph.db");
Console.WriteLine("Finished deleting graph.db folder contents.\n");
Console.WriteLine("Starting Neo4j Graph Database service...\n");
sc.Start();
//sc.WaitForStatus(ServiceControllerStatus.Running);
Console.WriteLine("Neo4j Graph Database running.\n");
}
private static void RecursiveDelete(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo directory in di.GetDirectories())
{
directory.Delete(true);
}
}