Skip to main content
Question

how to retrieve metadata key value pairs using python sdk

  • 24 July 2024
  • 6 replies
  • 39 views

Hello team,

 

I am bulding an application using the methods in cognite python sdk.

I want to create dropdowns for metadata and the values for the same. This similar to the filters on the data explorer screen in the fusion UI:

I could not find a direct SDK method to do so.

Could you please guide me on how can I achieve that?

6 replies

Userlevel 4
Badge

In order to do a value count for a particular metadata key, you can use the following:

from cognite.client.data_classes.time_series import TimeSeriesProperty

client.time_series.aggregate_unique_values(
property=TimeSeriesProperty.metadata_key("type")
)

In the above example, I’m aggregating time series on metadata key ”type”. Similar methods exists for e.g. Events and Assets as well.

 

Userlevel 1
Badge

Hello, thank you for the answer, how do you also extract the different keys in a project?

Userlevel 4
Badge

If you don’t care about casing, this will do the trick:

client.time_series.aggregate_unique_properties(
TimeSeriesProperty.metadata
)

If you need to distinguish “foo” from “Foo”, you need to exhaustively list all resources and aggregate client side.

Userlevel 1
Badge

Okay Thank you!

just another question I wanted to use the timeseries filter in such a way that I can give the exact external_id and not external_id_prefix. Can you please suggest me how I can do that?

Userlevel 4
Badge

I don’t understand exactly what kind of query you intend to do, but here are a few examples:

  • If you know the exact external IDs, you can use retrieve/retrieve_multiple:

client.time_series.retrieve(external_id="foo")

  • List all time series connected to an asset by giving asset external IDs:
client.time_series.list(
asset_external_ids=["foo", "bar"],
)

You can also use the same filtering for the aggregate methods and search by using:

from cognite.client.data_classes import TimeSeriesFilter

TimeSeriesFilter(asset_external_ids=["foo", "bar"])

 

I’d also like to point you to the SDK documentation (which in my opinion) has a lot of good examples on usage 😊:

https://cognite-sdk-python.readthedocs-hosted.com/en/latest/time_series.html#retrieve-a-time-series-by-id

Userlevel 4
Badge +2

Hi @Gargi Bhosale,

We are following up to see whether you're satisfied with the responses you've received? 

Reply