Skip to main content

Create a data model (5 mins)

Beta

The features described in this section are currently in beta testing and are subject to change. We recommend that you don't use the features in production systems.

For more information, request to join the data modeling group on the Cognite Hub.

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

Prerequisites

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

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

Create a data model with the CDF UI

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. Navigate to Cognite Data Fusion and select Explore and build > Flexible data modeling.

    info

    If you don't see the Flexible data modeling option, it has not been enabled for your CDF project. Contact your Cognite representative to enable flexible data modeling.

  2. Select the Create Data Model button and enter a name for the data model. In this example, we use the name MovieDM.

  3. Select Confirm to create both the data model and the external ID moviedm. We will use the external ID later when we populate the data model.

    New data model

    We use GraphQL as our data modeling language. A data model consists of multiple types, each with named properties. In this example, we'll create a data model that looks like this:

    New data model

    The model has two types: Movie and Actor. The Movie type has the properties name, description, watchedIt, imdbRating, releasedYear, runTime, gross, and actors. Note how the actors property is a list of references to the Actor type. This allows us to do advanced queries and filters later on.

    In the editor pane, you can toggle between creating the data model with a UI and entering GraphQL queries directly.

  4. Select the Add Type button to create the first type, and name it Actor.

  5. The first property of Actor is name of the type String. Select the Required checkbox to make this a required field.

  6. Select Add field to add an age property, and select the type Int. Now we have created the first of our two types.

  7. Select the back arrow next to Actor, and then add another type. Name it Movie and fill in the properties as shown below. To add the actors property, you must select the List button to show list types.

    Your data model should now look similar to this:

  8. Switch to the GraphQL editor again to see what the final data model looks like in the GraphQL language.

  9. To publish the data model, select the blue Publish button in the top right corner.

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):

    cdf data-models create --name="MovieDM"
  2. Next, define a GraphQL data model definition for the first version of the data model. In this example, we use a file named datamodel.graphql.

    datamodel.graphql
    type Actor {
    name: String!
    age: Int
    }

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

    cdf data-models publish --file=./datamodel.graphql --external-id=moviedm
  2. To make changes to the data model, update the datamodel.graphql file and rerun the command. For breaking changes, add the --allow-breaking-change parameter at the end of the command to publish a new version.

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