Skip to main content
Solved

how to retrieve metadata key value pairs using python sdk


Forum|alt.badge.img

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?

Best answer by Håkon V. Treider

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.

 

View original
Did this topic help you find an answer to your question?

7 replies

Forum|alt.badge.img

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.

 


Forum|alt.badge.img
  • Author
  • Committed
  • 20 replies
  • July 24, 2024

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


Forum|alt.badge.img

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.


Forum|alt.badge.img
  • Author
  • Committed
  • 20 replies
  • July 25, 2024

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?


Forum|alt.badge.img

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


Dilini Fernando
Seasoned Practitioner
Forum|alt.badge.img+2
  • Seasoned Practitioner
  • 671 replies
  • August 30, 2024

Hi @Gargi Bhosale,

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


Isha Thapliyal
Seasoned Practitioner
Forum|alt.badge.img+3
  • Seasoned Practitioner
  • 212 replies
  • September 19, 2024

Hi @Gargi Bhosale ,

I hope the issue has been resolved with the above reply. I'm closing this topic for now. Please feel free to start a new post if you have any questions. 

Thanks 
Isha


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings