有效清除Neo4j数据库 [英] Effectively clear Neo4j database

查看:158
本文介绍了有效清除Neo4j数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前的问题清除Neo4j嵌入式数据库

现在,我了解到我不需要关闭数据库,只需要擦除此数据库中的所有数据即可.

Right now I understand that I don't need to shutdown database, I only need to wipe all data inside of this database.

我使用以下方法:

public static void cleanDb(Neo4jTemplate template) {
    template.query("MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r", null);
}

但是在大​​数据集上无法正常工作.

but it's not work properly on a large data sets.

此外,在Spring Data Neo4j的新版本中,我无法使用Neo4jHelper.cleanDb(db);

Also, with a new version of Spring Data Neo4j I can't use Neo4jHelper.cleanDb(db);

有什么方法可以正确有效地清除数据库状态而无需关闭/删除数据库?

Is any ways to correctly and effectively clean the database state without database shutdown/deleting ?

已更新

我已经使用cleanDb方法实现了以下util类

I have implemented following util class with cleanDb method

public class Neo4jUtils {

    final static Logger logger = LoggerFactory.getLogger(Neo4jUtils.class);

    private static final int BATCH_SIZE = 10;

    public static void cleanDb(Neo4jTemplate template) {
        logger.info("Cleaning database");

        long count = 0;
        do {
            GraphDatabaseService graphDatabaseService = template.getGraphDatabaseService();
            Transaction tx = graphDatabaseService.beginTx();
            try {
                Result<Map<String, Object>> result = template.query("MATCH (n) WITH n LIMIT " + BATCH_SIZE + " OPTIONAL MATCH (n)-[r]-() DELETE n, r RETURN count(n) as count", null);
                count = (long) result.single().get("count");
                tx.success();
                logger.info("count: " + count);
            } catch (Throwable th) {
                logger.error("Error while deleting database", th);
                throw th;
            } finally {
                tx.close();
            }
        } while (count > 0);

    }

}

现在它挂在行上:

tx.close();

如何解决,我在做什么错了?

How to fix it, what am I doing wrong ?

此外,经过多次实验,我注意到我可以只在工作的应用程序上多次清理数据库.重新启动应用程序后(我从控制台终止了应用程序进程)cleanDb方法停止在此现有数据库上工作并挂起.

Also, after number of experiments I have noticed that I can clean the database as many times as I want only on the working application. Right after application restart(I kill application process from console) cleanDb method stops working on this existing database and hangs.

messages.log中没有问题,一切看起来都很好:

No issues in the messages.log, everything looks fine:

2015-07-25 23:06:59.285+0000 INFO  [o.n.k.EmbeddedGraphDatabase]: Database is now ready

我不知道有什么问题..请帮助解决该问题.

I have no idea what can be wrong.. please help to solve this issue.

我使用:

neo4j version 2.2.3
lucene version 3.6.2
spring-data-neo4j version 3.4.0.M1

重要更新

我注意到,如果我在终止应用程序之前使用 graphDatabaseService.shutdown(); 方法,则一切正常.此损坏的数据库).

I noticed that everything work properly if I use graphDatabaseService.shutdown(); method before terminating of my application.. Otherwise database is destroyed(Neo4j server also hangs on this corrupted db).

有什么方法可以使Neo4j Embedded数据库更具容错能力?在生产环境中出现第一个错误(例如,停电事件)后,我将丢失所有数据.

Is any way to make Neo4j Embedded database more fault tolerant ? I will lose all my data after the first error (for example blackout event) in the production environment..

推荐答案

我不知道它如何与Spring Data一起使用,但是一般来说,您应该尝试批量删除节点/关系.

I don't know how it works with Spring Data, but in general you should try to delete nodes/relationships in batches.

密码查询:

MATCH (n)
WITH n LIMIT 10000
OPTIONAL MATCH (n)-[r]-()
DELETE n, r
RETURN count(n)

在您的应用程序中,您执行以下操作:

In your application you do:

while return_value > 0:
    run_delete_query()      

根据您的记忆,您当然可以增加LIMIT.

Depending on your memory you can of course increase the LIMIT.

这篇关于有效清除Neo4j数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆