Workshop

Neo4j Wiki から

目次

[edit] Welcome

This is a workshop for the Neo4j ecosystem which can be used to get step by step hands-on experience with Neo4j and some of its related components.

[edit] Getting the environment sorted out

Download the backing code: neo4j-workshop-1.0.zip, neo4j-workshop-1.0.tar.gz. The code is also available at the following location using Subversion: https://svn.neo4j.org/examples/workshop.

Unzip the contents and import the project into your favorite toolset.

Eclipse: Use the New Project dialog (Ctrl-N) and select Java Project. Under Contents, select "Create project from existing source". In case you already use a Maven plugin, you can also use the Import wizard and select Existing Maven Projects.

NetBeans: Use the Open Project dialog (Ctrl-Shift-O) and open the project, which will be recognized as a Maven project (tested in Netbeans IDE 6.8). Note that the solutions are then found under Other Sources in the project. If this is too buggy (we had problems with the Windows version) or you don't want to use Maven, then use the New Project wizard, select Java and Java Project with Existing Sources. In the existing sources part, import src/main/java from the unzipped project contents for source and src/test/java for test packages. To make the solutions available as well, add the src/solutions/java directory too. After the project has been created, right click on Libraries in the Project view, click on Add JAR/Folder and browse to the lib directory and add all the jar files (you can simply select all the files at once).

The backing code to this workshop contains skeletons for you to fill with code in some key locations. It also contains the solutions for reference during the workshop. The workshop contains some assignments and each assignment has its script which runs it, they are located in the scripts/ folder and exists both as a *nix script as well as windows .bat files.

If you're using Maven to compile the code you can use mvn compile. You can also compile the code with Ant by issuing a ant command at the workshop/ root. Or you can import the project into Eclipse or any other IDE, just make sure that the compiled classes are in either build/classes or target/classes.

The dependencies are (for convenience) in the lib/ folder, even though Maven is capable of downloading them on the fly from the Neo4j Maven repository, where an equivalent would be: mvn dependency:copy-dependencies && ln -s target/dependency lib.

[edit] Javadoc and sources

To have your IDE bring embrace the javadocs/sources from the Neo4j code either make use of a maven plugin like the eclipse maven plugin or explicitly tell your IDE where they are:

In f.ex. Eclipse you can attach sources by right-click the neo4j-kernel-1.0.jar file in Referenced Libraries and choose Properties then fill in the javadoc/source fields there.

[edit] Assignment 1: Matrix, or building a graph

The purpose of this assignment is to get a first experience with Neo4j and its core component, the kernel. After this assignment you'll be able to build a simple graph and perform some basic traversals on it. If you're interested in getting an overview of the the basic interfaces and classes please head over to the API javadocs.

The graph we're about to build is a small outtake of the social graph from The Matrix movie.

[edit] Brief API introduction

So we start with a GraphDatabaseService that you use to embed a Neo4j graph database in your application. From that instance you can f.ex. create and retrieve nodes.

The Node interface contains methods to get/set/remove properties on the node as well as get/create relationships to other nodes. You can also instantiate traversers which can traverse the graph in different ways.

The Relationship interface has the same methods as node for property manipulation. It also has methods for getting the start and end node. Relationships also have a type which is set when they are created.

This should actually be enough to get you going in this example.

[edit] Tasks

  • Open the org.neo4j.examples.workshop.tutor.Matrix class in the src/main/java folder and look for TODO Your code here... comments. The setupMatrix() method should create the graph from this example (You only need to set the "name" property for the persons, setting the rest of the properties in this graph is optional in this assignment):

Image:Matrix-social-network-jw-workshop.png

  • Implement the traversers in printFriends() and printHackers() methods. Once that is done you can open a console, goto the project directory and run scripts/matrix or "Run as->Java application" in Eclipse (shift+alt+x j). The output should be something like:
Thomas Anderson's friends:
At depth 1 => Trinity
At depth 1 => Morpheus
At depth 2 => Cypher
At depth 3 => Agent Smith
Hackers:
At depth 4 => The Architect

You may have to run the default target for build.xml (shift+alt+x q) if Eclipse in its infinite wisdom decided to put the classes somewhere other than build/classes. If you're using the Eclipse maven plugin it'll put the built classes in the folder specified by maven, probably target/classes and then you'll be fine as well.

Repeat this step until you get the output as above.

[edit] Assignment 2: Getting to know the shell

Now lets have look at the graph using the Neo4j shell. It is a simple command line utility that can be used to look at the graph and modify it. In org.neo4j.examples.workshop.tutor.matrix.MatrixWithShell class, you will see the matrix graph being setup and then one additional line of code for enabling the remote shell.

    graphDb.enableRemoteShell();

[edit] Tasks

  • Open a console, go to the project directory, compile with maven or ant and type scripts/matrix-with-shell. You should get a printout to the console looking something like Thomas Anderson is Node[1]. This means that the node representing Thomas Anderson has an id equal to 1.
  • Open a second console and type scripts/shell. This will start a shell to the running matrix example and the output should look like this:
NOTE: Connected to remote neo4j service 'shell' at port 1337
Welcome to the Neo4j Shell!Enter 'help' for a list of commands

neo4j-sh (0)$ 

Now remember the node id for Thomas Anderson and go to that node by issuing the command:

neo4j-sh (0)$ cd -a <node-id>

Where node-id should be replaced with the id of Thomas Anderson. The -a means "jump directly to this node, even if it isn't connected to the current node (in this case 0)". Now list the contents of the node by typing ls and you will get an output that looks something like:

neo4j-sh (1, Thomas Anderson)$ ls
*age  =[29]
*name =[Thomas Anderson]
(me) --<KNOWS>--> (2, Trinity)
(me) --<KNOWS>--> (3, Morpheus)
neo4j-sh (1, Thomas Anderson)$ 

This output means that the current node has two properties (name and age) and there are two outgoing relationships of type KNOWS connected to the nodes representing Trinity and Morpheus. Note that the name property is showing up as a title for each node. You can now cd to any of the connected nodes (without the -a option). Play around some with the shell and traverse the graph. You can type help to get a list of available commands and help command for more info about each command.

  • Now, using the shell, have Trinity create a relationship to a new node representing the character Tank such that an ls for Trinity would look something like:
neo4j-sh (2, Trinity)$ ls
*name =[Trinity]
(me) --<KNOWS>--> (7, Tank)
(me) <--<KNOWS>-- (1, Thomas Anderson)
(me) <--<KNOWS>-- (3, Morpheus)

To exit the shell type exit

[edit] Assignment 3: Introducing indexing

Now is the time to introduce external indexing, which is done using IndexService.

Indexing is used in places where you want to get a hold of a node with a certain property. This example extends the Matrix example in that it will index the name of all the persons so that if we'd like to get a certain person we wouldn't have to traverse the entire graph looking for it. This isn't really an issue in a tiny little graph like this one, but consider a graph with more than a million persons and you could see the benefits of such an index!

[edit] Tasks

  • Open the org.neo4j.examples.workshop.tutor.matrix.MatrixWithIndex class and implement the methods with a TODO in it. More specifically, look at the methods findPerson(String name) and indexPerson(Node person) and implement them according to the comments in those method stubs. Use the key "person.name" for the index key. Run it with the scripts/matrix-with-index script.
  • Change the personToStartFrom by looking up a different person than Thomas Anderson. How does that affect the output/traversal?

[edit] Assignment 4: Tree, or building an app with Neo4j

The purpose of this assignment is to get a feeling for how we can build applications using Neo4j. In the first asignment we looked at and worked with Neo4j API. That was very straight forward but moving from that to building real applications where we have a domain model, that somehow should be fitted into the Neo4j graph model, can be a bit tricky the first time. Fortunately we have found that object-oriented models fit very well in Neo4j's graph-oriented model.

[edit] Introducing the application

To illustrate this we use a simple example application that models a file system.

In a normal file system we have files and directories. A directory may have child directories or files. Both files and directories may have one parent directory. A simple way to model this in Java would be to take the Unix approach (where everything is a file) and have a single class, File.

Time for some, well, UML... ish.

File
 +getFileName() : String
 +getParent() : File
 +getChildren() : Iterable<File>
 +addChild( File child )
 +disconnectFromParent()
 +delete()

And for file creation we can have a factory:

FileFactory
 +createFile( String fileName )

Now, how do we make our file system persistent in Neo4j?Well what we have is a simple tree structure and a tree structure can be fitted into a graph structure. The simplest way to model this in Neo4j would be to have a single relationship type called FILE. Each node is a File entity and an incoming relationship of type FILE comes from the entity's parent. Outgoing relationships of type FILE point to the file entity's children (also effectively making that File a folder).

Time to pull out the ASCII art!Here's what it might look like in the graph:

(Node)---FILE--->(Node)----FILE--->(Node)
                    |
                    |------FILE--->(Node)

Merging our object model with the Neo4j graph model can then result in the following implementation. This might be a lot to comprehend at first glance. Worry not, it will be clear as you progress through the tasks:

[edit] Tasks

Under org.neo4j.examples.workshop.tutor.tree a partial implementation of this "file system" can be found.

  • Get the unit tests in the same package (but under src/test/java) to pass by implementing the TODO in File.java and FileFactory.java.
  • Use the console to invoke the script scripts/list-files (from the project root directory). It will build a simple file structure and then list the result to the console. That is, if your implementation is correct!
$ scripts/list-files
/bin
/boot
/dev
/etc
/etc/passwd
/etc/group
/home
/home/neo
/home/neo/application.jar
/home/neo/readme.txt
/lib
/usr
/sbin
/sbin/reboot
/sbin/fsck
/tmp

[edit] That's all folks

This marks the end of this workshop... however, if you feel that you've got more time on your hands please check out the optional/extra assignments as well!

The next step is to head over to the IMDB_Workshop.

Neo4j のサイト
ツールボックス