Need to build a complex query for Azure Table to count number of rows - azure

I am trying to run complex query for Azure Table where I want to count number of rows for all deviceID for specific dateiot and timeiot? Is this possible?

Your query would be something like:
PartitionKey eq 'aaaa' and dateiot eq 'bbbb' and deviceID eq 'cccc' and timeiot eq 'dddd'
2 things though:
This query will do a complete partition scan and may result in incomplete data in a single request. Your code should be able to handle continuation tokens to get all data matching the query.
Table query does not support count functionality so you will get the entities back. You will need to add all entities to get the total count of entities matching the query.

Related

CosmosDB - does distinct scan indexe for the field or scan all records?

When there is a table with records of this format :
{
"productId" : "123"
"productCategory" : "fmcg"
}
There can be millions of such product records spread across a few hundred productCategory.
If I do a select distinct c.productCategory from c on this table, does it just do a scan on the index it has created for productCategory (is there such index??), or does it go through the millions of records to figure out the distinct productCategory?
At this moment Cosmos won't leverage your index to perform the DISTINCT in your query. It goes through all documents that match your filter and therefore will increase in RU linearly the more documents it'll have to scan.
It's however something that is being worked on according to the Azure Share Your Ideas board.
It looks like Microsoft has optimized the way Distinct, Group By, OFFSET LIMIT and JOIN operators work by leveraging the index better.
This feature was out around June 21.
More details:-
https://feedback.azure.com/d365community/idea/8d3cad4c-0e25-ec11-b6e6-000d3a4f0858
https://devblogs.microsoft.com/cosmosdb/introducing-a-new-system-function-and-optimized-query-operators/

Azure: Get a row key value from a table and display it as a token number

I need to get a row key value from a table and display it as a token number in logic apps. Is there any possibility for this? please let me know if any.
I have tried to get entity and display but it's asking row key as input. So I am not getting any idea what to do.
You could use Get Entities to implement it, this action supports Filter Query and Select Query
OData filter query is for which entities to return and OData select query is for the columns to be returned.
So you could in the filter query use like PartitionKey eq '001' to get the entities and in the select query use RowKey then it will only return the RowKey of all entities.

how Cql's Collection contains alternative value?

I have a question to query to cassandra collection.
I want to make a query that work with collection search.
CREATE TABLE rd_db.test1 (
testcol3 frozen<set<text>> PRIMARY KEY,
testcol1 text,
testcol2 int
)
table structure is this...
and
this is the table contents.
in this situation, I want to make a cql query has alternative option values on set column.
if it is sql and testcol3 isn't collection,
select * from rd.db.test1 where testcol3 = 4 or testcol3 = 5
but it is cql and collection.. I try
select * from test1 where testcol3 contains '4' OR testcol3 contains '5' ALLOW FILTERING ;
select * from test1 where testcol3 IN ('4','5') ALLOW FILTERING ;
but this two query didn't work...
please help...
This won't work for you for multiple reasons:
there is no OR operation in CQL
you can do only full match on the value of partition key (testcol3)
although you may create secondary indexes for fields with collection type, it's impossible to create an index for values of partition key
You need to change data model, but you need to know the queries that you're executing in advance. From brief looking into your data model, I would suggest to rollout the set field into multiple rows, with individual fields corresponding individual partitions.
But I want to suggest to take DS201 & DS220 courses on DataStax Academy site for better understanding how Cassandra works, and how to model data for it.

Azure Table Storage - Retrieving all entities matching a partial row key

I am just learning Azure Table Storage and I'm able to save and retrieve entities without any problem. However, I'd like to do the following. Say I have row keys (all with the same partition key) that look as follows:
KJV-C1-V1
KJV-C1-V2
KJV-C1-V3
KJV-C2-V1
KJV-C2-V2
KJV-C2-V3
I'd like to be able to perform two types of queries in .NET C#:
Retrieve all entities with row keys that start with 'KJV-C1'.
Retrieve all entities with row keys that contain '-C1-' in the key
Preferrably I'd like to be able to do this without reading all entities in the partition and filtering the ones that don't match the pattern I'm looking for. Is this possible with Azure Table Storage queries?
Retrieve all entities with row keys that start with 'KJV-C1'.
This is possible. Sample OData query:
PartitionKey eq 'your partition key' and (RowKey ge 'KJV-C1' and RowKey lt 'KJV-C2')
Retrieve all entities with row keys that contain '-C1-' in the key
This unfortunately is not possible. You would have to fetch all entities and filter the data on the client side.
You cannot do something like contains() over keys. But as it supports CompareTo("") method, you need to slightly modify your table design.
Maintain multiple partition keys instead of single. You can simple push 'KJV' part of your row key to partition key. Then start with C1-V1, C1-V2 as your row keys.
Then, if you want
All entries of KJV - Query for partition key 'KJV'
All 'C1' entries of KJV - Query for partition key 'KJV' and row key starting with 'C1-'
All entries for C1 - Query for row key starting with 'C1-'
OR with out design change in table, you need to loop through your major products like 'KJV' and build multiple queries with each starting with 'KJV-C1-', then union all of them to get final result.
Please mind that table storage does not allow all LINQ operations and sometimes you need to design the table keys keeping your majority of queries in mind.

Azure query using the select

I am trying to get a query in azure in which I want to get the entity with the given partition key and row key based on Date.
I am keeping entities
Partisionkey, row key, Date, Additional info.
I am looking for a query using tableservies so that ,
I always get the latest one (using date)
How can I get the query? (I am using node and Azure)
TableQuery
.select()
.from('myusertables')
.where('PartitionKey eq ?', '545455');
How write the table query?
To answer you question, check out this previously answered question: How to select only the records with the highest date in LINQ
However, you may be facing a design issue. Performing the operation you are trying to do will require you to pull all the entities from the underlying Azure Table, which will perform slower over time as entities are added. So you may want to reconsider your design and possibly change the way you use your partitionkey and rowkey. You could also store the latest entities in a separate table, so that only 1 entity is found per table, transforming your scan/filter into a seek operation. Food for thought...

Resources