using Neo4j embedded in Java applications - java

I am trying to use Neo4j embedded in Java applications, and I am using this code:
package com.tp.neo4j.java.examples;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Neo4jJavaAPIDBOperation {
public static void main(String[] args) {
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db = dbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");
try (Transaction tx = db.beginTx()) {
// Perform DB operations
tx.success();
}
}
}
But I got this Exception:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method newEmbeddedDatabase(File) in the type GraphDatabaseFactory is not applicable for the arguments (String)
Syntax error on token ";", try expected after this token
any idea, please

newEmbeddedDatabase expect File as argument
GraphDatabaseService db = dbFactory.newEmbeddedDatabase(new File("C:/TPNeo4jDB"));

Related

Neo4j embedded graph java visualization

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

unresolved compilation using hbase in java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
Import org.apache.hadoop.hbase.client.HBaseAdmin;
public class HBaseConnection
{
public static void main(String[] args) throws IOException
{
HBaseConfiguration hc = new HBaseConfiguration(new Configuration());
HTableDescriptor ht = new HTableDescriptor("guru99");
ht.addFamily( new HColumnDescriptor("education"));
ht.addFamily( new HColumnDescriptor("projects"));
System.out.println( "connecting" );
HBaseAdmin hba = new HBaseAdmin( hc );
System.out.println( "Creating Table" );
hba.createTable( ht );
System.out.println("Done......");
}
}
The above is my java code that I'm using to connect my hbase with my java api, but I get an error as
Exception in thread "main" java.lang.Error: Unresolved compilation
error
I cleaned the project and tried running it again, I have added all the external jar files that HBase has, by the im using HBase in a pseudo distribution mode with hadoop, and at the top of my eclipse I also get an error as
The type com.google.com.protobuf.GeneratedMessage$Builder cannot be resolved. It is indirectly referenced from required .class files

Neo4j Java API, how to specify the path of creating database

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

Using neo4j database in eclipse project

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:

Exception in thread “main” java.lang.Error: Unresolved compilation problems:

Whenver I run this program, its always throwing this error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
And while I was trying run this code in Eclipse IDE it's telling me
declared package org.exist.examples.xmldb does not match the expected package
When I compile it it's throwing the error at Put.main(Put.java:27)
package org.exist.examples.xmldb;
import java.io.File;
import org.exist.xmldb.XmldbURI;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;
/**
* Add a document to the database.
*
* Call with java -jar start.jar org.exist.examples.xmldb.Put collection docName
*
*/
public class Put {
public final static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
protected static void usage() {
System.out.println("usage: org.exist.examples.xmldb.Put collection docName");
System.exit(0);
}
public static void main(String args[]) throws Exception {
if(args.length < 2)
usage();
String collection = args[0], file = args[1];
// initialize driver
String driver = "org.exist.xmldb.DatabaseImpl";
Class<?> cl = Class.forName(driver);
Database database = (Database)cl.newInstance();
database.setProperty("create-database", "true");
DatabaseManager.registerDatabase(database);
// try to get collection
Collection col =
DatabaseManager.getCollection(URI + collection);
if(col == null) {
// collection does not exist: get root collection and create.
// for simplicity, we assume that the new collection is a
// direct child of the root collection, e.g. /db/test.
// the example will fail otherwise.
Collection root = DatabaseManager.getCollection(URI + XmldbURI.ROOT_COLLECTION);
CollectionManagementService mgtService =
(CollectionManagementService)root.getService("CollectionManagementService", "1.0");
col = mgtService.createCollection(collection.substring((XmldbURI.ROOT_COLLECTION + "/").length()));
}
File f = new File(file);
// create new XMLResource
XMLResource document = (XMLResource)col.createResource(f.getName(), "XMLResource");
document.setContent(f);
System.out.print("storing document " + document.getId() + "...");
col.storeResource(document);
System.out.println("ok.");
}
}
You are trying to run code that does not compile. Eclipse inserts bytecode that throws this error instead.

Categories