0

I am successfully implemented neo4j on both mac and java application but I cannot access the same from and android and the it crashes at dbpath.But it keeps crashing.How can I get it to work?

INstead of

graphDb = new EmbeddedGraphDatabase(DB_PATH);

it is

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

also tried

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

Entire Code:

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

}
jason
  • 3,932
  • 11
  • 52
  • 123
  • do you want to run Neo4j on Android or do you want to access a Neo4j instance running on some server from an Android app? – Stefan Armbruster Feb 16 '15 at 15:10
  • I want access neo4j instance running on my mac (localhost:7474) from android .Basically an Http call .I dont want to install neo4j on my device.Thanks Stefan for looking into this query. – jason Feb 16 '15 at 15:16
  • @StefanArmbruster on some server from android app. – jason Feb 16 '15 at 15:28

1 Answers1

2

EmbeddedGraphDatabase can only be used if the DB and your client code should reside in the same JVM (therefore the word 'embedded').

If you want to remote access a Neo4j server the best ways today is either communicating with the transactional Cypher endpoint directly or using the Neo4j JDBC driver. Please note, in both cases you use Cypher to interact with the graph.

The library for java rest bindings is originated in the days where the two mentioned approached where not yet in place - so java-rest-bindings will be deprecated in the future.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • So how can I use http://neo4j.com/docs/stable/rest-api-transactional.html ,I mean: If I want to return "MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;" then How can I do the same in android.Here POST request will have key,value .So How or what I have to send to get the query above?? Thanks for your time .I really appreciate it.I am stuck here for a long time..Thanks again. – jason Feb 16 '15 at 15:54
  • How do I pass parameter when sending POST request ? – jason Feb 16 '15 at 17:17
  • So how should I proceed ? – jason Feb 17 '15 at 02:06
  • maybe http://stackoverflow.com/questions/6533234/how-to-make-httppost-call-with-json-encoded-body – Stefan Armbruster Feb 17 '15 at 09:32
  • but how/where do I define the function for particular path ? Like http://jon2012.com/api/register as mentioned in the example : api/register is located where ? .If I am using php then I can define the path but how to do the same for neo4j ? – jason Feb 17 '15 at 14:01
  • instead of jon2012.com/api/register use http://myhostname.com:7474/db/data/transaction/commit – Stefan Armbruster Feb 17 '15 at 14:13
  • I still couldn't get it to work .I tried neo4j-jdbc https://github.com/neo4j-contrib/neo4j-jdbc but I get class not found error.Can you please explain step by step implementation of localhost:7474/db/data/transaction/commit .Please.I really appreciate your help. – jason Feb 19 '15 at 16:43
  • Can you throw more light on transactional Cypher endpoint or an step by step tutorial? – jason Feb 19 '15 at 16:52
  • Hi Stefan.I have a query .How can I loop through create/merge in rest call .I tried passing params/array but it working only for single value not for array.Please help me out.I would really appreciate your help.Thanks .Also http://stackoverflow.com/questions/28716699/parameter-maps-cannot-be-used-in-merge-patterns and http://stackoverflow.com/questions/28714278/create-is-working-but-merge-in-neo4j-post-params-has-error – jason Feb 25 '15 at 13:40
  • How can I loop through 100 numbers ? Should I call merge statement 100 times ? – jason Feb 25 '15 at 19:55