I am new to neo4j. I have created a java project in Eclipse to create a neo4j database and some nodes in it. I am seeing the database directory getting created in my workspace folder. But how do i view it on the neo4j browser?
Here is the example code i used :
package com.neo4j.demo;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Neo4jDemoOperations {
public static void main(String args[]) {
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db = dbFactory.newEmbeddedDatabase("./DemoDatabase");
try (Transaction tx = db.beginTx()) {
Node javaNode = db.createNode(Tutorials.JAVA);
javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");
Node scalaNode = db.createNode(Tutorials.SCALA);
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");
Relationship relationship = javaNode.createRelationshipTo(
scalaNode, TutorialRelationships.JVM_LANGIAGES);
relationship.setProperty("Id", "1234");
relationship.setProperty("OOPS", "YES");
relationship.setProperty("FP", "YES");
tx.success();
}
System.out.println("Done successfully");
}
}
The Tutorials and TutorialRelationships are enums.
Thanks in advance.
I changed the path to the database in the line
org.neo4j.server.database.location=
in the file neo4j-server.properties inside the conf folder. I chose the path as the path to my database in the workspace folder and it worked!
Related
I'm beginning in Neo4j java embedded graph.
I have made a first test but I cant visualize my graph in neo4j-community.
Here is my code for creating the graph :
package connection;
import java.io.File;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class embededdedGraph {
public static void main(String... args) throws Exception {
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
File graphDir = new File("/home/nicolas/neo4j-community-3.5.14/data/databases/cars.db");
GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase(graphDir);
Transaction tx = graphDb.beginTx();
createNode(graphDb);
tx.close();
}
public static void createNode(GraphDatabaseService graphDb) {
Node car = graphDb.createNode();
car.addLabel(Label.label("Car"));
car.setProperty("make", "tesla");
car.setProperty("model", "model3");
Node owner = graphDb.createNode(Label.label("Person"));
owner.setProperty("firstName", "Oliver");
owner.setProperty("lastName", "John");
owner.createRelationshipTo(car, RelationshipType.withName("owner"));
}
}
Next I changed the value of "#dbms.active_database" to "dbms.active_database=cars.db in the /neo4j-community-3.5.14/conf/neo4.conf file.
When I restart the neo4j-community, the database name is "cars.db" but it indicated that there are no labels and relationships in it.
What is the problem I cannot figure?
Nicolas
It looks like you need to call tx.success() or tx.fail() before tx.close().
https://neo4j.com/docs/java-reference/3.5/javadocs/org/neo4j/graphdb/Transaction.html
I am also new at this API, I hope it helps
I have installed the community edition 3.5.0-alpha07 of Neo4j and I am trying to create a simple graph containing two nodes following this tutorial. The problem is that the keyword Transaction from org.neo4j.graphdb.Transaction shows syntax error. The code is given below :
Operation.java
import java.io.File;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Operation {
//private static final File file =new File();
public static void main(String[] args) {
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db= dbFactory.newEmbeddedDatabaseBuilder("C:\\Users\\pritom.mazumdar\\Downloads\\neo4j-community-3.5.0-alpha07\\data\\databases\\graph.db").newGraphDatabase();
//Transaction tx = db.beginTx();
try (Transaction tx = db.beginTx()) {
The resource type Transaction does not implement java.lang.AutoCloseable
Node javaNode = db.createNode();
javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");
Node scalaNode = db.createNode();
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");
Relationship relationship = javaNode.createRelationshipTo(scalaNode, (RelationshipType) Label.label("JVM_LANG"));
relationship.setProperty("Id","1234");
relationship.setProperty("OOPS","YES");
relationship.setProperty("FP","YES");
tx.success();
}
System.out.println("Done successfully");
}
}
I have tried implementing AutoCloseable and overriding the close method, but it still doesn't work, the syntax error remians.
It is because try-with-resources Only Accepts AutoClosable Objects. The compiler Says that Transaction is not an AutoClosable.
I am trying to create a simple connection and insert a document to local MongoDB instance using java (eclipse 4.7.1a). MongoDB is up & running and able to do operations via "Compass" as well.
package com;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
public class connectToDB {
public static void main( String args[] ) {
try{
// To connect to mongodb server
MongoClient mongoClient = new MongoClient( "localhost" , 27017
);
// Now connect to your databases
DB db = mongoClient.getDB("test");
System.out.println("Connect to database successfully");
// if collection doesn't exists, MongoDB will create it for you
DBCollection coll = db.getCollection("testCollection");
System.out.println("Collection coll selected successfully");
// insert
List<Integer> books = Arrays.asList(27464, 747854);
DBObject documents = new BasicDBObject("_id", "jo")
.append("name", "Jo Bloggs")
.append("address", new BasicDBObject("street", "123 Fake St")
.append("city", "Faketon")
.append("state", "MA")
.append("zip", 12345))
.append("books", books);
coll.insert(documents);
System.out.println("Document added to Collection coll successfully");
// update
BasicDBObject query = new BasicDBObject();
query.put("name", "Xo");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "Jo Bloggs-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
coll.update(query, updateObj);
System.out.println("Document updated to Collection coll successfully");
// find - search
BasicDBObject searchQuery1 = new BasicDBObject();
searchQuery1.put("name", "mkyong");
DBCursor cursor = coll.find(searchQuery1);
System.out.println("Selection from Collection coll done successfully");
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
// delete
BasicDBObject searchQuery2 = new BasicDBObject();
searchQuery2.put("name", "Xo");
coll.remove(searchQuery2);
System.out.println("Document deleted from Collection mycol2 successfully");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
But when executed, ends with following error in eclipse.
Nov 01, 2017 2:19:31 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE,
requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms',
maxWaitQueueSize=500}
Exception in thread "main" java.lang.NoSuchMethodError:
org.bson.BsonDocument.clone()Lorg/bson/BsonDocument;
at com.mongodb.connection.ClientMetadataHelper.
createClientMetadataDocument(ClientMetadataHelper.java:159)
at com.mongodb.connection.ClientMetadataHelper.
createClientMetadataDocument(ClientMetadataHelper.java:149)
at com.mongodb.connection.InternalStreamConnectionFactory.<init>
(InternalStreamConnectionFactory.java:37)
at com.mongodb.connection.DefaultClusterableServerFactory.
create(DefaultClusterableServerFactory.java:58)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:359)
at com.mongodb.connection.SingleServerCluster.<init>
(SingleServerCluster.java:52)
at com.mongodb.connection.DefaultClusterFactory.
createCluster(DefaultClusterFactory.java:147)
at com.mongodb.Mongo.createCluster(Mongo.java:726)
at com.mongodb.Mongo.createCluster(Mongo.java:720)
at com.mongodb.Mongo.<init>(Mongo.java:290)
at com.mongodb.Mongo.<init>(Mongo.java:285)
at com.mongodb.Mongo.<init>(Mongo.java:281)
at com.mongodb.MongoClient.<init>(MongoClient.java:186)
at com.mongodb.MongoClient.<init>(MongoClient.java:163)
at com.mongodb.MongoClient.<init>(MongoClient.java:153)
at com.connectToDB.main(connectToDB.java:25)
MongoDB version : v3.4.9
Following jar files used. bson 3.0.4.jar & mongo-java-driver-3.5.0.jar
Could anyone help me out with on why this is been giving this error?
Your codes are working perfectly before you use your MongoDB code run simple hello world program,
public class connectToDB {
public static void main( String args[] ) {
System.out.println("Hello world");
}
}
if it works then put your MongoDB codes in the main and run it again. in case if it does not work then the first thing you need to learn "how to use Eclipse IDE?" before you jump into the database.
Here is the result of your codes.
here is how your data looks like in the database.
I deleted 'bson-3.0.2.jar' from referenced libraries of the code, and it works.
As the error message said,
NoSuchMethodError:
org.bson.BsonDocument.clone()Lorg/bson/BsonDocument;
it seems that certain method dependency has been crashed between bson jar and mongodb java driver.
According to the document of Neo4j, I tried below to create the database using Neo4j Java APIs in Eclipse:
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("D:/Eclipse/Workplace/test");
and also tried to set the path under the Neo4j folder("D:\Neo4j3.0.1\workplace3"). But I got the same error:
The method newEmbeddedDatabase(File) in the type GraphDatabaseFactory is not applicable for the arguments (String)
Then I tried to import java.io.File; and add:
File dbpath = new File("D:/Neo4j3.0.1/workplace3");
org.neo4j.graphdb.GraphDatabaseService db = dbFactory.newEmbeddedDatabase(dbpath);
then the previous two packages are not used: org.neo4j.graphdb.GraphDatabaseService;, org.neo4j.graphdb.Transaction; But I can compile the program and show "Done successfully" in the Eclipse console.
When I tried to connect to the database using neo4j-ce.exe, it gave me below Alert:
Starting Neo4j failed: Component org.neo4j.server.database.LifecycleManagingDatabase#397b7f" was successfully initialized, but failed to start. Please see attached cause exception.
My code:
package com.peterlan522.neo4j.java.example;
import java.io.File;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.RelationshipType;
public class Neo4jJavaAPIDBOperation {
public enum Tutorials implements Label { JAVA, SCALA, SQL, NEO4J,}
public enum TutorialRelationships implements RelationshipType { JVM_LANGIAGES, NON_JVM_LANGIAGES,}
public static void main(String[] args) {
org.neo4j.graphdb.factory.GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
File dbpath = new File("D:/Neo4j3.0.1/workplace3");
//GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("D:/Eclipse/Workplace/test");
org.neo4j.graphdb.GraphDatabaseService db = dbFactory.newEmbeddedDatabase(dbpath);
try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
Node javaNode = db.createNode(Tutorials.JAVA);
javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");
Node scalaNode = db.createNode(Tutorials.SCALA);
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");
Relationship relationship = javaNode.createRelationshipTo
(scalaNode,TutorialRelationships.JVM_LANGIAGES);
relationship.setProperty("Id","1234");
relationship.setProperty("OOPS","YES");
relationship.setProperty("FP","YES");
tx.success();
}
System.out.print("Done successfully");
}
}
Could anyone help on this? And give executable examples on this. Thank you so much!
Below are the softwares version:
Neo4j community version 3.0.1,
Eclipse Mars(4.5.0),
Java 1.8.0_91,
JRE System Library: JavaSE-1.8
And please get in below link to see the log.txt:
https://drive.google.com/file/d/0B2xDq3--mwK4a0FoanlDengzVWs/view?usp=sharing
I'm working on an webserver project with tomact. I have a dataBase in neo4j. How can I link it to the dataBase? I want to get information from there and also add new data, in the servlets that I created. I tried running the dataBase in the background But i still get "Forbidden (403) - Forbidden" in my project. Do I have to set it somehow? I'm using eclipse, how can I do that?
You can try this approach:
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
// ...
public static void main(String[] args) {
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase("path_to_neo4j_database/your_database_folder");
try (Transaction tx = graphDb.beginTx()) {
// use graphDb object to manipulate the DB content
tx.success();
}
graphDb.shutdown();
System.out.println("Done :) ");
}
// ...
And don't forget to add the Neo4j library to build path: