从Azure存储表中检索1000多个记录-JS

时间:2018-11-20 01:58:41

标签: node.js azure azure-storage azure-table-storage

我有一个检索一千多条记录的问题。不知何故,只检索了前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

});

3 个答案:

答案 0 :(得分:2)

要基于代码使用回调,请使用递归。

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); })

答案 1 :(得分:0)

您可以尝试

        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;

答案 2 :(得分:0)

如果您想使用它,我就是为此创建了一个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);