【问题标题】:How can I access neo4j running on ( any http or localhost) from android如何从 android 访问在(任何 http 或 localhost)上运行的 neo4j
【发布时间】:2015-02-16 10:59:54
【问题描述】:

我在 mac 和 java 应用程序上都成功实现了 neo4j,但我无法从和 android 访问相同的,它在 dbpath 处崩溃。但它一直在崩溃。我怎样才能让它工作?

代替

graphDb = new EmbeddedGraphDatabase(DB_PATH);

是的

 RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data");  

也试过

 GraphDatabaseService graphDb=new RestGraphDatabase(“http://localhost:7474/db/data”);  

完整代码:

import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.impl.util.FileUtils;

public class EmbeddedNeo4j {
    private static final String DB_PATH = "/home/User/Documents/neo4j/";
    String greeting;
    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;

    // END SNIPPET: vars

    // START SNIPPET: createReltype
    private static enum RelTypes implements RelationshipType {
        KNOWS
    }

    // END SNIPPET: createReltype

    public static void main(final String[] args) {
        EmbeddedNeo4j hello = new EmbeddedNeo4j();
        hello.createDb();
        hello.removeData();
        hello.shutDown();
    }

    void createDb() {
        clearDb();
        // START SNIPPET: startDb
        graphDb = new EmbeddedGraphDatabase(DB_PATH);
        registerShutdownHook(graphDb);
        // END SNIPPET: startDb

        // START SNIPPET: transaction
        Transaction tx = graphDb.beginTx();
        try {
            // Mutating operations go here
            // END SNIPPET: transaction
            // START SNIPPET: addData
            firstNode = graphDb.createNode();
            firstNode.setProperty("message", "Hello, ");
            secondNode = graphDb.createNode();
            secondNode.setProperty("message", "World!");

            relationship = firstNode.createRelationshipTo(secondNode,
                    RelTypes.KNOWS);
            relationship.setProperty("message", "brave Neo4j ");
            // END SNIPPET: addData

            // START SNIPPET: readData
            System.out.print(firstNode.getProperty("message"));
            System.out.print(relationship.getProperty("message"));
            System.out.print(secondNode.getProperty("message"));
            // END SNIPPET: readData

            greeting = ((String) firstNode.getProperty("message"))
                    + ((String) relationship.getProperty("message"))
                    + ((String) secondNode.getProperty("message"));

            // START SNIPPET: transaction
            tx.success();
        } finally {
            tx.finish();
        }
        // END SNIPPET: transaction
    }

    private void clearDb() {
        try {
            FileUtils.deleteRecursively(new File(DB_PATH));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    void removeData() {
        Transaction tx = graphDb.beginTx();
        try {
            // START SNIPPET: removingData
            // let's remove the data
            firstNode.getSingleRelationship(RelTypes.KNOWS, Direction.OUTGOING)
                    .delete();
            firstNode.delete();
            secondNode.delete();
            // END SNIPPET: removingData

            tx.success();
        } finally {
            tx.finish();
        }
    }

    void shutDown() {
        System.out.println();
        System.out.println("Shutting down database ...");
        // START SNIPPET: shutdownServer
        graphDb.shutdown();
        // END SNIPPET: shutdownServer
    }

    // START SNIPPET: shutdownHook
    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running example before it's completed)
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                graphDb.shutdown();
            }
        });
    }
    // END SNIPPET: shutdownHook

}

【问题讨论】:

  • 您想在 Android 上运行 Neo4j,还是想从 Android 应用访问在某个服务器上运行的 Neo4j 实例?
  • 我想从 android 访问我的 mac (localhost:7474) 上运行的 neo4j 实例。基本上是一个 Http 调用。我不想在我的设备上安装 neo4j。感谢 Stefan 调查此查询。跨度>
  • @StefanArmbruster 在某些服务器上来自 android 应用程序。

标签: android neo4j


【解决方案1】:

EmbeddedGraphDatabase 只能在数据库和您的客户端代码应该驻留在同一个 JVM 中时使用(因此使用“嵌入”一词)。

如果您想远程访问 Neo4j 服务器,目前最好的方法是直接与transactional Cypher endpoint 通信或使用Neo4j JDBC driver。请注意,在这两种情况下,您都使用 Cypher 与图表进行交互。

java rest bindings 的库起源于所提到的两者尚未到位的日子——因此 java-rest-bindings 将在未来被弃用。

【讨论】:

  • 那么我该如何使用 neo4j.com/docs/stable/rest-api-transactional.html ,我的意思是:如果我想返回 "MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;"那么我该如何在android中做同样的事情。这里的POST请求将有键,值。那么我必须如何或发送什么来获取上面的查询?谢谢你的时间。我真的很感激。我被困在这里很长时间了。再次感谢。
  • 发送POST请求时如何传递参数?
  • 那我应该怎么做?
  • 但是如何/在哪里为特定路径定义函数?就像示例中提到的jon2012.com/api/register:api/register 位于哪里? .如果我使用的是 php,那么我可以定义路径,但如何为 neo4j 做同样的事情?
猜你喜欢
  • 2021-12-01
  • 2019-03-05
  • 2019-08-05
  • 1970-01-01
  • 2019-03-30
  • 2022-06-16
  • 2021-07-31
  • 2018-01-25
  • 1970-01-01
相关资源
最近更新 更多