Skip to main content

Create a data model (5 mins)

This unit describes how to create and manage data models using the CDF portal application or from the command line with the CDF CLI.

Prerequisites

Before creating a data model, complete the steps in the Configure access to data models article.

This guide assumes you have followed the steps to upload a demo data set into the CDF staging area.

The data model

A data model consists of multiple types, each with named fields. In this example, we'll create a data model that looks like this:

Visual representation of data model

The model has three types: Movie, Person, and Actor with fields describing the attributes of each type.

  • The Person type has the fields name and age, which acts as an interface.
  • The Actor and Director type implements Person with the additional didWinOscar field.
  • The Movie type has the fields name, description, watchedIt, imdbRating, releasedYear, runTime, gross, director, and actors.

Required fields are marked with an ! next to the field name and you can't create an instance without a value for this field.

Interfaces

GraphQL uses interfaces to allow other types/interfaces to inherit from them. In this case, Person is an interface. You need to repeat all fields from the interface you inherit from, and Actor therefore explicitly includes the name and age fields.

The Movie type illustrates a more complex example of relations. The director field is a 1-1 relation to a Director type, and the actors field is a list of Actor types. In a later section, you'll see how this lets us do advanced queries and filters.

You'll see the difference between these two kinds of relations during data population, when we'll use the list to create edges (a new "relations" instance pointing from and to instances of Movie and Actor). director is simpler and only requires a reference to the node of type Director.

Create a data model in the CDF portal application

Follow the steps in the example below to create a MovieDM data model to hold data for an application that keeps track of movies you have watched and who played in them.

  1. Navigate to Cognite Data Fusion and select Explore and build > Data models.

  2. Select the Create a Data Model button and enter a name for the data model. In this example, we use the name MovieDM. You will see that a suggested externalId is generated based on the data model name. Add an optional description if you want to.

  3. Specify the space where your data model and types will live. You can reuse an existing space, or create a new one.

    info

    The types in your data model are unique in the entire space. If Movie already exists as a type in the space you select, you must reuse the existing type to include Movie in your data model.

  4. Select Create to create the data model.

    New data model
  5. To define the data model, copy this GraphQL code into the code editor:

    datamodel.graphql
    type Actor implements Person {
    name: String!
    age: Int
    didWinOscar: Boolean
    }

    type Director implements Person {
    name: String!
    age: Int
    didWinOscar: Boolean
    }

    interface Person {
    name: String!
    age: Int
    }

    type Movie {
    name: String!
    description: String
    watchedIt: Boolean
    imdbRating: Float
    releasedYear: Int
    gross: Int
    runTime: Int
    director: Director
    actors: [Actor]
    }
  6. To publish the data model, select the blue Publish button in the top right corner and publish the data model as version 1.

In the next unit, you'll learn how to populate the data model with data.

Create a data model with the CDF CLI

The Cognite Data Fusion (CDF) CLI allows you to manage data models from the command line. Follow the steps in the CDF CLI documentation to install and sign in using the CDF CLI.

Follow the steps in the example below to create a MovieDM data model to hold data for an application that keeps track of movies you have watched.

  1. To create the data model, open a terminal and run this command (the external ID of this model will be MovieDM, under a space of the same name):
cdf data-models create --name="MovieDM" --externalId="MovieDM" --space="MovieDM"
  1. Next, define a GraphQL data model definition for the first version of the data model. This example uses a file named datamodel.graphql.

    datamodel.graphql
    type Actor implements Person {
    name: String!
    age: Int
    didWinOscar: Boolean
    }

    type Director implements Person {
    name: String!
    age: Int
    didWinOscar: Boolean
    }

    interface Person {
    name: String!
    age: Int
    }

    type Movie {
    name: String!
    description: String
    watchedIt: Boolean
    imdbRating: Float
    releasedYear: Int
    gross: Int
    runTime: Int
    director: Director
    actors: [Actor]
    }
  1. Publish the definition from datamodel.graphql as version 1 (v1) of the MovieDM data model in MovieDM space.

    cdf data-models publish --file=./datamodel.graphql --external-id=MovieDM --version=1 --space=MovieDM

In the next section, you'll learn how to populate the data model with data.