First, if you haven’t already familiarized yourself with Getting Start with Neo4J and the Getting Started with the Neo4J Cypher Shell, check out those two articles.
Here are the steps to communicating with Neo4J from a Java environment. This tutorial should take you about 30 minutes to complete.
SETUP
1. Ensure you have the right version of Java and the Java JDK environment set up properly. On a Mac, this is probably already done for you. On a PC, you might have to manually download and install the right version of Java and set the PATH and JAVA_HOME environmental variable from scratch.
2. Download and install Neo4J. In this tutorial I am using Neo4J 1.8 M01 release. I installed it on my OSX 10.7.3 laptop under my home directory (/Users/dnickull/Software/Neo4J_1.8/)
3. Ensure you have the correct version of Eclipse installed. For this tutorial, I downloaded Eclipse Indigo Service Release 1, Build id: 20110916-0149. I use the version entitled Eclipse IDE for Java EE Developers.
STARTING THE PROJECT
4. Within Eclipse, select File -> new -> Java Project and give the new project a name. In my case I m calling mine “Neo_1.8”. Select Next.
5. Under the second new project page, select Java Build Path (1) the libraries tab (2) and then click on Add External Jars (3). This will allow you to browse for external jar files.
a. myString – concatenates various strings to print back to the console
b. graphDB – an instance of Neo4J to work with
c. myFirstNode – a neo4J node
d. mySecondNode – a second neo4J node
e. myRelationship – the simple relationship between the two nodes.
14. The next item to create is a static method to declare the list of relationships In this case we are using the word “KNOWS”, which sub-types the neo4J interface RelationshipTypes (API Docs).
15. A relationship type is mandatory on all relationships and is used to navigate the node space. RelationshipType is in particular a key part of the traverser framework but it's also used in various relationship operations on Node. RelationshipType is designed to work well with Java 5 enumerationss. This means that it's very easy to define a set of valid relationship types by declaring an enum that implements RelationshipType and then reuse that across the application.
16. Now we need to implement our main() method. The main method has 3 basic parts. The first is to instantiate myNeo4JInstance, an embedded instance of Neo4J. There are then consecutive calls to createDb(), removeData() and shutdown(). Add the stub code for these methods.
17. The first method to complete will be the createDb(). Add the following lines of code to this method.
NOTE: This is a bare bones, minimal tutorial. In production, there are several other items to take care of such as registering a shutdown hook to ensure the database stops properly. This would normally be done at this stage. Read more about the importance of the shutdown hook here - http://docs.neo4j.org/chunked/stable/tutorials-java-embedded-setup.html
THE TRANSACTION
18. The first line calls the GraphDatabaseFactory method to create a new instance of the embedded Neo4J which takes one argument, the DB_PATH we set up earlier. The second line start a new transaction named tx.
19. The next line will set up a new Transaction with the identifier “tx” and calls the graphDB’s createNode() method. The transaction basically has two main parts:
try {
//Some logic
tx.success
} finally {
tx.finish();
}
20. The idea is that your application logic goes under the try. All your application logic should be executed before the tx.success() statement. If all goes well, tx.success marks the transaction as successful but does not actually commit it. This is only done when tx.finish() is called. If tx.falure() is thrown during the logic phase, the database is rolled back. This is about as simple and elegant as it gets.
Here is how our logic will go to create a very simple graph of two musicians who know each other.
21. Lines 44 and 45 set properties for myFirstNote while lines 46-47 do the same for mySecondNode. Starting on line 49, a relationship is created between those two nodes using the type from our Java enum, namely "KNOWS". This is all printed to string before tx.success() is called. tx.finish() is where the transaction is actually committed.
22. Next we will tackle the removeData() method, which will be executed next in the main method. This essentially undoes all the work we just completed. Modify the contents as follows. Note that this uses the exact same transaction construct as when we created the database.
RUN THE PROJECT
25. That is it! If you run your project, all should go well and you should see the following in your console.













Thanks for taking the time to write it up ... 'tis particularly useful for those of us dusting off our "not microsoft" box ...
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete@ocs123 - never spam me again you asshole. I have sent the following email to Burberry (official), the webmaster the URL listed above, several authorities who deal with counterfeit merchandise and spam laws and google themselves. You IP address has been sent along with the reports and I know where you are. I never forgive, I never forget. Expect me.
ReplyDelete************************************************
Hi:
I run a successful blog at http://technoracle.blogspot.com Recently someone spammed my blog leaving unsolicited advertisements for your brand. I have reported this to Google and law enforcement authorities and asked them to suspend the bloggers account. Please note that advertising on my site is a minimum of $1,000 which I am happy to do, but not via spam and not for free.
The ad is as follows:
http://technoracle.blogspot.ca/2012/05/third-neo4j-tutorial-getting-started.html
ocs1232012-05-14 10:55:00 PM
Good post! Share our burberry outlet store. 2012 burberry sunglasses hot sale in www.designerburberrystore.com.
I trust this is not your doing and merely an ill-guided person with a financial interest in earning money from your brand. For most of my audience, they will actually be less likely to buy your brand by seeing you spamming my blog.
For your records, I clicked on the link at 11:09 PDT.
I will provide you 24 hours to clear this up before I take legal action. The ID of the spammer is published on the site. I trust that this is probably a a counterfeiter and that www.designerburberrystore.com is selling fake merchandise, ripping you off and at the same time violating my terms of service.
I trust you will take correct action. The comment will be removed in 24 hours, after I report the URL and blogger account to google's anti spam unit.
Duane Nickull
is there any way to see the programmatically created nodes in neo4j console???
ReplyDeleteis there any way to see the programmatically created code in neo4j console?
ReplyDeleteGreat primer, gets to the bare bones without swamping a first time Neo4jer with info.
ReplyDeleteHi Duane,
ReplyDeleteGreat article. Thanks a lot for posting it.
I just have one question. Hope you would answer it.
In this case, you are creating one instance and carrying out simple operations and finally shutting down the instance.
However, I wanted to know how could I create a single instance which is always running and multiple threads accessing it. To give you a little more idea, I am trying to create a website using Neo4j. I am using Java base (JSP, Servlets, etc.). So, since a graph DB can only have one instance, I need to create a single instance which is then used by various threads arising from the servlet.
Would highly appreciate a little guidance from you.
Regards,
Nick
HI there,
Deleteto run against a server, either you use the Neo4j Server (default download, docs at http://docs.neo4j.org/chunked/snapshot/server.html) or you just use the Neo4j embedded instance as a singleton in your servlet container, since Neo4j is thread safe.
HTH
/peter
Knowledgeable. Thank You
ReplyDeleteThere are a few error
ReplyDeleteon this line
private static enum RelTypes implements RelationshipType
error:
Multiple markers at this line
- 'enum' should not be used as an identifier, since it is a reserved keyword from source
level 1.5 on
- enum cannot be resolved to a type
- Syntax error on token "enum", class expected
There are a few error
ReplyDeleteon this line
private static enum RelTypes implements RelationshipType
error:
Multiple markers at this line
- 'enum' should not be used as an identifier, since it is a reserved keyword from source
level 1.5 on
- enum cannot be resolved to a type
- Syntax error on token "enum", class expected
nurullah
ReplyDeleteI am not seeing an error. Can you let me know what environment you are using?
Duane
Hi Duane,
ReplyDeleteThanks for the article. I'm new to Neo4j and have gone through the tutorials of connecting to Neo in embedded mode.
My background of relational database is still haunting me. I have a basic question is to how to acquire the neo4j database instance that is running in a remote server. I want to do it using a client java program.
Thanks again for the article and I would really appreciate if you can help me out.
There is a good example here:
ReplyDeletehttp://docs.neo4j.org/chunked/snapshot/tutorials-java-embedded-hello-world.html
Knowledgeable. Thank You
ReplyDelete