Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-10-2018 09:35 AM
Dear community,
I am quite new to NEO4J. I am attempting to learn about capabilities through an application that I am building in JAVA. There may be better ways of building my app, but that isn't what this post is about.
There is a documentation chapter here:
I went to the repo to get the source code too, just so I could run it and see what was happening where.
At the end of this post, I have placed a copy of the source.
This led me to some questions.
The line
private static final File databaseDirectory = new File( "target/neo4j-hello-db" );
identifies the file that will hold the data (or at least, I think it does). However, no such file gets created when I execute the code snippet. I am executing under Eclipse and the directory does get established. In the directory there is a single file - the log file.
What should I do to create the database?
What is the relationship (if any) between the configuration file identified when I start the neo4j service and the files used in the worked examples?
Is there a way to make the logs less verbose currently lots of INFO messages in the LOG. Many of which I cannot understand?
What fo the various single letters in the LOG output mean?
I don't see any error or warning messages in the LOG or on the console to indicate why the database file may not5 have been created
I am running the examples in Eclipse.
If there is a "simple" (i.e. for absolute neo4j newbies) set of turorials somewhere, I would be grateful if someone could point me to it.
Thanks
Chris
Example Code Follows.
/*
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.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;
public class EmbeddedNeo4j
{
private static final File databaseDirectory = new File( "target/neo4j-hello-db" );
public String greeting;
// tag::vars[]
GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;
// end::vars[]
// tag::createReltype[]
private enum RelTypes implements RelationshipType
{
KNOWS
}
// end::createReltype[]
public static void main( final String[] args ) throws IOException
{
EmbeddedNeo4j hello = new EmbeddedNeo4j();
hello.createDb();
hello.removeData();
hello.shutDown();
}
void createDb() throws IOException
{
FileUtils.deleteRecursively( databaseDirectory );
// tag::startDb[]
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( databaseDirectory );
registerShutdownHook( graphDb );
// end::startDb[]
// tag::transaction[]
try ( Transaction tx = graphDb.beginTx() )
{
// Database operations go here
// end::transaction[]
// tag::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::addData[]
// tag::readData[]
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );
// end::readData[]
greeting = ( (String) firstNode.getProperty( "message" ) )
+ ( (String) relationship.getProperty( "message" ) )
+ ( (String) secondNode.getProperty( "message" ) );
// tag::transaction[]
tx.success();
}
// end::transaction[]
}
void removeData()
{
try ( Transaction tx = graphDb.beginTx() )
{
// tag::removingData[]
// let's remove the data
firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
secondNode.delete();
// end::removingData[]
tx.success();
}
}
void shutDown()
{
System.out.println();
System.out.println( "Shutting down database ..." );
// tag::shutdownServer[]
graphDb.shutdown();
// end::shutdownServer[]
}
// tag::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 application).
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
// end::shutdownHook[]
}
12-10-2018 01:22 PM
HI @chris.bird,
have you followed the steps in the previous chapter?
https://neo4j.com/docs/java-reference/current/tutorials-java-embedded/#tutorials-java-embedded-setup
Does the hello world there works?
Does the folder for 'target' exists?
p.s.
Maybe you want to reformat the code in your post, but if this is the same of github you can just link there, unless you've changed anything.
12-14-2018 08:44 AM
Kuzeko,
Thanks for the reply. I had done everything you suggested and on one VM i have the issue I described. On a different VM on a different computer, the folder is present and behaves correctly.
Thanks also for the hint about the linking to code. I'll do that next time.
12-16-2018 06:34 AM
Maybe different configurations?
Have you tried docker? I generally find it much easier to use.
Look here for example
All the sessions of the conference are now available online