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
Related
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'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:
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!
I am trying to query solr via solrj in Eclipse.
I have tried the latest solrj wiki example:
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
import java.net.MalformedURLException;
public class SolrQuery2 {
public static void main(String[] args) throws MalformedURLException, SolrServerException {
SolrServer solr = new CommonsHttpSolrServer("http://localhost:8080/solr");
// http://localhost:8080/solr/select/?q=outside
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/select");
params.set("q", "outside");
QueryResponse response = solr.query(params);
System.out.println("response = " + response);
}
}
However, I cant get past this error no matter what I do:
Exception in thread "main" java.lang.NoSuchMethodError:
org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
Next, I tried the cookbook example:
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery; //Error: The import org.apache.solr.client.solrj.SolrQuery conflicts with a type defined in the same file
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
public class SolrQuery {
public static void main(String[] args) throws Exception {
CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8080/solr");
server.setParser(new XMLResponseParser());
SolrQuery query = new SolrQuery();
query.setQuery("document"); //Error: The method setQuery(String) is undefined for the type SolrQuery
query.setStart(0); //Error: The method setStart(int) is undefined for the type SolrQuery
query.setRows(10); //Error: The method setRows(int) is undefined for the type SolrQuery
QueryResponse response = server.query(query); //Error: The method query(SolrParams) in the type SolrServer is not applicable for the arguments (SolrQuery)
SolrDocumentList documents = response.getResults();
Iterator<SolrDocument> itr = documents.iterator();
System.out.println("DOCUMENTS");
while(itr.hasNext()){
SolrDocument doc = itr.next();
System.out.println(doc.getFieldValue("id")+":"+doc.getFieldValue("content"));
}
}
}
However, that example might be dated for the current api as I cant even import the SolrQuery library.
Does anyone have a quick boilerplate example that works?
Thank you in advance.
PS. I am running windows7 with tomcat7 and solr 3.5. All I am trying to do at this point is a basic query and get the results back in some kind of list, array, whatever. When I query: http://localhost:8080/solr/select/?q=outside in firefox, the results come back just fine.
Here is how I got Solrj (Solr 3.6) working on my Windows7 box with eclipse:
import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;
public class SolrQuery {
public static void main(String[] args) throws MalformedURLException, SolrServerException {
SolrServer server = new HttpSolrServer("http://localhost:8080/solr");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "1");
QueryResponse response = server.query(params);
System.out.println("response = " + response);
}
}
I had to download additional jars (outside Solr of 3.6) for this to work: httpcomponents-client-4.2-beta1
In total, I needed 4 jars to get this working :
apache-solr-solrj-3.6.0.jar
httpclient-4.2-beta1.jar
httpcore-4.2-beta1.jar
httpmime-4.2-beta1.jar
Im not sure if my solution is considered a best practice in terms of boilercode, but it solves the issue of getting up on solrj w/ eclipse.
The org.slf4j.spi.LocationAwareLogger class is provided by the slf4j-api jar. What version are you running? Solr 3.5 requires version 1.6.1, I suspect you're using an eariler version.
If you're looking for a Solrj quick-start I'd recommend switching to Groovy. It can download the jar dependencies at run-time using Grab annotations. Example:
parse Solr xml files to SolrInputDocument
I would like to pre-fill and periodically put data to the Google Appengine database.
I would like to write a program in java and python that connect to my GAE service and upload data to my database.
How can I do that?
Thanks
Please use RemoteAPI for doing this programmatically.
In python, you can first configure the appengine_console.py as described here
Once you have that, you can launch and write the following commands in the python shell:
$ python appengine_console.py yourapp
>>> import yourdbmodelclassnamehere
>>> m = yourmodelclassnamehere(x='',y='')
>>> m.put()
And here is code from the java version which is self explanatory (directly borrowed from the remote api page on gae docs):
package remoteapiexample;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;
import java.io.IOException;
public class RemoteApiExample {
public static void main(String[] args) throws IOException {
String username = System.console().readLine("username: ");
String password =
new String(System.console().readPassword("password: "));
RemoteApiOptions options = new RemoteApiOptions()
.server("<your app>.appspot.com", 443)
.credentials(username, password);
RemoteApiInstaller installer = new RemoteApiInstaller();
installer.install(options);
try {
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
System.out.println("Key of new entity is " +
ds.put(new Entity("Hello Remote API!")));
} finally {
installer.uninstall();
}
}
}