Skip to main content

Query data from a data model (3 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 article describes how to access the data in your data model via Query Explorer and how to use the query builder to create GraphQL queries. For more information about GraphQL, refer to the Learn GraphQL guide.

Prerequisites

Before you can query the data, make sure that you have completed the steps in these articles:

  1. Upload data to RAW.
  2. Create a data model.
  3. Populate the data model.

Querying data in the query explorer

  1. Navigate to Cognite Data Fusion and select Explore and build > Flexible data modeling. Then select the data model you want to query, for example the MoviesDB data model we made earlier.

  2. Select Query Explorer.

  3. Select the Explorer button to see the available query builder features, and use Query Explorer to query data from the data model.

List query

There are two main options to find data with Query Explorer. You can list data, potentially with filters and sorting, or you can search (also with filters and sort). See the next sections for a few examples based on the data model we created earlier.

List movies

  1. Select listMovies from Query Explorer to start a query to list movies.

  2. In GraphQL, you can specify which fields of a query you want to return. As a start, let's list the movie titles and their released year.

    In the query, expand items and select name and releasedYear.

  3. To run the query, select the Play button or press Ctrl+Enter.

    The final query should look similar to this:

    query Movies {
    listMovie(first: 10) {
    items {
    name
    }
    }
    }

Filter movies on released year

  1. To find movies that were released before the year 2000, enter this query:

    query OldMovies {
    listMovie(first: 10, filter: { releasedYear: { lt: 2000 } }) {
    items {
    name
    releasedYear
    imdbRating
    }
    }
    }

Filter top rated movies on released year (combining multiple filters)

  1. To find the top rated movies released before year 2000, enter this query:

    query GoodOldMovies {
    listMovie(
    first: 10
    filter: {
    and: [{ releasedYear: { lt: 2000 } }, { imdbRating: { gt: 7.8 } }]
    }
    ) {
    items {
    name
    releasedYear
    imdbRating
    }
    }
    }

Include the actors in the query

  1. To include the actors we created as edges, enter this query:

    query MoviesAndActors {
    listMovie(first: 10) {
    items {
    name
    releasedYear
    imdbRating
    actors {
    items {
    name
    }
    }
    }
    }
    }

Search movies

  1. To find data through searching, you can write a query like this:

    query SearchMovie {
    searchMovie(fields: description, query: "love") {
    items {
    name
    description
    actors {
    items {
    name
    }
    }
    }
    }
    }

Access the data via a URL

You can use any application to directly access the data in the data model as long as you have an authenticated token to pass in the Authentication field in the header.

For instance, you can call this URL: https://<cluster>.cognitedata.com/api/v1/projects/<project>/schema/api/<data model external id>/<version>/graphql

Where:

  • cluster - The CDF cluster, for example api)
  • project - The CDF project (for example demo)
  • data model external id - The external ID of your data model, for example MoviesDM)
  • version - The data model version you want to use, for example 1.

An example for a full URL is https://api.cognitedata.com/api/v1/projects/publicdata/schema/api/modeldm/1/graphql

Access data with the JavaScript SDK

To get started with the JavaScript SDK, see the JavaScript Getting started guide.

When you have authenticated the SDK, you can run the following code with the same URL as described above:

// Authenticate the `SDK` CogniteClient instance

const PROJECT = `publicdata`;
const EXTERNAL_ID = `moviesdm`;
const VERSION = `1`;

const query = {
query: `
query MoviesAndActors {
listMovie {
items {
actors {
items {
age
name
}
}
name
watchedIt
}
}
}
`,
},

sdk
.post(
`/api/v1/projects/${PROJECT}/schema/api/${EXTERNAL_ID}/${VERSION}/graphql`,
{
data: query
}
)
.then((data) => {
// ... do something with the `data`
});

Access data with the Python SDK

To get started with the Python SDK, see the Python Getting started guide.

When you have authenticated the SDK, you can run the following code with the same URL as described above:

# Authenticate the `SDK` CogniteClient instance

PROJECT = "xxx"
EXTERNAL_ID = "moviesdm" # Important: This must be lower case
VERSION = "1"
query = {
"query": """
query MoviesAndActors {
listMovie {
items {
actors {
items {
age
name
}
}
name
watchedIt
}
}
}
"""
}
response = client.post(f"/api/v1/projects/{PROJECT}/schema/api/{EXTERNAL_ID}/{VERSION}/graphql", json=query)

# ... do something with the `response.content`