如何从 android 访问(任何 http 或本地主机)上的 neo4j 运行
How can I access neo4j running on ( any http or localhost) from android
我在 mac 和 java 应用程序上成功实施了 neo4j,但我无法从 android 访问相同的应用程序,它在 dbpath.But 处崩溃,它保持 crashing.How 我可以让它工作吗?
而不是
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
}
EmbeddedGraphDatabase
只能在数据库和您的客户端代码驻留在同一个 JVM 中时使用(因此 'embedded' 这个词)。
如果您想远程访问 Neo4j 服务器,目前最好的方法是与 transactional Cypher endpoint directly or using the Neo4j JDBC driver 通信。请注意,在这两种情况下,您都使用 Cypher 与图表进行交互。
java rest bindings 的库起源于上述两个方法尚未到位的日子 - 因此 java-rest-bindings 将在未来被弃用。
我在 mac 和 java 应用程序上成功实施了 neo4j,但我无法从 android 访问相同的应用程序,它在 dbpath.But 处崩溃,它保持 crashing.How 我可以让它工作吗?
而不是
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
}
EmbeddedGraphDatabase
只能在数据库和您的客户端代码驻留在同一个 JVM 中时使用(因此 'embedded' 这个词)。
如果您想远程访问 Neo4j 服务器,目前最好的方法是与 transactional Cypher endpoint directly or using the Neo4j JDBC driver 通信。请注意,在这两种情况下,您都使用 Cypher 与图表进行交互。
java rest bindings 的库起源于上述两个方法尚未到位的日子 - 因此 java-rest-bindings 将在未来被弃用。