question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Classcast error while using CosmosDB query with COUNT and VALUE

See original GitHub issue

Hi!

I use Java to write a function that reads data from a CosmosDB instance. I encounter a problem when I use a query like SELECT VALUE COUNT(1) FROM Messages m.

I get a class cast exception: System.Private.CoreLib: Exception while executing function: Functions.ReadDevicesLastSeenHistogram. Anonymously Hosted DynamicMethods Assembly: Unable to cast object of type ‘Newtonsoft.Json.Linq.JValue’ to type ‘Newtonsoft.Json.Linq.JObject’.

I am using version 3.0.3 of the CosmosDB extension.

Repro steps

    @FunctionName("ReadDevicesLastSeenHistogram")
    public HttpResponseMessage readDevicesLastSeenHistogram(@HttpTrigger(name = "req",
            methods = {HttpMethod.GET},
            route = "statistics/lastSeenHistogram",
            authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<Void>> request,
            @CosmosDBInput(name="databaseAll",
                    databaseName = "IoTMessagesDatabase",
                    collectionName = "DevicesLastDocumentCollection",
                    connectionStringSetting = "siriusiotdb_DOCUMENTDB",
                    partitionKey = "/deviceId",
                    sqlQuery = "SELECT VALUE COUNT(1) FROM Messages m") String allCount,
            @CosmosDBInput(name="databaseMinus1",
                    databaseName = "IoTMessagesDatabase",
                    collectionName = "DevicesLastDocumentCollection",
                    connectionStringSetting = "siriusiotdb_DOCUMENTDB",
                    partitionKey = "/deviceId",
                    sqlQuery = "SELECT VALUE COUNT(1) FROM Messages m WHERE m._ts>udf.timestampMinusDays(1)") String dayCount){
        Map<String,Long> result=new HashMap<>();

        result.put("all",Long.valueOf(allCount));
        result.put("1day",Long.valueOf(dayCount));

        return request.createResponseBuilder(HttpStatus.OK).body(result).build();
    }

Expected behavior

No class cast error.

Actual behavior

System.Private.CoreLib: Exception while executing function: Functions.ReadDevicesLastSeenHistogram. Anonymously Hosted DynamicMethods Assembly: Unable to cast object of type ‘Newtonsoft.Json.Linq.JValue’ to type ‘Newtonsoft.Json.Linq.JObject’

Known workarounds

None

Related information

  • Java Function App
  • CosmosDB extension version 3.0.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ealsurcommented, Mar 12, 2021

The fix will be available in extension 3.0.10 coming in the next week.

0reactions
ealsurcommented, Feb 23, 2021

I can try to send a PR this week with this requested change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot query issues when using Azure Cosmos DB
APPLIES TO: NoSQL. This article walks through a general recommended approach for troubleshooting queries in Azure Cosmos DB.
Read more >
Azure cosmosdb doesnt show proper value for select count ...
Per my experience, your error is resulted of bottleneck of RUs setting. The count query is limited by the number for RUs allocated...
Read more >
ClassCastException while using count(*) in HQL query
Hi, I am using Hibernate 3.3.1 GA version for one of my project. I am getting following exception while using "count()" agreegate function....
Read more >
Get the Record Count in Cosmos DB | Azure Tips and Tricks
If you're wondering about the VALUE keyword – all queries return JSON fragments back. By using VALUE, you can get the scalar value...
Read more >
Querying JSON documents in Azure Cosmos DB using the ...
This article will describe to query JSON documents in Azure Cosmos DB using the SQL API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found