从 Azure 表存储 OData 筛选器查询读取超过 1000 行?



我们如何从 Azure 表存储中读取超过 1000 行?这是我用于从表存储读取数据的代码,它只检索 1000 行:

readData()
{
var s = @$"https://{storageAccountName}.table.core.windows.net/{tableName}()";          
var baseurl = $"{s}<sas-token>&$filter=Name%20eq%20'XYZ'";
var data = GetForOData(baseurl);
var responseData = data.Data.Replace(".", "_");
var odata = JsonConvert.DeserializeObject<ODataResponse>(responseData); 
}
GetForOData(string url) {return InvokeForOData<Object>("GET", url, null, null);}
private static HttpResponseData InvokeForOData<T>(string method, string url, Object id, T data)
{
var Response = new HttpResponseData()
{
Code = HttpStatusCode.RequestTimeout, Data = string.Empty, Message = string.Empty
};

var PostParam = string.Empty;
if (data != null) { PostParam = data.ToString(); }
var postData = Encoding.UTF8.GetBytes(PostParam);
var request = (HttpWebRequest)WebRequest.Create(new Uri(url + (id == null ? "" : '/' + id.ToString())));
request.Method = method;
// add headers
if (postData.Length > 0)
{
using (Stream requestStream = request.GetRequestStream())
{ requestStream.Write(postData, 0, postData.Length); }
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Response.Code = response.StatusCode;
using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{ Response.Data = stream.ReadToEnd(); }
}            
return Response;
}

在哪里检查 x-ms-continuation-NextPartitionKey 和 x-ms-continuation-NextRowKey,并在下一个请求中使用它们?

更新:我能够找到下一个分区键和下一个行键标题值。如何在下一个请求中传递这些值?

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Response.Code = response.StatusCode;
var nextPartitionKey = response.Headers["x-ms-continuation-NextPartitionKey"];
var nextRowKey = response.Headers["x-ms-continuation-NextRowKey"];
using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
Response.Data = stream.ReadToEnd();
}
}

对 Azure 表存储的单个调用最多将返回 1000 个实体。如果有更多实体与查询匹配,你将获得一个继续令牌,你将需要使用它来获取下一组实体。

因此,在您的情况下,为了读取 1000 多个实体,您必须发送请求、获取数据并检查响应中的继续令牌(x-ms-continuation-NextPartitionKeyx-ms-continuation-NextRowKey(,并在下一个请求中使用它们。

可在此处了解有关 Azure 表存储中分页的详细信息:https://learn.microsoft.com/en-us/rest/api/storageservices/query-timeout-and-pagination。

更新

请参阅下面的代码(尽管未经测试(:

private static HttpResponseData InvokeForOData<T>(string method, string url, Object id, T data)
{
var Response = new HttpResponseData()
{
Code = HttpStatusCode.RequestTimeout, 
Data = string.Empty, 
Message = string.Empty,
NextPartitionKey = string.Empty,
NextRowKey = string.Empty
};

var PostParam = string.Empty;
if (data != null) { PostParam = data.ToString(); }
var postData = Encoding.UTF8.GetBytes(PostParam);
var request = (HttpWebRequest)WebRequest.Create(new Uri(url + (id == null ? "" : '/' + id.ToString())));
request.Method = method;
// add headers
if (postData.Length > 0)
{
using (Stream requestStream = request.GetRequestStream())
{ requestStream.Write(postData, 0, postData.Length); }
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Response.Code = response.StatusCode;
var nextPartitionKey = response.Headers["x-ms-continuation-NextPartitionKey"];
var nextRowKey = response.Headers["x-ms-continuation-NextRowKey"];
using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
Response.Data = stream.ReadToEnd();
Response.NextPartitionKey = nextPartitionKey;
Response.NextRowKey = nextRowKey;
}
}     
return Response;
}

基本上我所做的是在HttpResponseData对象中添加了两个属性(NextPartitionKeyNextRowKey(,并分别用您从响应标头获得的nextPartitionKeynextRowKey填充它。

在代码(处理响应的位置(中,您必须检查这两个值中的任何一个是否不为 null 或为空。非空/空值表示存在更多实体。

如果是这种情况,那么您要做的就是通过将NextPartitionKeyNextRowKey值追加为查询字符串参数来修改 URL(有关详细信息,请参阅上面的链接(,然后再次发送请求。您需要执行此操作,直到将这两个值都作为 null 或空字符串获取。

相关内容

  • 没有找到相关文章

最新更新