C#의 Azure 테이블 저장소에서 모든 행을 가져오는 방법은 무엇입니까?
zure 테이블 내의 모든 엔티티 목록을 가져오려고 합니다.
제가 이 질문을 어떻게 작성해야 할지 아십니까?
질문에 답하려면 다음과 같은 작업을 수행할 수 있습니다.
var acc = new CloudStorageAccount(
new StorageCredentials("account name", "account key"), true);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("table name");
var entities = table.ExecuteQuery(new TableQuery<MyEntity>()).ToList();
그러나 테이블 서비스는 한 번의 호출로 최대 1000개의 엔티티를 반환합니다.테이블에 사용 가능한 엔티티가 1000개 이상 있으면 다음을 반환합니다.continuation token다음 엔티티 집합을 가져오는 데 사용할 수 있습니다.그ExecuteQuery메소드는 실제로 이 계속 토큰을 내부적으로 처리하므로 어떤 이유로든 이 작업을 취소하려면 그렇게 할 수 없습니다.
더 나은 접근 방식은 다음과 같습니다.ExecuteQuerySegmented응용프로그램이 토큰을 처리하도록 합니다.이를 위한 샘플 코드는 다음과 같습니다.
var acc = new CloudStorageAccount(
new StorageCredentials("account name", "account key"), true);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("table name");
TableContinuationToken token = null;
var entities = new List<MyEntity>();
do
{
var queryResult = table.ExecuteQuerySegmented(new TableQuery<MyEntity>(), token);
entities.AddRange(queryResult.Results);
token = queryResult.ContinuationToken;
} while (token != null);
매번 모든 행이 필요하지 않은 경우 다음을 사용하여 주문형 항목을 천천히 검색하는 것이 더 효율적입니다.yield:
public async Task<IEnumerable<T>> GetAll<T>(string tableName) where T : class
{
var table = this.GetCloudTable(tableName);
TableContinuationToken token = null;
do
{
var q = new TableQuery<T>();
var queryResult = await table.ExecuteQuerySegmentedAsync(q, token);
foreach (var item in queryResult.Results)
{
yield return item;
}
token = queryResult.ContinuationToken;
} while (token != null);
}
이 접근 방식을 사용하면 모든 행을 얻을 수 있지만, 결과를 반복적으로 확인할 수 있습니다.GetAll()그리고 당신이 찾고 있는 것을 찾으면, 당신은 그냥.break루프, 그리고GetAll()메소드가 테이블에서 다음 행을 검색하지 않고 중지됩니다.
더 새로운 애저와 함께.Data.Tables SDK는 특히 시스템을 사용할 때 훨씬 더 짧아졌습니다.린크.신시너겟 패키지.이를 통해 다음과 같이 간단히 작성할 수 있습니다.
/// <summary>
/// Returns all rows in the table
/// </summary>
/// <typeparam name="T">Implementation of ITableEntity</typeparam>
/// <param name="tableClient">The authenticated TableClient</param>
/// <returns></returns>
public static async Task<List<T>> GetAllEntitiesAsync<T>(this TableClient tableClient) where T : class, ITableEntity, new()
{
return await tableClient.QueryAsync<T>(maxPerPage: 1000).ToListAsync().ConfigureAwait(false);
}
참고: 한 번에 1000개의 행을 요청하면(최대) 총 요청량을 대폭 줄일 수 있습니다.
언급URL : https://stackoverflow.com/questions/23940246/how-to-get-all-rows-in-azure-table-storage-in-c
'programing' 카테고리의 다른 글
| 설치된 SQL Server 인스턴스와 해당 버전을 확인하려면 어떻게 해야 합니까? (0) | 2023.04.29 |
|---|---|
| 컬렉션 보기를 올바르게 사용하는 방법뷰 모델의 소스 (0) | 2023.04.29 |
| 부동산 게터 및 세터 (0) | 2023.04.29 |
| 함수의 반환 값을 다중 처리로 전달하는 방법.과정? (0) | 2023.04.29 |
| 이클립스(ADT)에서 APK 내보내기가 자동으로 충돌합니다. (0) | 2023.04.29 |