-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUERY] is it possible to query the DIGITALTWINS using LastUpdatedOn in the WHERE statement? #33694
Comments
Thank you for your feedback. This has been routed to the support team for assistance. |
@uriel-kluk Thanks for reaching out to us and reporting this issue. Could you please let me know if the below sample helps ?
This sample code uses the Azure.DigitalTwins.Core SDK to query for digital twins that have been updated in the last 30 days (change the value of AddDays as per your requirement). The DigitalTwinsClient.QueryAsync method is used to execute the query, passing in the query string and the parameters. The results are then printed to the console. Make sure to replace with your actual connection string. |
Thank you navba.
The sample provided does not compile using Azure.DigitalTwins.Core 1.4.0
I changed it to
var queryResult = await _client.QueryAsync<BasicDigitalTwin>(query, parameters);
But QueryAsync does not support parameters and compiler throws the error:
Severity
Code
Description
Project
File
Line
Suppression State
Error
CS1503
Argument 2: cannot convert from 'System.Collections.Generic.Dictionary<string, object>' to 'System.Threading.CancellationToken'
Mesh.Core.DigitalTwinStorageMgr.UnitTests
C:\_Src\Mesh.Olympus.Models.Core\test\Mesh.Core.DigitalTwinStorageMgr.UnitTest\CustomTwinsTests.cs
312
Active
I tried directly on the twin explorer the query
SELECT * FROM digitaltwins WHERE LastUpdatedOn > '2020/01/01'
But get no results.
I suspect I am doing something wrong, but I am unable to spot it.
Best regards,
UK
From: navba-MSFT ***@***.***>
Sent: Thursday, January 26, 2023 10:13 PM
To: Azure/azure-sdk-for-net ***@***.***>
Cc: Uriel Kluk ***@***.***>; Mention ***@***.***>
Subject: Re: [Azure/azure-sdk-for-net] [QUERY] is it possible to query the DIGITALTWINS using LastUpdatedOn in the WHERE statement? (Issue #33694)
@uriel-kluk<https://github.com/uriel-kluk> Thanks for reaching out to us and reporting this issue. Could you please let me know if the below sample helps ?
using Azure.DigitalTwins.Core;
using System;
class Program
{
static async Task Main()
{
// Create a new DigitalTwinsClient instance
var connectionString = "<Your connection string>";
var client = new DigitalTwinsClient(connectionString);
// Define the query
var query = "SELECT * FROM digitaltwins WHERE LastUpdatedOn > @Updatedon";
var parameters = new Dictionary<string, object>
{
{ ***@***.***", DateTime.Now.AddDays(-30) }
};
// Execute the query
var queryResult = await client.QueryAsync(query, parameters);
// Print the results
foreach (var result in queryResult)
{
Console.WriteLine(result);
}
}
}
This sample code uses the Azure.DigitalTwins.Core SDK to query for digital twins that have been updated in the last 30 days (change the value of AddDays as per your requirement). The DigitalTwinsClient.QueryAsync method is used to execute the query, passing in the query string and the parameters. The results are then printed to the console.
Make sure to replace with your actual connection string.
—
Reply to this email directly, view it on GitHub<#33694 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJ2NCAY6Y3OIESJT7LHGWTLWUNDMJANCNFSM6AAAAAAUH4HCCM>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
@uriel-kluk Thanks for your reply. I am sharing this sample code link which has the sample queries and walkthrough information to use with the Azure Digital Twins query plugin. Also here is the Comparison operators that are supported and can be used in Digital Twins Select clause:
Hope this helps. |
Thank you Navba,
We can close the question, yet I am unable to answer how to query the database to select multiple twins using the built-in LastUpdatedOn field that shows in the BasicDigitalTwin.
I manage to issue the query, but it returns an empty collection.
This code shows how to query a single twin and enumerate the properties including the built-in.
Response<BasicDigitalTwin> getBasicDtResponse = await client.GetDigitalTwinAsync<BasicDigitalTwin>(basicDtId);
BasicDigitalTwin basicDt = getBasicDtResponse.Value;
// Must cast Component1 as a JsonElement and get its raw text in order to deserialize it as a dictionary
string component1RawText = ((JsonElement)basicDt.Contents["Component1"]).GetRawText();
var component1 = JsonSerializer.Deserialize<BasicDigitalTwinComponent>(component1RawText);
Console.WriteLine($"Retrieved and deserialized digital twin {basicDt.Id}:\n\t" +
$"ETag: {basicDt.ETag}\n\t" +
$"ModelId: {basicDt.Metadata.ModelId}\n\t" +
$"LastUpdatedOn: {basicDt.LastUpdatedOn}\n\t" +
$"Prop1: {basicDt.Contents["Prop1"]}, last updated on {basicDt.Metadata.PropertyMetadata["Prop1"].LastUpdatedOn}\n\t" +
$"Prop2: {basicDt.Contents["Prop2"]}, last updated on {basicDt.Metadata.PropertyMetadata["Prop2"].LastUpdatedOn} and sourced at {basicDt.Metadata.PropertyMetadata["Prop2"].SourceTime}\n\t" +
$"Component1.LastUpdatedOn: {component1.LastUpdatedOn}\n\t" +
$"Component1.Prop1: {component1.Contents["ComponentProp1"]}, last updated on: {component1.Metadata["ComponentProp1"].LastUpdatedOn}\n\t" +
$"Component1.Prop2: {component1.Contents["ComponentProp2"]}, last updated on: {component1.Metadata["ComponentProp2"].LastUpdatedOn} and sourced at: {component1.Metadata["ComponentProp2"].SourceTime}");
From: navba-MSFT ***@***.***>
Sent: Monday, January 30, 2023 1:12 AM
To: Azure/azure-sdk-for-net ***@***.***>
Cc: Uriel Kluk ***@***.***>; Mention ***@***.***>
Subject: Re: [Azure/azure-sdk-for-net] [QUERY] is it possible to query the DIGITALTWINS using LastUpdatedOn in the WHERE statement? (Issue #33694)
@uriel-kluk<https://github.com/uriel-kluk> Thanks for your reply. I am sharing this sample code<https://github.com/Azure-Samples/azure-digital-twins-getting-started/tree/main/adt-adx-queries> link which has the sample queries and walkthrough information to use with the Azure Digital Twins query plugin.
Also here is the Comparison operators that are supported and can be used in Digital Twins Select clause:
=, !=: Used to compare equality of expressions.
<, >: Used for ordered comparison of expressions.
<=, >=: Used for ordered comparison of expressions, including equality.
Hope this helps.
—
Reply to this email directly, view it on GitHub<#33694 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJ2NCA27C46F6YLNHGB4QH3WU5STBANCNFSM6AAAAAAUH4HCCM>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
@uriel-kluk Thanks for getting back and sharing your sample code snippet. Regarding the issue about the query returning the empty collection, would require some troubleshooting. Could you open a support ticket for that ? You could leverage the below sample code which creates a DigitalTwinsClient instance and uses the GetDigitalTwinsAsync method to get a collection of digital twins. The digital twins are then sorted in descending order based on the LastUpdatedOn property using the OrderByDescending method.
Hope this helps. |
Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you! |
The query using the built-in |
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @johngallardo, @efriesner, @abhinav-gha, @Aashish93-stack, @sjiherzig, @Satya-Kolluri. Issue DetailsLibrary name and versionAzure.DigitalTwins.Core Query/QuestionHello ADT Team, I would like to run a query to discover fresh or stale twins. The I am able to run a query and filter the result set:
But, I would like the internal database to filter for me without having to add a custom capability. Is there a simple way to do this? Best regards, EnvironmentNo response
|
@johngallardo, @efriesner, @abhinav-gha, @Aashish93-stack, @sjiherzig, @Satya-Kolluri Could you please look into this request ? Thanks in advance |
You can query the $metadata.$lastUpdateTime field. Just make sure to use an ISO8601 formatted string (DateTime.ToString(“o”)).
For example:
var time = DateTime.UtcNow.AddYears(-2);
var query = $"SELECT * FROM DIGITALTWINS WHERE $metadata.$lastUpdateTime > '{time.ToString("o")}'";
Console.WriteLine(query);
var q = dt.QueryAsync<JsonElement>(query);
await foreach(var item in q)
{
Console.WriteLine(item);
}
|
@uriel-kluk Thanks for the update. |
Library name and version
Azure.DigitalTwins.Core
Query/Question
Hello ADT Team,
I would like to run a query to discover fresh or stale twins.
The
BasicDigitalTwin
class contains the field LastUpdatedOn.I am able to run a query and filter the result set:
But, I would like the internal database to filter for me without having to add a custom capability.
SELECT * FROM DIGITALTWINS WHERE LastUpdatedOn > '{yesterday}'
Is there a simple way to do this?
Best regards,
UK
Environment
No response
The text was updated successfully, but these errors were encountered: