The Neo4j Manual V1.9 - Brown University

2y ago
11 Views
3 Downloads
3.88 MB
528 Pages
Last View : 21d ago
Last Download : 3m ago
Upload by : Kairi Hasson
Transcription

The Neo4j Manual v1.9.M04The Neo4j Team neo4j.org http://neo4j.org/ www.neotechnology.com http://www.neotechnology.com/

The Neo4j Manual v1.9.M04by The Neo4j Team neo4j.org http://neo4j.org/ www.neotechnology.com http://www.neotechnology.com/ Publication date 2013-01-17 17:29:44Copyright 2013 Neo TechnologyStarting points What is a graph database?Cypher Query LanguageUsing Neo4j embedded in Java applicationsUsing Neo4j embedded in Python applicationsRemote Client LibrariesLanguagesNeo4j ServerREST APILicense: Creative Commons 3.0This book is presented in open source and licensed through Creative Commons 3.0. You are free to copy, distribute, transmit, and/or adapt the work. Thislicense is based upon the following conditions:Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or youruse of the work).Share Alike.If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.Any of the above conditions can be waived if you get permission from the copyright holder.In no way are any of the following rights affected by the license: Your fair dealing or fair use rights The author’s moral rights Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rightsNoteFor any reuse or distribution, you must make clear to the others the license terms of this work. The best way to do this is with a direct link tothis page: http://creativecommons.org/licenses/by-sa/3.0/ http://creativecommons.org/licenses/by-sa/3.0/

Table of ContentsPreface . ivI. Introduction . 11. Neo4j Highlights . 22. Graph Database Concepts . 33. The Neo4j Graph Database . 11II. Tutorials . 204. Using Neo4j embedded in Java applications . 215. Neo4j Remote Client Libraries . 506. The Traversal Framework . 567. Data Modeling Examples . 658. Languages . 1019. Using Neo4j embedded in Python applications . 10210. Extending the Neo4j Server . 106III. Reference . 11311. Capabilities . 11412. Transaction Management . 12113. Data Import . 13014. Indexing . 13415. Cypher Query Language . 15316. Graph Algorithms . 23917. Neo4j Server . 24118. REST API . 25419. Python embedded bindings . 368IV. Operations . 38420. Installation & Deployment . 38521. Configuration & Performance . 39622. High Availability . 42723. Backup . 44424. Security . 44925. Monitoring . 455V. Tools . 46626. Web Administration . 46727. Neo4j Shell . 473VI. Community . 48928. Community Support . 49029. Contributing to Neo4j . 491A. Manpages . 515neo4j . 516neo4j-shell . 518neo4j-backup . 519B. Questions & Answers . 521iii

PrefaceThis is the reference manual for Neo4j version 1.9.M04, written by the Neo4j Team.The main parts of the manual are: Part I, “Introduction” — introducing graph database concepts and Neo4j.Part II, “Tutorials” — learn how to use Neo4j.Part III, “Reference” — detailed information on Neo4j.Part IV, “Operations” — how to install and maintain Neo4j.Part V, “Tools” — guides on tools.Part VI, “Community” — getting help from, contributing to.Appendix A, Manpages — command line documentation.Appendix B, Questions & Answers — common questions.The material is practical, technical, and focused on answering specific questions. It addresses howthings work, what to do and what to avoid to successfully run Neo4j in a production environment.The goal is to be thumb-through and rule-of-thumb friendly.Each section should stand on its own, so you can hop right to whatever interests you. When possible,the sections distill "rules of thumb" which you can keep in mind whenever you wander out of thehouse without this manual in your back pocket.The included code examples are executed when Neo4j is built and tested. Also, the REST API requestand response examples are captured from real interaction with a Neo4j server. Thus, the examples arealways in sync with Neo4j.Who should read this?The topics should be relevant to architects, administrators, developers and operations personnel.iv

Part I. IntroductionThis part gives a bird’s eye view of what a graph database is, and then outlines some specifics of Neo4j.

Chapter 1. Neo4j HighlightsAs a robust, scalable and high-performance database, Neo4j is suitable for full enterprise deploymentor a subset of the full server can be used in lightweight projects.It features: true ACID transactionshigh availabilityscales to billions of nodes and relationshipshigh speed querying through traversalsProper ACID behavior is the foundation of data reliability. Neo4j enforces that all operations thatmodify data occur within a transaction, guaranteeing consistent data. This robustness extends fromsingle instance embedded graphs to multi-server high availability installations. For details, seeChapter 12, Transaction Management.Reliable graph storage can easily be added to any application. A graph can scale in size andcomplexity as the application evolves, with little impact on performance. Whether starting newdevelopment, or augmenting existing functionality, Neo4j is only limited by physical hardware.A single server instance can handle a graph of billions of nodes and relationships. When datathroughput is insufficient, the graph database can be distributed among multiple servers in a highavailability configuration. See Chapter 22, High Availability to learn more.The graph database storage shines when storing richly-connected data. Querying is performed throughtraversals, which can perform millions of traversal steps per second. A traversal step resembles a joinin a RDBMS.2

Chapter 2. Graph Database ConceptsThis chapter contains an introduction to the graph data model and also compares it to other datamodels used when persisting data.3

Graph Database Concepts2.1. What is a Graph Database?A graph database stores data in a graph, the most generic of data structures, capable of elegantlyrepresenting any kind of data in a highly accessible way. Let’s follow along some graphs, using themto express graph concepts. We’ll “read” a graph by following arrows around the diagram to formsentences.2.1.1. A Graph contains Nodes and Relationships“A Graph —records data in Nodes —which have Properties”The simplest possible graph is a single Node, a record that has named values referred to as Properties.A Node could start with a single Property and grow to a few million, though that can get a littleawkward. At some point it makes sense to distribute the data into multiple nodes, organized withexplicit Relationships.Graphrecords dat a inrecords dat a inRelat ionshipsorganizeNodeshavehavePropert ies2.1.2. Relationships organize the Graph“Nodes —are organized by Relationships —which also have Properties”Relationships organize Nodes into arbitrary structures, allowing a Graph to resemble a List, a Tree,a Map, or a compound Entity – any of which can be combined into yet more complex, richly interconnected structures.2.1.3. Query a Graph with a Traversal“A Traversal —navigates a Graph; it —identifies Paths —which order Nodes”A Traversal is how you query a Graph, navigating from starting Nodes to related Nodes according toan algorithm, finding answers to questions like “what music do my friends like that I don’t yet own,”or “if this power supply goes down, what web services are affected?”4

Graph Database ConceptsTraversalnavigat esident ifiesGraphAlgorit hmrecords dat a inRelat ionshipsexpressesPat hsrecords dat a inorderorganizeNodes2.1.4. Indexes look-up Nodes or Relationships“An Index —maps from Properties —to either Nodes or Relationships”Often, you want to find a specific Node or Relationship according to a Property it has. Rather thantraversing the entire graph, use an Index to perform a look-up, for questions like “find the Account forusername master-of-graphs.”5

Graph Database ConceptsIndexesm ap t oRelat ionshipsm ap t oorganizehavem ap fromNodeshavePropert ies2.1.5. Neo4j is a Graph Database“A Graph Database —manages a Graph and —also manages related Indexes”Neo4j is a commercially supported open-source graph database. It was designed and built from theground-up to be a reliable database, optimized for graph structures instead of tables. Working withNeo4j, your application gets all the expressiveness of a graph, with all the dependability you expectout of a database.6

Graph Database ConceptsGraph Dat abaseTraversalm anagesm anagesIndexesnavigat esident ifiesGraphm ap t om ap t oAlgorit hmrecords dat a inRelat ionshipsm ap fromrecords dat a inorganizehaveexpressesNodeshavePropert ies7Pat hsorder

Graph Database Concepts2.2. Comparing Database ModelsA Graph Database stores data structured in the Nodes and Relationships of a graph. How does thiscompare to other persistence models? Because a graph is a generic structure, let’s compare how a fewmodels would look in a graph.2.2.1. A Graph Database transforms a RDBMSTopple the stacks of records in a relational database while keeping all the relationships, and you’ll seea graph. Where an RDBMS is optimized for aggregated data, Neo4j is optimized for highly connecteddata.Figure 2.1. RDBMSA1B1A2B2A3B3B4B5C1B6C2B7C3Figure 2.2. Graph Database as RDBMSC3A1A2C1B2B6B1B4B5A3C2B3B72.2.2. A Graph Database elaborates a Key-Value StoreA Key-Value model is great for lookups of simple values or lists. When the values are themselvesinterconnected, you’ve got a graph. Neo4j lets you elaborate the simple data structures into morecomplex, interconnected data.8

Graph Database ConceptsFigure 2.3. Key-Value StoreV1K2K1K2V2K3K1K3V3K1K*represents a key, V* a value. Note that some keys point to other keys as well as plain values.Figure 2.4. Graph Database as Key-Value StoreK2K1V2V1V3K32.2.3. A Graph Database relates Column-FamilyColumn Family (BigTable-style) databases are an evolution of key-value, using "families" to allowgrouping of rows. Stored in a graph, the families could become hierarchical, and the relationshipsamong data becomes explicit.2.2.4. A Graph Database navigates a Document StoreThe container hierarchy of a document database accommodates nice, schema-free data that can easilybe represented as a tree. Which is of course a graph. Refer to other documents (or document elements)within that tree and you have a more expressive representation of the same data. When in Neo4j, thoserelationships are easily navigable.9

Graph Database ConceptsFigure 2.5. Document StoreD1D2S1D2/S2S3V1D1/S1D Document, S Subdocument, V Value, D2/S2S2V4V3V2 reference to subdocument in (other) document.Figure 2.6. Graph Database as Document StoreV1D1V2D2S3S1S2V3V410

Chapter 3. The Neo4j Graph DatabaseThis chapter goes into more detail on the data model and behavior of Neo4j.11

The Neo4j Graph Database3.1. NodesThe fundamental units that form a graph are nodes and relationships. In Neo4j, both nodes andrelationships can contain properties.Nodes are often used to represent entities, but depending on the domain relationships may be used forthat purpose as well.A Nodecan haveRelat ionshipscan havecan havePropert iesLet’s start out with a really simple graph, containing only a single node with one property:nam e: Marko12

The Neo4j Graph Database3.2. RelationshipsRelationships between nodes are a key part of a graph database. They allow for finding related data.Just like nodes, relationships can have properties.A Relat ionshiphas aSt art nodehas ahas aEnd nodecan haveRelat ionship t ypePropert iesuniquely ident ified byNam eA relationship connects two nodes, and is guaranteed to have valid start and end nodes.St art noderelat ionshipEnd nodeAs relationships are always directed, they can be viewed as outgoing or incoming relative to a node,which is useful when traversing the graph:incom ing relat ionshipNodeout going relat ionshipRelationships are equally well traversed in either direction. This means that there is no need to addduplicate relationships in the opposite direction (with regard to traversal or performance).While relationships always have a direction, you can ignore the direction where it is not useful in yourapplication.Note that a node can have relationships to itself as well:NodeloopTo further enhance graph traversal all relationships have a relationship type. Note that the word typemight be misleading here, you could rather think of it as a label. The following example shows asimple social network with two relationship types.13

The Neo4j Graph illiamUsing relationship direction and typeWhatHowget who a person followsoutgoing follows relationships, depth oneget the followers of a personincoming follows relationships, depth oneget who a person blocksoutgoing blocks relationships, depth oneget who a person is blocked byincoming blocks relationships, depth oneThis example is a simple model of a file system, which includes symbolic links:/AfilefileBCsym bolic link{ nam e: " E" }fileDDepending on what you are looking for, you will use the direction and type of relationships duringtraversal.14

The Neo4j Graph DatabaseWhatHowget the full path of a fileincoming file relationshipsget all paths for a fileincoming file and symbolic link relationshipsget all files in a directoryoutgoing file and symbolic link relationships,depth oneget all files in a directory, excluding symboliclinksoutgoing file relationships, depth oneget all files in a directory, recursivelyoutgoing file and symbolic link relationships15

The Neo4j Graph Database3.3. PropertiesBoth nodes and relationships can have properties.Properties are key-value pairs where the key is a string. Property values can be either a primitive or anarray of one primitive type. For example String, int and int[] values are valid for properties.Notenullis not a valid property value. Nulls can instead be modeled by the absence of a key.A Propert yhas aValuehas aKeycan be an array of can be ais abooleanbyt eshortintPrim it ivelongfloatdoublecharSt ringProperty value typesTypeDescriptionValue rangetrue/falsebooleanbyte8-bit integer-128short16-bit integer-32768int32-bit integer-2147483648long64-bit integer-9223372036854775808 to9223372036854775807, inclusivefloat32-bit IEEE 754 floating-point numberdouble64-bit IEEE 754 floating-point number16to 127, inclusiveto 32767, inclusiveto 2147483647, inclusive

The Neo4j Graph DatabaseTypeDescriptionValue rangechar16-bit unsigned integers representingUnicode charactersu0000Stringsequence of Unicode charactersto uffff (0 to 65535)For further details on float/double values, see Java Language Specification /typesValues.html#4.2.3 .17

The Neo4j Graph Database3.4. PathsA path is one or more nodes with connecting relationships, typically retrieved as a query or traversalresult.A Pat hhas a can cont ain one or m oreSt art NodeRelat ionshipaccom panied by aNodeThe shortest possible path has length zero and looks like this:NodeA path of length one:Node 1Relat ionship 1Node 2Another path of length one:Node 1Relat ionship 118has anEnd Node

The Neo4j Graph Database3.5. TraversalTraversing a graph means visiting its nodes, following relationships according to some rules. In mostcases only a subgraph is visited, as you already know where in the graph the interesting nodes andrelationships are found.Neo4j comes with a callback based traversal API which lets you specify the traversal rules. At a basiclevel there’s a choice between traversing breadth- or depth-first.For an in-depth introduction to the traversal framework, see Chapter 6, The Traversal Framework. ForJava code examples see Section 4.5, “Traversal”.Other options to traverse or query graphs in Neo4j are Cypher and Gremlin.19

Part II. TutorialsThe tutorial part describes how to set up your environment, and write programs using Neo4j. It takes youfrom Hello World to advanced usage of graphs.

Chapter 4. Using Neo4j embedded in Java applicationsIt’s easy to use Neo4j embedded in Java applications. In this chapter you will find everythingneeded — from setting up the environment to doing something useful with your data.21

Using Neo4j embedded in Java applications4.1. Include Neo4j in your projectAfter selecting the appropriate edition for your platform, embed Neo4j in your Java application byincluding the Neo4j library jars in your build. The following sections will show how to do this byeither altering the build path directly or by using dependency management.4.1.1. Add Neo4j to the build pathGet the Neo4j libraries from one of these sources: Extract a Neo4j download http://neo4j.org/download/ zip/tarball, and use the jar files found inthe lib/ directory. Use the jar files available from Maven Central Repository http://search.maven.org/#search ga 1 g%3A%22org.neo4j%22 Add the jar files to your project:JDK toolsAppend to -classpathEclipse Right-click on the project and then go Build Path Configure Build Path. In the dialog,choose Add External JARs, browse to the Neo4j lib/ directory and select all of the jar files. Another option is to use User Libraries http://help.eclipse.org/indigo/index.jsp?topic va/buildpath/ref-preferences-userlibraries.htm .IntelliJ IDEASee Libraries, Global Libraries, and the Configure Library dialog ml NetBeans Right-click on the Libraries node of the project, choose Add JAR/Folder, browse to the Neo4jlib/ directory and select all of the jar files. You can also handle libraries from the project node, see Managing a Project’s Classpath l#projects-classpath .4.1.2. Add Neo4j as a dependencyFor an overview of the main Neo4j artifacts, see Neo4j editions. The artifacts listed there are top-levelartifacts that will transitively include the actual Neo4j implementation. You can either go with the toplevel artifact or include the individual components directly. The examples included here use the toplevel artifact approach.MavenMaven dependency. project . dependencies dependency groupId org.neo4j /groupId artifactId neo4j /artifactId version 1.9.M04 /version /dependency .22

Using Neo4j embedded in Java applications /dependencies . /project Where the artifactId is found in Neo4j editions.Eclipse and MavenFor development in Eclipse http://www.eclipse.org , it is recommended to install the m2e plugin http://www.eclipse.org/m2e/ and let Maven manage the project build classpath instead, see above.This also adds the possibility to build your project both via the command line with Maven and have aworking Eclipse setup for development.IvyMake sure to resolve dependencies from Maven Central, for example using this configuration in yourivysettings.xml file: ivysettings settings defaultResolver "main"/ resolvers chain name "main" filesystem name "local" artifact pattern " ].[ext]" / /filesystem ibiblio name "maven central" root "http://repo1.maven.org/maven2/" m2compatible "true"/ /chain /resolvers /ivysettings With that in place you can add Neo4j to the mix by having something along these lines to your ivy.xmlfile:. dependencies . dependency org "org.neo4j" name "neo4j" rev "1.9.M04"/ . /dependencies .Where the name is found in Neo4j editions.GradleThe example below shows an example gradle build script for including the Neo4j libraries.def neo4jVersion "1.9.M04"apply plugin: 'java'repositories {mavenCentral()}dependencies {compile "org.neo4j:neo4j: {neo4jVersion}"}Where the coordinates (org.neo4j:neo4j in the example) are found in Neo4j editions.4.1.3. Starting and stoppingTo create a new database or ópen an existing one you instantiate an EmbeddedGraphDatabase org/neo4j/kernel/EmbeddedGraphDatabase.html .graphDb new GraphDatabaseFactory().newEmbeddedDatabase( DB PATH );23

Using Neo4j embedded in Java applicationsregisterShutdownHook( graphDb );NoteThe EmbeddedGraphDatabase instance can be shared among multiple threads. Note howeverthat you can’t create multiple instances pointing to the same database.To stop the database, call the shutdown() method:graphDb.shutdown();To make sure Neo4j is shut down properly you can add a shutdown hook: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 example before it's completed)Runtime.getRuntime().addShutdownHook( new Thread(){@Overridepublic void run(){graphDb.shutdown();}} );}If you want a read-only view of the database, use EmbeddedReadOnlyGraphDatabase l .To start Neo4j with configuration settings, a Neo4j properties file can be loaded like this:GraphDatabaseService graphDb new GraphDatabaseFactory().newEmbeddedDatabaseBuilder( "target/database/location" ).loadPropertiesFromFile( pathToConfig "neo4j.properties" ).newGraphDatabase();Or you could of course create you own Map String, String programatically and use that instead.For configuration settings, see Chapter 21, Configuration & Performance.24

Using Neo4j embedded in Java applications4.2. Hello WorldLearn how to create and access nodes and relationships. For information on project setup, seeSection 4.1, “Include Neo4j in your project”.Remember, from Section 2.1, “What is a Graph Database?”, that a Neo4j graph consist of: Nodes that are connected by Relationships, with Properties on both nodes and relationships.All relationships have a type. For example, if the graph represents a social network, a relationship typecould be KNOWS. If a relationship of the type KNOWS connects two nodes, that probably represents twopeople that know each other. A lot of the semantics (that is the meaning) of a graph is encoded in therelationship types of the application. And although relationships are directed they are equally welltraversed regardless of which direction they are traversed.TipThe source code of this example is found here: EmbeddedNeo4j.java ples/EmbeddedNeo4j.java 4.2.1. Prepare the databaseRelationship types can be created by using an enum. In this example we only need a single relationshiptype. This is how to define it:private static enum RelTypes implements RelationshipType{KNOWS}We also prepare some variables to use:GraphDatabaseService graphDb;Node firstNode;Node secondNode;Relationship relationship;The next step is to start the database server. Note that if the directory given for the database doesn’talready exist, it will be created.graphDb new GraphDatabaseFactory().newEmbeddedDatabase( DB PATH );registerShutdownHook( graphDb );Note that starting a database server is an expensive operation, so don’t start up a new instanceevery time you need to interact with the database! The instance can be shared by multiple threads.Transactions are thread confined.As seen, we register a shutdown hook that will make sure the database shuts down when the JVMexits. Now it’s time to interact with the database.4.2.2. Wrap writes in a transactionAll writes (creating, deleting and updating any data) have to be performed in a transaction. This isa conscious design decision, since we believe transaction demarcation to be an important part ofworking with a real enterprise database. Now, transaction handling in Neo4j is very easy:Transaction tx graphDb.beginTx();25

Using Neo4j embedded in Java applicationstry{// Updating operations go heretx.success();}finally{tx.finish();}For more information on transactions, see Chapter 12, Transaction Management and JavaAPI for Transaction org/neo4j/graphdb/Transaction.html .4.2.3. Create a small graphNow, let’s create a few nodes. The API is very intuitive. Feel free to have a look at the JavaDocs s/. They’re included in the distribution, as well.Here’s how to create a small graph consisting of two nodes, connected with one relationship and someproperties: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 " );We now have a graph that looks like this:Figure 4.1. Hello World Graphm essage 'Hello, 'KNOWSm essage 'brave Neo4j 'm essage 'World! '4.2.4. Print the resultAfter we’ve created our graph, let’s read from it and print the result.System.out.print( firstNode.getProperty( "message" ) );System.out.print( relationship.getProperty( "message" ) );System.out.print( secondNode.getProperty( "message" ) );Which will output:Hello, brave Neo4j World!4.2.5. Remove the dataIn this case we’ll remove t

This is the reference manual for Neo4j version 1.9.M04, written by the Neo4j Team. The main parts of the manual are: Part I, “Introduction”—introducing graph database concepts and Neo4j. Part II, “Tutorials”—learn how to use Neo4j. Part III, “Reference”—detailed information on Neo4j.

Related Documents:

Neo4j i About the Tutorial Neo4j is one of the popular Graph Databases and Cypher Query Language (CQL). Neo4j is written in Java Language. This tutorial explains the basics of Neo4j, Java with Neo4j, and Spring DATA with Neo4j. The tutorial is divided into sections such as Neo4j Introduction, Neo4j CQL, Neo4j CQL Functions, Neo4j Admin, etc.File Size: 1MB

May 02, 2018 · D. Program Evaluation ͟The organization has provided a description of the framework for how each program will be evaluated. The framework should include all the elements below: ͟The evaluation methods are cost-effective for the organization ͟Quantitative and qualitative data is being collected (at Basics tier, data collection must have begun)

Silat is a combative art of self-defense and survival rooted from Matay archipelago. It was traced at thé early of Langkasuka Kingdom (2nd century CE) till thé reign of Melaka (Malaysia) Sultanate era (13th century). Silat has now evolved to become part of social culture and tradition with thé appearance of a fine physical and spiritual .

On an exceptional basis, Member States may request UNESCO to provide thé candidates with access to thé platform so they can complète thé form by themselves. Thèse requests must be addressed to esd rize unesco. or by 15 A ril 2021 UNESCO will provide thé nomineewith accessto thé platform via their émail address.

̶The leading indicator of employee engagement is based on the quality of the relationship between employee and supervisor Empower your managers! ̶Help them understand the impact on the organization ̶Share important changes, plan options, tasks, and deadlines ̶Provide key messages and talking points ̶Prepare them to answer employee questions

Dr. Sunita Bharatwal** Dr. Pawan Garga*** Abstract Customer satisfaction is derived from thè functionalities and values, a product or Service can provide. The current study aims to segregate thè dimensions of ordine Service quality and gather insights on its impact on web shopping. The trends of purchases have

Bases de données graphes : comparaison de NEO4J et OrientDB Nicolas Vergnes 2 Neo4J (version 2.1.7) 2.1 Présentation Neo4J est un SGBD orienté graphe de la famille NoSQL crée en 2000 par la société Neo Technology. Neo4J est sous licence GPLv3 sous la dénomination de Community Edition. La version commerciale sera abordée dans le chapitre 2.3

Nutrition and Food Science [CODE] SPECIMEN PAPER Assessment Unit A2 1 assessing. 21 Option A: Food Security and Sustainability or Option B: Food Safety and Quality. 22 Option A: Food Security and Sustainability Quality of written communication will be assessed in all questions. Section A Answer the one question in this section. 1 (a) Outline the arguments that could be used to convince .