meta_util

A module that contains procedures describing graphs on a meta-level.

TraitValue
Module typeutil
ImplementationPython
Parallelismsequential

Procedures

schema()

Knowing what kind of data, that is, what kind of nodes and relationships, are stored inside the database and how they're connected can be helpful. Besides that, each node or relationship can have a set of properties, and while loading the data in the database, you should be sure that a certain amount of graph objects has a particular property. That's where the number of graph objects with a particular property (property count) might come in handy.

The schema() procedure returns a list of distinct relationships connecting distinct nodes, that is, a graph schema. If include_properties is set to true, the graph schema will contain additional information about properties.

Input:

  • include_properties: bool (default=false) ➡ If set to true, the graph schema will include properties count information.

Output:

  • nodes: List[Map] ➡ List of distinct node objects with their count. If include_properties is set to true, the node object contains properties count too.
  • relationships: List[Map] ➡ List of distinct relationship objects with their count. If include_properties is set to true, the relationship object contains properties count too.

Usage:

Get graph schema without properties count:

CALL meta_util.schema() 
YIELD nodes, relationships 
RETURN nodes, relationships;

Get graph schema with properties count:

CALL meta_util.schema(true) 
YIELD nodes, relationships 
RETURN nodes, relationships;

The queries above will return results in the graph view only in Memgraph Lab versions >= 2.4.0. For earlier versions of the Memgraph Lab, call UNWIND on returned object properties nodes and edges.

Example

Create a graph

Create a graph by running the following Cypher query:

CREATE (n:Person {name: "Kate", age: 27})-[:IS_FRIENDS_WITH]->(m:Person:Student {name: "James", age: 30, year: "second"})-[:STUDIES_AT]->(:University {name: "University of Vienna"})
WITH n, m
CREATE (n)-[:LIVES_IN]->(:City {name: "Zagreb"})<-[:LIVES_IN]-(m);

Create a schema

Once the graph is created, run the following query to call the schema procedure:

CALL meta_util.schema() 
YIELD nodes, relationships 
RETURN nodes, relationships;

The graph result of the schema procedure in Memgraph Lab:

Memgraph Lab can also return data results - a list of nodes and a list of relationships:

[
   {
      "id": 0,
      "labels": [
         "Person"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   },
   {
      "id": 1,
      "labels": [
         "Person",
         "Student"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   },
   {
      "id": 2,
      "labels": [
         "University"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   },
   {
      "id": 3,
      "labels": [
         "City"
      ],
      "properties": {
         "count": 1
      },
      "type": "node"
   }
]

Here is the obtained list of relationships:

[
   {
      "end": 1,
      "id": 0,
      "label": "IS_FRIENDS_WITH",
      "properties": {
         "count": 1
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 1,
      "label": "LIVES_IN",
      "properties": {
         "count": 1
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 2,
      "id": 2,
      "label": "STUDIES_AT",
      "properties": {
         "count": 1
      },
      "start": 1,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 3,
      "label": "LIVES_IN",
      "properties": {
         "count": 1
      },
      "start": 1,
      "type": "relationship"
   }
]

Create a schema including the properties count

Run the following code to call the schema procedure including the properties count:

CALL meta_util.schema(true) 
YIELD nodes, relationships 
RETURN nodes, relationships;

The graph result of the schema procedure seen in Memgraph Lab:

Graph Result Graph Result Count

Memgraph Lab can also return data results - a list of nodes and a list of relationships:

[
   {
      "id": 0,
      "labels": [
         "Person"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "age": 1,
            "name": 1
         }
      },
      "type": "node"
   },
   {
      "id": 1,
      "labels": [
         "Person",
         "Student"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "age": 1,
            "name": 1,
            "year": 1
         }
      },
      "type": "node"
   },
   {
      "id": 2,
      "labels": [
         "University"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "name": 1
         }
      },
      "type": "node"
   },
   {
      "id": 3,
      "labels": [
         "City"
      ],
      "properties": {
         "count": 1,
         "properties_count": {
            "name": 1
         }
      },
      "type": "node"
   }
]

Here is the obtained list of relationships:

[
   {
      "end": 1,
      "id": 0,
      "label": "IS_FRIENDS_WITH",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 1,
      "label": "LIVES_IN",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 0,
      "type": "relationship"
   },
   {
      "end": 2,
      "id": 2,
      "label": "STUDIES_AT",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 1,
      "type": "relationship"
   },
   {
      "end": 3,
      "id": 3,
      "label": "LIVES_IN",
      "properties": {
         "count": 1,
         "properties_count": {}
      },
      "start": 1,
      "type": "relationship"
   }
]