Neo4j and embedded Java


(Chris Bird) #1

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:

https://neo4j.com/docs/java-reference/current/tutorials-java-embedded/#tutorials-java-embedded-hello-world

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.

/*

  • Licensed to Neo4j under one or more contributor
  • license agreements. See the NOTICE file distributed with
  • this work for additional information regarding copyright
  • ownership. Neo4j licenses this file to you under
  • the Apache License, Version 2.0 (the "License"); you may
  • not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing,
  • software distributed under the License is distributed on an
  • "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  • KIND, either express or implied. See the License for the
  • specific language governing permissions and limitations
  • under the License.
    */
    package org.neo4j.examples;

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[]

}


(Kuzeko) #2

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.


Kuzeko: Discovering my Community Connections!
(Chris Bird) #3

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.


(Kuzeko) #4

Maybe different configurations?
Have you tried docker? I generally find it much easier to use.

Look here for example