从 Azure 存储中检索超过 1000 条记录 table - js

Retrieving more than 1000 records from azure storage table - js

我遇到了检索超过一千条记录的问题。不知何故只有前 1000 个被检索。从我的研究中,我发现我必须使用 getContinuation Token 来获取后续记录。请告知我应该如何将其添加到我的代码中。

tableService.queryEntities(table, tableQuery, null, function(error, results) {
    if (error) {
        alert('List table entities error, please open browser console to view detailed error');
        console.log(error);
    } else {
        //display records

});

以下在 C# 中的工作:

var resultList = new List<T>();
TableQuery<T> query = new TableQuery<T>();

TableContinuationToken continuationToken = null;


var table = _tableClient.GetTableReference(tableName);

do
{
    var token = continuationToken;
    var queryResult = TryOperation(() => table.ExecuteQuerySegmented(new TableQuery<T>(), token), tableName);
    if (queryResult != null)
    {
        resultList.AddRange(queryResult.Results);
        continuationToken = queryResult.ContinuationToken;
    }
} while (continuationToken != null);

return resultList;

要根据您的代码使用回调,请使用递归。

var entities = [];
function queryEntitiesSegmented(table, tableQuery, continuationToken){
    tableService.queryEntities(table, tableQuery, continuationToken, (error, results)=> {
        if (error) {   console.log(error); } 
        else { 
            entities.push.apply(entities, results.entries);
            if(results.continuationToken){
                queryEntitiesSegmented(table, tableQuery, results.continuationToken);
            }
            else{
                entities.forEach(entity=>{
                   // display each one
                })
            }
        }
    });
}

queryEntitiesSegmented(table, tableQuery, null);

另一种选择是异步和等待。

const queryEntitiesSegmented = async(table, tableQuery, continuationToken)=>{
    return new Promise((resolve,reject)=>{
        tableService.queryEntities(table, tableQuery, continuationToken, (error, results)=> {
            if (error) {  reject(error); } 
            else { resolve(results);  }
        });
    });
}

async function queryAllEntities(){
    var tableQuery = new azure.TableQuery();
    var table = 'tableName';
    var continuationToken = null;
    var entities = [];
    do{
        var results =  await queryEntitiesSegmented(table, tableQuery, continuationToken);
        continuationToken = results.continuationToken;
        entities.push.apply(entities, results.entries);
    }
    while(continuationToken!=null);
    return entities;
}

queryAllEntities().then(entities=>{
    entities.forEach(entity=>{
    //display each one
    })
}).catch(error=>{ console.log(error); })

如果你想使用它,我为此创建了一个 npm 包。 https://www.npmjs.com/package/azure-table-query-recursive

用法

const {queryAzureTableStorage, TableQuery, createTableService} = require('azure-table-query-recursive');

const validConnectionString = 'DefaultEndpointsProtocol=https;AccountName=xxxxxxx;AccountKey=xxxxxxxxxxxxxxxxxxxxxxx==;EndpointSuffix=core.windows.net';
const query = `PartitionKey eq 'apartitionkey'`;
const table = 'aTable';

//Constract a tableStorage object
const tableStorage = createTableService(validConnectionString);
//Constract an azure table query
const azureQuery = new TableQuery().where(query);

const tableResults = await queryAzureTableStorage(azureQuery, table, tableStorage);