1

I want to filter on the PartitionKey, and get the top 1 record from the matches (using the default ordering specified by the RowKey values).

For filtering entities in a table, I'm using TableClient.query_entities. How can I add the $top parameter to the executed HTTP request?

results_per_page = 1 does not limit the result count. I also tried these (found in random source codes) but they're ignored:

service.query_entities(
  query_filter = 'PartitionKey eq \'abc\'',
  parameters = {
    'top': 1
  }
)
from azure.data.tables._generated.models import QueryOptions
service.query_entities(
  query_filter = 'PartitionKey eq \'abc\'',
  query_options = QueryOptions(top = 1)
)

1 Answer 1

0

Created two table entities to query the top entity that is BlueMarker in myTable.

enter image description here

You can get the Top row by http URL, via filtering your table with top parameter:

Refer here

Run this command to get Top1 table entity per page :

List_entities > Lists the entities in the table
from azure.data.tables import TableClient
table_client = TableClient.from_connection_string(conn_str="xxxxDefaultEndpointsProtocol=https;AccountName=<AccountName>;AccountKey=<Accountkey>==;EndpointSuffix=core.windows.net", table_name="<myTable>")
for entity in table_client.list_entities(results_per_page=1):
    print(entity)

enter image description here

Top entity is returned in the output.

enter image description here

  • BlueMarker is the Top entity of the table.
  • After you know the Topmost entity in your table, you can get that specific entity by filtering out its PartitionKey and RowKey with get_entity as per the below code.

If you need only a specific Top entity you can use the following code:


from azure.data.tables import TableClient
from azure.data.tables import TableServiceClient
table_client = TableClient.from_connection_string(conn_str="xxxxxEndpointsProtocol=https;AccountName=<AccountName>;AccountKey=<accountkey>==;EndpointSuffix=core.windows.net", table_name="<myTable>")
task = table_client.get_entity( 'BlueMarker', 'RowID')
print(task)

Output: enter image description here

Only the top Entity BlueMarker is printed in the output by filtering with Partition Key and Row Key.

You can use this code to get the topmost entity > by combining get-entity and list_entity command. Refer below:-

from select import select
from tkinter import TOP
from azure.data.tables import TableClient
from azure.data.tables import TableServiceClient
table_client = TableClient.from_connection_string(conn_str="xxxxxEndpointsProtocol=https;AccountName=<AccountName";AccountKey=<AccountKey>==;EndpointSuffix=core.windows.net", table_name="<myTable>")
for entity in table_client.list_entities(results_per_page=1, select=["PartitionKey", "RowKey"] ):
    entity.task = table_client.get_entity( 'BlueMarker', 'RowID')
else:
    print(entity.task)

Output:

enter image description here

You will get the Topmost entity: Without else-


from select import select
from tkinter import TOP
from azure.data.tables import TableClient
from azure.data.tables import TableServiceClient
table_client = TableClient.from_connection_string(conn_str="xxxxxEndpointsProtocol=https;AccountName=<AccountName>;AccountKey=<AccountKey>==;EndpointSuffix=core.windows.net", table_name="<myTable>")
for entity in table_client.list_entities(results_per_page=1, select=["PartitionKey", "RowKey"] ):
    entity.task = table_client.get_entity('BlueMarker', 'RowID')
print(entity.task)

Output: enter image description here

Note:

  1. Install pip install azure-data-tables
  2. Get a connection string from StorageAccounts -> AccessKeys as shown below. enter image description here

References: MsDoc Github

Sign up to request clarification or add additional context in comments.

4 Comments

This is not so useful and does not solve my problem. If I get a large list of the entities (a page), then I can just keep the first element and that is the topmost entity. There is no reason to query that one by exact PartitionKey-RowKey values. list_entities is not acceptable, since I have to filter on the PartitionKey, that is why I used query_entities. The result image of your first Python code clearly shows my problem: it returns more than one element, which is not needed for me. Your Python code does not show any example on how to use the $top parameter.
$top query is not yet supported in the Python azure-data-tables SDK, refer this document. As a workaround you can use Pandas head method to get the top entity Or try LINQ queries via C# refer here and here. So, you can try with pandas head method and if that'll work, I can provide you the code.
That PyPi page does not indicate $top is not supported in the package. In fact, the module itself uses $top several times under the hood, based on their GitHub source codes, I just can't figure out how I should do it myself. Pandas has nothing to do here, that is a data processing module, calling .head() does not limit the downloaded dataset, and as I said I can already take the first element of a large raw downloaded list. I know it is possible in C#, but I have to use Python, that's why I asked about this module.
Please only answer to the exact question if you can. I'm not interested in anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.