并行向Neo4j数据库添加关系 [英] Adding relationships to a Neo4j database in parallel

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

问题描述

我试图将关系添加到我使用Java的ExecutorService并行创建的Neo4j图上.我有两个问题.首先,当我所有的可运行对象被累加时,我的程序向前跳转并关闭事务.其次,如果我保持事务处于打开状态(通过一个无限的事前操作,那么它就不会关闭,这样就不能真正解决问题),当可运行对象被执行时,它们将无法添加关系并且似乎卡在了该对象上.这些代码行.

Im trying to add relationships to a Neo4j graph I created in parallel using Java's ExecutorService. I'm having two issues. First, is while all my runnables are sumbitted my program jumps forward and closes the Transaction. Second, if I keep the Transaction open (via an infinite before hand so it doesn't close so that problem isn't really solved), when the runnables are being executed they are not able to add the relationships and seem to be stuck on those lines of code.

private void createDB()
{
    graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(Neo4j_DBPath);
    registerShutdownHook( graphDB );

    Transaction tx = graphDB.beginTx();
    try
    {
        parser.read(File, graphDB);
        parser.similirize();
        while (Global.running) {

        }
        tx.success();
        System.out.println("donedone");
    }
    finally
    {
        System.out.println("closing");
        tx.finish();
    }
}

read通过一些文件进行解析并创建我的数据库. Similirize进行m x n比较,并将这些关系添加到图形中.我希望可以并行执行此操作.这就是我现在要模拟的内容:

read parses through some files and creates my database. Similirize makes m x n comparisons and add these relationships to the graph. I was hoping to do this in parallel. This is what I have for similirize right now:

public void similirize() {
    System.out.println("starting ||");
    final Node NoSim = graphDB.createNode();
    NoSim.setProperty("type", "No Similarites");
    int threads = Runtime.getRuntime().availableProcessors();
    System.out.println(threads);
    final ExecutorService service = Executors.newFixedThreadPool(threads);
    for (final Reaction r: FailedRxns){
        System.out.println("submitting runnable");
        service.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("running runnable");
                try {
                    System.out.println("try");
                    Node SIM = Parser.this.mostSimilar(r);
                    while (!Thread.interrupted()) {
                        System.out.println("while");
                        if (SIM != null) {
                            System.out.println("if");
                            r.getUnderlyingNode().createRelationshipTo(SIM, RelTypes.SIMILAR_TO);
                            System.out.println("btwn the lines");
                            r.getUnderlyingNode().createRelationshipTo(SIM.getRelationships(Direction.OUTGOING).iterator().next().getOtherNode(SIM), RelTypes.INFERRED_IN);
                            System.out.println("made connection!");
                        } else {
                            System.out.println("else");
                            r.getUnderlyingNode().createRelationshipTo(NoSim, RelTypes.IN);
                            System.out.println("Couldn't make connection :(");
                        }
                    }
                } finally {
                    service.shutdown();
                }
            }
                });
        }
}

我的输出看起来像: 开始|| 提交可运行 提交可运行的....(持续了很长时间) 运行可运行 尝试 尽管 如果 运行可运行 尝试 尽管 如果 运行可运行 尝试 尽管 如果 运行可运行 尝试 尽管 如果

my output looks something like: starting || submitting runnable submitting runnable....(goes on for a long time) running runnable try while if running runnable try while if running runnable try while if running runnable try while if

,然后它永远被困在这里.

and then it is stuck here forever.

感谢您的所有帮助!

推荐答案

每个线程必须具有自己的事务.

Each Thread must have it's own Transaction.

来自文档:

所有访问图形,索引或架构的数据库操作都必须在事务中执行.

All database operations that access the graph, indexes, or the schema must be performed in a transaction.

...

事务绑定到创建它们的线程上.

Transactions are bound to the thread in which they were created.

此外,我不想让您想在 finally 块中调用service.shutdown().

Also, I don't think you want to invoke service.shutdown() in the finally block.

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

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