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

查看:20
本文介绍了有效清除 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(); 方法,一切都会正常工作.. 否则数据库会被破坏(Neo4j 服务器也挂在这个损坏的数据库上).

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 嵌入式数据库更容错?在生产环境中出现第一个错误(例如停电事件)后,我将丢失所有数据..

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)

在您的应用程序中:

while return_value > 0:
    run_delete_query()      

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

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

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

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