简体   繁体   中英

Windows Azure Table Storage Take Issue

On Windows Azure table Storage I am executing the next query:

CloudTableClient ctc=TableStorage.getTableClient();
String q1=TableQuery.generateFilterCondition(TableConstants.PARTITION_KEY, QueryComparisons.EQUAL, Long.toHexString(partitionId));
TableQuery<Actividad> rangeQuery=TableQuery.from(tableName, Actividad.class).where(q1).take(2);
int e=0;
for(Actividad ac:ctc.execute(query)){
    e++;
}
System.out.println(e);

But I am getting all rows on partition, not just the top 2 specified on take(2).

Any suggestion?

Answering smarx:

I use Wireshark to see the http requests:

The initial request is:

GET /actividadreciente?$filter=PartitionKey%20eq%20%2717%27&$top=2&timeout=60

There is a response from the service and then a lot of requests are made:

GET /actividadreciente? $filter=PartitionKey%20eq%20%2717%27&$top=2&NextRowKey=1%2124%21RkZGRkZFQzY2REYzMTA3Mw--&timeout=60&NextPartitionKey=1%214%21MTc-

GET /actividadreciente?$filter=PartitionKey%20eq%20%2717%27&$top=2&NextRowKey=1%2124%21RkZGRkZFQzY3Mjk2MEZEOA--&timeout=60&NextPartitionKey=1%214%21MTc-

GET /actividadreciente?$filter=PartitionKey%20eq%20%2717%27&$top=2&NextRowKey=1%2124%21RkZGRkZFQzY3Mjk5MzVGOQ--&timeout=60&NextPartitionKey=1%214%21MTc-

And so on.

Smarx is right, one can iterate over all results, but if you only iterate over the first 2, the library will only make a request with those items. Subsequence iterations are made with aditional requests. So the solution is to use the next code:

CloudTableClient ctc=TableStorage.getTableClient();
final int limit=2;
String q1=TableQuery.generateFilterCondition(TableConstants.PARTITION_KEY, QueryComparisons.EQUAL, Long.toHexString(partitionId));
TableQuery<Actividad> rangeQuery=TableQuery.from(tableName, Actividad.class).where(q1).take(limit);
int e=0;
for(Actividad ac:ctc.execute(query)){
    e++;
    //Use ac
    if(e==limit)break;
}
System.out.println(e);

Thank you a lot Smarx!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM