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?

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.

 


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


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.


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?


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=s"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=d"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


Hi @Gargi Bhosale,

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


Reply