Query and analyze a graph
Updated at:
After you connect to a Graph Database (GDB) instance, you can query and analyze your graph using Data Management (DMS) or the open source GDB Console.
Graph Database supports two query languages: Gremlin and OpenCypher. To query and analyze your graph, choose the query language that corresponds to your instance's engine.
Prerequisites
You have logged on to the database as described in the Quick Start. For more information, see Log on to a database.
Gremlin
The following example statements are for GDB instances that use the Gremlin engine.
Vertex queries
- Count the number of vertices.
g.V().count() - Count the number of vertices of each type.
g.V().group().by(label).by(count()) g.V().groupCount().by(label) - Query all vertices.
g.V().limit(100) - Query vertices with a specific label.
g.V().hasLabel('person') - Query a vertex by its ID.
g.V('123') - Filter vertices by property.
g.V().has('name','Zhang San')
Edge queries
- Count the number of edges.
g.E().count() - Count the number of edges of each type.
g.E().group().by(label).by(count()) - Query all edges.
g.E() - Query edges with a specific label.
g.E().hasLabel('connect') - Query an edge by its ID.
g.E('12') - Query the edges associated with a vertex.
g.V('123').outE('connect') - Query a path.
g.V('123').outE('connect').path()
Property queries
- Query the properties of a vertex.
g.V('123').valueMap(true)
Sort queries
- Sort by a property field.
g.V().has('name','Zhang San').outE('connect').order().by('weight', decr).valueMap(true)
Add a vertex
- Add a vertex.
g.addV('person').property(id,'23').property('name','Li Si')
Delete vertices
- Delete a specific vertex.
g.V('123').drop() - Delete vertices by property.
g.V().has('person').property('age', values('18')).drop() - Delete all vertices in batches.
g.V().drop().limit(1024)
Add an edge
- Add an edge.
g.addE('connect').property('weight', '4').from(g.V('12')).to(g.V('16'))
Delete an edge
- Delete an edge.
g.E('23').drop()
OpenCypher
The following example statements are for GDB instances that use the OpenCypher engine.
Vertex queries
- Count the number of vertices.
MATCH (n) return count(n) - Count the number of vertices of each type.
MATCH (n) return labels(n), count(*) - Query all vertices (not recommended).
MATCH (n) return n limit 10 - Query vertices with a specific label.
MATCH (n:person) return n - Filter vertices by property.
MATCH (n:person {firstName: "Carlos"}) return n;
Edge queries
- Count the number of edges.
MATCH (n)-[r]->(m) return count(r) - Count the number of edges of each type.
MATCH (n)-[r]->(m) return type(r), count(*) - Query all edges.
MATCH (n)-[r]->(m) return r - Query edges with a specific label.
MATCH (n)-[r:knows]->(m) return r - Query edges by edge property.
MATCH (n)-[r:knows {creationDate:1274809046847}]->(m) return r - Query the edges associated with a vertex.
MATCH (n:person {firstName: "Carlos"})-[r]->(m) return r - Query a path.
MATCH p=(n:person {firstName: "Carlos"})-[r]->(m) return p
Property queries
- Query the properties of a vertex.
MATCH (n:person {firstName: "Carlos"}) return properties(n) limit 2;
Sort queries
- Sort by a property field.
MATCH (n:person {firstName: "Mahinda"})-[r]-(m) return m order by m.locationIP
Add a vertex
- Add a vertex.
CREATE (n:Person {name: 'Andy', title: 'Developer'})
Delete vertices
- Delete a specific vertex.
MATCH (n:Person {name: 'tom'}) DELETE n - Remove a property from a vertex.
MATCH (a {name: 'Andy'}) REMOVE a.age RETURN a - Delete vertices in batches.
MATCH (n:Person ) DELETE n LIMIT 10
Add an edge
- Add an edge.
MATCH (a:Person), (b:Person) WHERE a.name = 'A' AND b.name = 'B' CREATE (a)-[r:RELTYPE]->(b) RETURN type(r)
Delete an edge
- Delete an edge.
MATCH (n {name: 'Andy'})-[r:KNOWS]->() DELETE rg.E('23').drop()
Is this page helpful?