Reputation: 1117
I'm using the Movie example graph that comes with Neo4j and I want to get:
I am able to get 1 all labels using:
ResourceIterable<Label> allLabels = GlobalGraphOperations.at(graph).getAllLabels();
So, I get:
Movie
Person
I am also able to get 2 all relation types using:
Iterable<RelationshipType> allRelationshipTypes = GlobalGraphOperations.at(graph).getAllRelationshipTypes();
This are:
ACTED_IN
DIRECTED
PRODUCED
WROTE
FOLLOWS
REVIEWED
Finally, I can get all the properties, but not per label or per relation types, using:
ResourceIterable<String> allPropertyKeys = GlobalGraphOperations.at(graph).getAllPropertyKeys();
title
released
tagline
name
born
roles
summary
rating
What I am trying to get is:
Movie: [title, released, tagline]
Person: [name, born]
ACTED_IN: [roles]
DIRECTED: []
PRODUCED: []
WROTE: []
FOLLOWS: []
REVIEWED: [summary, rating]
Is it possible using the Java API or even executing Cypher queries? Please have in mind that this is something to be done in a graph with millions of nodes.
Upvotes: 3
Views: 2502
Reputation: 19211
To find all labels is easy in Neo4j. You have already solved it and it is a simple query in Cypher:
match (n) return distinct labels(n)
To find all relationship types is also easy using Cypher with the following query:
match (n)-[r]-() return distinct type(r)
But, for question number 3 and 4 it gets a lot tougher. Actually, there is no cost effective way of getting all properties for a Label or all properties for a relationship type.
Regarding getting all the properties for a Label. That implies that the properties belongs to the Label like for a schema (e.g. if you compare a DDL for RDBMS). There is currently no schema support of this type built in to Neo4j (the current version of Neo4j at the time of writing is 2.1.6) since Neo4j is basically schema free. This type of structural integrity is quite often handled in the application layer for NoSQL databases.
The only schema operations that are available today for Neo4j are described here.
Currently they include:
CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE
CREATE INDEX ON :Person(name)
If you want more structural enforcement the following may be of help:
In addition to this, there is also the tool NeoProfiler that contains a number of profilers, most of which run very simple Cypher queries against your database and provide summary statistics. Some profilers will actually discover data in your graph and then spawn other profilers which will run later. For example, if a label called "Person" is discovered in the data, a label profiler will be added to the run queue to inspect the population of nodes with that label.
In order to find all properties that "belongs to a Label" you will have to traverse all nodes of that type and extract the property names (which could be very costly).
The same goes for relationship types. Since they are not part of any schema it is an expensive operation to find all properties that are defined for a certain type (you will have to traverse all relationships).
Upvotes: 3