首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从android访问运行在(任何http或localhost)上的neo4j?

如何从android访问运行在(任何http或localhost)上的neo4j?
EN

Stack Overflow用户
提问于 2015-02-16 10:59:54
回答 1查看 640关注 0票数 0

我在mac和java应用程序上都成功地实现了neo4j,但是我不能访问相同的dbpath.But和android,它在dbpath.But上崩溃,保持crashing.How,我能让它正常工作吗?

而不是

代码语言:javascript
运行
复制
graphDb = new EmbeddedGraphDatabase(DB_PATH);

它是

代码语言:javascript
运行
复制
 RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data");  

也试过

代码语言:javascript
运行
复制
 GraphDatabaseService graphDb=new RestGraphDatabase(“http://localhost:7474/db/data”);  

整个法典:

代码语言:javascript
运行
复制
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

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-16 15:40:36

只有当DB和客户端代码驻留在同一个JVM中(因此“embedded”一词)时,才能使用EmbeddedGraphDatabase

如果您想远程访问Neo4j服务器,最好的方法是直接与事务性Cypher端点通信,或者使用Neo4j JDBC驱动程序。请注意,在这两种情况下,您都使用Cypher与图形交互。

java rest绑定的库起源于上述两种方法尚未到位的日子,因此java-rest绑定将来将不再受欢迎。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28539896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档