{
"cells": [
{
"cell_type": "markdown",
"id": "97698baf-35d1-422f-a962-1e28ad07bdb6",
"metadata": {},
"source": [
"# Querying Data Modeling\n",
"\n",
"This tutorial demonstrates how to query a data model in Cognite Data Fuction (CDF) with Python \n",
"\n",
"**Prerequisites**\n",
"\n",
"* Installed `cognite-sdk`\n",
"* Setup a service principal to connect to CDF.\n",
"* Use `cognite-toolkit` quickstart to setup configuration and populated the `CogniteCore` Model [Toolkit quickstart](https://docs.cognite.com/cdf/deploy/cdf_toolkit/references/packages/quickstart)"
]
},
{
"cell_type": "markdown",
"id": "6268a78f-e8de-482f-832b-20d28333a3b1",
"metadata": {},
"source": [
"## Setting up a cognite client"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3d0b28eb-87fe-453e-9cad-a7f5d8c3fb90",
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-09T13:13:17.005946Z",
"start_time": "2024-11-09T13:13:16.981218Z"
}
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from cognite.client import CogniteClient\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv(\"../../.env\")\n",
"\n",
"client = CogniteClient.default_oauth_client_credentials(\n",
" project=os.environ[\"CDF_PROJECT\"],\n",
" cdf_cluster=os.environ[\"CDF_CLUSTER\"],\n",
" tenant_id=os.environ[\"IDP_TENANT_ID\"],\n",
" client_id=os.environ[\"IDP_CLIENT_ID\"],\n",
" client_secret=os.environ[\"IDP_CLIENT_SECRET\"],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "308cb4ec-826a-496e-ae32-2762658ace18",
"metadata": {},
"source": [
"## Querying across a direct relation"
]
},
{
"cell_type": "markdown",
"id": "6af59c70-5e20-4563-bda5-f1d0eb62ca2d",
"metadata": {},
"source": [
"First, we will query across a direct relation. For example, we can get the `parent` starting from `CogniteAsset`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2db7c273-c079-4ce7-8ae1-08dbb9f334ac",
"metadata": {},
"outputs": [],
"source": [
"from cognite.client import data_modeling as dm\n",
"from cognite.client.data_classes import filters\n",
"\n",
"asset_view = dm.ViewId(\"cdf_cdm\", \"CogniteAsset\", \"v1\")\n",
"# The instance space with the instance demo data\n",
"data_space = \"springfield_instances\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77a56e22-dbd8-40ee-997c-e60564bdde18",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4829284b-429c-4bf2-b4ce-f64d7e848bc1",
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-09T13:23:47.333397Z",
"start_time": "2024-11-09T13:23:47.167740Z"
}
},
"outputs": [],
"source": [
"query = dm.query.Query(\n",
" with_={\n",
" \"asset\": dm.query.NodeResultSetExpression(\n",
" limit=10,\n",
" filter=filters.And(\n",
" filters.HasData(views=[asset_view]),\n",
" filters.Equals([\"node\", \"space\"], data_space),\n",
" ),\n",
" ),\n",
" \"parent\": dm.query.NodeResultSetExpression(\n",
" from_=\"asset\",\n",
" through=dm.PropertyId(source=asset_view, property=\"parent\"),\n",
" ),\n",
" },\n",
" select={\n",
" \"asset\": dm.query.Select(\n",
" sources=[\n",
" dm.query.SourceSelector(source=asset_view, properties=[\"*\"]),\n",
" ]\n",
" ),\n",
" \"parent\": dm.query.Select(\n",
" sources=[\n",
" dm.query.SourceSelector(source=asset_view, properties=[\"*\"]),\n",
" ],\n",
" ),\n",
" },\n",
")\n",
"\n",
"result = client.data_modeling.instances.query(query)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "38fb49fa-3cff-4fa8-9797-3b134e0f60e3",
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-09T13:23:47.767980Z",
"start_time": "2024-11-09T13:23:47.739654Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" space | \n",
" external_id | \n",
" version | \n",
" last_updated_time | \n",
" created_time | \n",
" instance_type | \n",
" path | \n",
" root | \n",
" pathLastUpdatedTime | \n",
" name | \n",
" description | \n",
" sourceId | \n",
" sourceUpdatedUser | \n",
" parent | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:20.316 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 2ND STAGE COMP DRY GAS SEAL SYS ON PH | \n",
" 682546 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 1 | \n",
" springfield_instances | \n",
" WMT:230900 | \n",
" 4 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:20.316 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 230900 | \n",
" GAS COMPRESSION AND RE-INJECTION ON PH | \n",
" 681760 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 2 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:27.925 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 1ST STAGE COMP DRY GAS SEAL SYS ON PH | \n",
" 682540 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 3 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:27.925 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSION-PH | \n",
" 2ND STAGE COMPRESSION ON PH | \n",
" 681794 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 4 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSOR-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:28.103 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSOR-PH | \n",
" 2ND STAGE COMPRESSOR ON PH | \n",
" 681825 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 5 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSOR-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSOR-PH | \n",
" 1ST STAGE COMPRESSOR ON PH | \n",
" 681824 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 6 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
" 1ST STAGE COMP LUBE OIL SYS ON PH | \n",
" 682544 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 7 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMP ENCLOSURE-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMP ENCLOSURE-PH | \n",
" 1ST STAGE COMP ENCLOSURE ON PH | \n",
" 682542 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 8 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSION-PH | \n",
" 1ST STAGE COMPRESSION ON PH | \n",
" 681792 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" space external_id version \\\n",
"0 springfield_instances WMT:23-2ND STAGE COMP DRY GAS SEAL SYS-PH 3 \n",
"1 springfield_instances WMT:230900 4 \n",
"2 springfield_instances WMT:23-1ST STAGE COMP DRY GAS SEAL SYS-PH 3 \n",
"3 springfield_instances WMT:23-2ND STAGE COMPRESSION-PH 5 \n",
"4 springfield_instances WMT:23-2ND STAGE COMPRESSOR-PH 5 \n",
"5 springfield_instances WMT:23-1ST STAGE COMPRESSOR-PH 5 \n",
"6 springfield_instances WMT:23-1ST STAGE COMP LUBE OIL SYS-PH 3 \n",
"7 springfield_instances WMT:23-1ST STAGE COMP ENCLOSURE-PH 3 \n",
"8 springfield_instances WMT:23-1ST STAGE COMPRESSION-PH 5 \n",
"\n",
" last_updated_time created_time instance_type \\\n",
"0 2024-11-13 16:30:39.129 2024-11-08 14:20:20.316 node \n",
"1 2024-11-13 16:30:39.129 2024-11-08 14:20:20.316 node \n",
"2 2024-11-13 16:30:39.129 2024-11-08 14:20:27.925 node \n",
"3 2024-11-13 16:30:39.129 2024-11-08 14:20:27.925 node \n",
"4 2024-11-13 16:30:39.129 2024-11-08 14:20:28.103 node \n",
"5 2024-11-13 16:30:39.129 2024-11-08 14:21:06.876 node \n",
"6 2024-11-13 16:30:39.129 2024-11-08 14:21:06.876 node \n",
"7 2024-11-13 16:30:39.129 2024-11-08 14:21:06.876 node \n",
"8 2024-11-13 16:30:39.129 2024-11-08 14:21:06.876 node \n",
"\n",
" path \\\n",
"0 [{'space': 'springfield_instances', 'externalI... \n",
"1 [{'space': 'springfield_instances', 'externalI... \n",
"2 [{'space': 'springfield_instances', 'externalI... \n",
"3 [{'space': 'springfield_instances', 'externalI... \n",
"4 [{'space': 'springfield_instances', 'externalI... \n",
"5 [{'space': 'springfield_instances', 'externalI... \n",
"6 [{'space': 'springfield_instances', 'externalI... \n",
"7 [{'space': 'springfield_instances', 'externalI... \n",
"8 [{'space': 'springfield_instances', 'externalI... \n",
"\n",
" root \\\n",
"0 {'space': 'springfield_instances', 'externalId... \n",
"1 {'space': 'springfield_instances', 'externalId... \n",
"2 {'space': 'springfield_instances', 'externalId... \n",
"3 {'space': 'springfield_instances', 'externalId... \n",
"4 {'space': 'springfield_instances', 'externalId... \n",
"5 {'space': 'springfield_instances', 'externalId... \n",
"6 {'space': 'springfield_instances', 'externalId... \n",
"7 {'space': 'springfield_instances', 'externalId... \n",
"8 {'space': 'springfield_instances', 'externalId... \n",
"\n",
" pathLastUpdatedTime name \\\n",
"0 2024-11-13T16:30:39.129624+00:00 23-2ND STAGE COMP DRY GAS SEAL SYS-PH \n",
"1 2024-11-13T16:30:39.129624+00:00 230900 \n",
"2 2024-11-13T16:30:39.129624+00:00 23-1ST STAGE COMP DRY GAS SEAL SYS-PH \n",
"3 2024-11-13T16:30:39.129624+00:00 23-2ND STAGE COMPRESSION-PH \n",
"4 2024-11-13T16:30:39.129624+00:00 23-2ND STAGE COMPRESSOR-PH \n",
"5 2024-11-13T16:30:39.129624+00:00 23-1ST STAGE COMPRESSOR-PH \n",
"6 2024-11-13T16:30:39.129624+00:00 23-1ST STAGE COMP LUBE OIL SYS-PH \n",
"7 2024-11-13T16:30:39.129624+00:00 23-1ST STAGE COMP ENCLOSURE-PH \n",
"8 2024-11-13T16:30:39.129624+00:00 23-1ST STAGE COMPRESSION-PH \n",
"\n",
" description sourceId sourceUpdatedUser \\\n",
"0 2ND STAGE COMP DRY GAS SEAL SYS ON PH 682546 8137 \n",
"1 GAS COMPRESSION AND RE-INJECTION ON PH 681760 8137 \n",
"2 1ST STAGE COMP DRY GAS SEAL SYS ON PH 682540 8137 \n",
"3 2ND STAGE COMPRESSION ON PH 681794 8137 \n",
"4 2ND STAGE COMPRESSOR ON PH 681825 8137 \n",
"5 1ST STAGE COMPRESSOR ON PH 681824 8137 \n",
"6 1ST STAGE COMP LUBE OIL SYS ON PH 682544 8137 \n",
"7 1ST STAGE COMP ENCLOSURE ON PH 682542 8137 \n",
"8 1ST STAGE COMPRESSION ON PH 681792 8137 \n",
"\n",
" parent \n",
"0 NaN \n",
"1 NaN \n",
"2 NaN \n",
"3 {'space': 'springfield_instances', 'externalId... \n",
"4 {'space': 'springfield_instances', 'externalId... \n",
"5 {'space': 'springfield_instances', 'externalId... \n",
"6 NaN \n",
"7 NaN \n",
"8 {'space': 'springfield_instances', 'externalId... "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = result[\"asset\"].to_pandas(expand_properties=True)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "400c57d2-754e-4c52-92ec-03dc0e9123d7",
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-09T13:23:48.546916Z",
"start_time": "2024-11-09T13:23:48.536611Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" space | \n",
" external_id | \n",
" version | \n",
" last_updated_time | \n",
" created_time | \n",
" instance_type | \n",
" path | \n",
" root | \n",
" pathLastUpdatedTime | \n",
" name | \n",
" description | \n",
" sourceId | \n",
" sourceUpdatedUser | \n",
" parent | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" springfield_instances | \n",
" WMT:230900 | \n",
" 4 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:20.316 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 230900 | \n",
" GAS COMPRESSION AND RE-INJECTION ON PH | \n",
" 681760 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 1 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:27.925 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSION-PH | \n",
" 2ND STAGE COMPRESSION ON PH | \n",
" 681794 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 2 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSION-PH | \n",
" 1ST STAGE COMPRESSION ON PH | \n",
" 681792 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"NodeListWithCursor([
,\n",
" ,\n",
" ])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result[\"parent\"]"
]
},
{
"cell_type": "markdown",
"id": "1ef1f8db-e525-4b2f-bf47-dabe93abaf2a",
"metadata": {},
"source": [
"## Quering across reverse direct relation of single relation"
]
},
{
"cell_type": "markdown",
"id": "c8d8bf72-4907-4064-a509-a9437f9f82f4",
"metadata": {},
"source": [
"We can for example query across the `children` in `CogniteAsset`. The `children` property is a reverse direct relation of `parent`, so we query in `inwards` direction towards parent"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "75db7a18-99fe-4656-86a3-d33c9c4acb75",
"metadata": {},
"outputs": [],
"source": [
"query = dm.query.Query(\n",
" with_={\n",
" \"asset\": dm.query.NodeResultSetExpression(\n",
" limit=10,\n",
" filter=filters.And(\n",
" filters.HasData(views=[asset_view]),\n",
" filters.Equals([\"node\", \"space\"], data_space),\n",
" ),\n",
" ),\n",
" \"children\": dm.query.NodeResultSetExpression(\n",
" from_=\"asset\",\n",
" through=dm.PropertyId(source=asset_view, property=\"parent\"),\n",
" direction=\"inwards\",\n",
" ),\n",
" },\n",
" select={\n",
" \"asset\": dm.query.Select(\n",
" sources=[\n",
" dm.query.SourceSelector(source=asset_view, properties=[\"*\"]),\n",
" ]\n",
" ),\n",
" \"children\": dm.query.Select(\n",
" sources=[\n",
" dm.query.SourceSelector(source=asset_view, properties=[\"*\"]),\n",
" ],\n",
" ),\n",
" },\n",
")\n",
"\n",
"result = client.data_modeling.instances.query(query)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b53e0aff-ac57-4c02-a9a7-b40f1a844dff",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" space | \n",
" external_id | \n",
" version | \n",
" last_updated_time | \n",
" created_time | \n",
" instance_type | \n",
" path | \n",
" root | \n",
" pathLastUpdatedTime | \n",
" name | \n",
" description | \n",
" sourceId | \n",
" sourceUpdatedUser | \n",
" parent | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:20.316 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 2ND STAGE COMP DRY GAS SEAL SYS ON PH | \n",
" 682546 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 1 | \n",
" springfield_instances | \n",
" WMT:230900 | \n",
" 4 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:20.316 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 230900 | \n",
" GAS COMPRESSION AND RE-INJECTION ON PH | \n",
" 681760 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 2 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:27.925 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMP DRY GAS SEAL SYS-PH | \n",
" 1ST STAGE COMP DRY GAS SEAL SYS ON PH | \n",
" 682540 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 3 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:27.925 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSION-PH | \n",
" 2ND STAGE COMPRESSION ON PH | \n",
" 681794 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 4 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSOR-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:28.103 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSOR-PH | \n",
" 2ND STAGE COMPRESSOR ON PH | \n",
" 681825 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 5 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSOR-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSOR-PH | \n",
" 1ST STAGE COMPRESSOR ON PH | \n",
" 681824 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
" 6 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMP LUBE OIL SYS-PH | \n",
" 1ST STAGE COMP LUBE OIL SYS ON PH | \n",
" 682544 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 7 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMP ENCLOSURE-PH | \n",
" 3 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMP ENCLOSURE-PH | \n",
" 1ST STAGE COMP ENCLOSURE ON PH | \n",
" 682542 | \n",
" 8137 | \n",
" NaN | \n",
"
\n",
" \n",
" 8 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSION-PH | \n",
" 1ST STAGE COMPRESSION ON PH | \n",
" 681792 | \n",
" 8137 | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"NodeListWithCursor([,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result[\"asset\"]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "b16d7ff4-9f74-475d-981c-8b197f182878",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" space | \n",
" external_id | \n",
" version | \n",
" last_updated_time | \n",
" created_time | \n",
" instance_type | \n",
" path | \n",
" root | \n",
" parent | \n",
" pathLastUpdatedTime | \n",
" name | \n",
" description | \n",
" sourceId | \n",
" sourceUpdatedUser | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:27.925 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSION-PH | \n",
" 2ND STAGE COMPRESSION ON PH | \n",
" 681794 | \n",
" 8137 | \n",
"
\n",
" \n",
" 1 | \n",
" springfield_instances | \n",
" WMT:23-2ND STAGE COMPRESSOR-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:20:28.103 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-2ND STAGE COMPRESSOR-PH | \n",
" 2ND STAGE COMPRESSOR ON PH | \n",
" 681825 | \n",
" 8137 | \n",
"
\n",
" \n",
" 2 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSOR-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSOR-PH | \n",
" 1ST STAGE COMPRESSOR ON PH | \n",
" 681824 | \n",
" 8137 | \n",
"
\n",
" \n",
" 3 | \n",
" springfield_instances | \n",
" WMT:23-1ST STAGE COMPRESSION-PH | \n",
" 5 | \n",
" 2024-11-13 16:30:39.129 | \n",
" 2024-11-08 14:21:06.876 | \n",
" node | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" {'space': 'springfield_instances', 'externalId... | \n",
" 2024-11-13T16:30:39.129624+00:00 | \n",
" 23-1ST STAGE COMPRESSION-PH | \n",
" 1ST STAGE COMPRESSION ON PH | \n",
" 681792 | \n",
" 8137 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"NodeListWithCursor([,\n",
" ,\n",
" ,\n",
" ])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result[\"children\"]"
]
},
{
"cell_type": "markdown",
"id": "bd779189-a9ee-4a54-8a3e-c9b0634929b2",
"metadata": {},
"source": [
"## Querying across reverse direct relation of listable relation"
]
},
{
"cell_type": "markdown",
"id": "c8b73be4-04f0-4b82-a9f8-364de5e087b4",
"metadata": {},
"source": [
"We want all activities connected to equipment. The property `activities` in `CogniteEquipment` is a reverse direct relation of `equipment` in `CogniteActivity`. \n",
"The `/query` enpoint does not support querying accross a reverse direct relation of listable relation. Instead, we will first lookup `CogniteEquipment` and the after we have retrieved the equipment items,\n",
"we will use `/search` to lookup activities that are connected to the equipment we found."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a2a567c8-e21b-4dec-8fb8-1782f0b9d470",
"metadata": {},
"outputs": [],
"source": [
"equipment_view = dm.ViewId(\"cdf_cdm\", \"CogniteEquipment\", \"v1\")\n",
"activity_view = dm.ViewId(\"cdf_cdm\", \"CogniteActivity\", \"v1\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c3d22491-d4a0-4d57-9bb2-03c4434b6daa",
"metadata": {},
"outputs": [],
"source": [
"equipment = client.data_modeling.instances.list(limit=100, sources=equipment_view, space=data_space)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7e8712b9-930e-4732-8df5-a9e113962452",
"metadata": {},
"outputs": [],
"source": [
"is_equipment = filters.In(\n",
" activity_view.as_property_ref(\"equipment\"),\n",
" [item.as_id() for item in equipment],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4070ecce-adcc-4204-8523-74ab36357fd2",
"metadata": {},
"outputs": [],
"source": [
"activities = client.data_modeling.instances.search(activity_view, filter=is_equipment, limit=1_000)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8be52c76-3fcc-4d4e-ba65-f20df5e7ed9c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" space | \n",
" external_id | \n",
" version | \n",
" last_updated_time | \n",
" created_time | \n",
" instance_type | \n",
" assets | \n",
" equipment | \n",
" timeSeries | \n",
" name | \n",
" tags | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" springfield_instances | \n",
" 6834296454446862 | \n",
" 8 | \n",
" 2024-11-11 04:01:24.673 | \n",
" 2024-11-08 14:20:32.010 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" Ettersyn av instrumentering | \n",
" [23-PT-96186-03] | \n",
"
\n",
" \n",
" 1 | \n",
" springfield_instances | \n",
" 4779071375364741 | \n",
" 8 | \n",
" 2024-11-11 04:01:24.673 | \n",
" 2024-11-08 14:20:32.968 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" Ettersyn av instrumentering | \n",
" [23-PT-96186-03] | \n",
"
\n",
" \n",
" 2 | \n",
" springfield_instances | \n",
" 7676140200338059 | \n",
" 8 | \n",
" 2024-11-11 04:01:24.673 | \n",
" 2024-11-08 14:21:09.459 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" Årlig ettersyn av instrumentering | \n",
" [23-PT-96186-03] | \n",
"
\n",
" \n",
" 3 | \n",
" springfield_instances | \n",
" 2419388136472507 | \n",
" 6 | \n",
" 2024-11-10 04:01:27.838 | \n",
" 2024-11-08 14:20:42.833 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" | \n",
" [23-PDT-96167] | \n",
"
\n",
" \n",
" 4 | \n",
" springfield_instances | \n",
" 3625599615521242 | \n",
" 6 | \n",
" 2024-11-10 04:01:27.838 | \n",
" 2024-11-08 14:20:42.833 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" | \n",
" [23-PDT-96167] | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 995 | \n",
" springfield_instances | \n",
" 3615479516814565 | \n",
" 3 | \n",
" 2024-11-09 04:01:18.514 | \n",
" 2024-11-08 14:20:49.286 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [] | \n",
" Årlig ettersyn av instrumentering | \n",
" [23-TE-96103-01] | \n",
"
\n",
" \n",
" 996 | \n",
" springfield_instances | \n",
" 2539613360803803 | \n",
" 3 | \n",
" 2024-11-09 04:01:18.514 | \n",
" 2024-11-08 14:20:49.286 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [] | \n",
" *** / *** | \n",
" [23-TE-96103-01] | \n",
"
\n",
" \n",
" 997 | \n",
" springfield_instances | \n",
" 8028091942242686 | \n",
" 3 | \n",
" 2024-11-09 04:01:18.514 | \n",
" 2024-11-08 14:20:50.755 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [] | \n",
" Årlig sampling assessment | \n",
" [23-LT-96182-01] | \n",
"
\n",
" \n",
" 998 | \n",
" springfield_instances | \n",
" 7618946439648778 | \n",
" 3 | \n",
" 2024-11-09 04:01:18.514 | \n",
" 2024-11-08 14:20:52.102 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [] | \n",
" Årlig ettersyn av instrumentering | \n",
" [23-TE-96103-01] | \n",
"
\n",
" \n",
" 999 | \n",
" springfield_instances | \n",
" 3497364432758517 | \n",
" 3 | \n",
" 2024-11-09 04:01:18.514 | \n",
" 2024-11-08 14:20:52.102 | \n",
" node | \n",
" [] | \n",
" [{'space': 'springfield_instances', 'externalI... | \n",
" [] | \n",
" | \n",
" [23-LT-96182-01] | \n",
"
\n",
" \n",
"
\n",
"
1000 rows × 11 columns
\n",
"
"
],
"text/plain": [
"NodeList([,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
" ,\n",
"