所有行复制到Azure的表的存储另一表 [英] Copy all Rows to another Table in Azure Table Storage

查看:148
本文介绍了所有行复制到Azure的表的存储另一表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是所有行从一个表复制到另一个表的最佳方法?结果
我想下面code拿到表中的所有行:

  TableServiceContext _dataContext;
    公共IEnumerable的< T> GetAllEntities()
    {
        IQueryable的< T>查询= NULL;
        尝试
        {
            查询= _dataContext.CreateQuery< T>(_ tableName值);
        }
        赶上(异常前)
        {        }
        返回query.ToArray();
    }

但它不得到比行大约900多。
我有几十万行。结果
更新code:

 公共类TableRepository< T> :IRepository< T>
    其中T:TableEntity
{
    保护只读字符串_tableName;
    保护只读TableServiceContext _dataContext;
    保护只读CloudTable _tableReference;    公共TableRepository(字符串tableName值,CloudTableClient tableClient)
    {
        _tableName = tableName值;
        _dataContext = tableClient.GetTableServiceContext();
        _tableReference = tableClient.GetTableReference(tableName值);
        _dataContext.ResolveType = ResolveEntityType;
        _dataContext.MergeOption = System.Data.Services.Client.MergeOption.NoTracking;
    }    公共IEnumerable的< T> GetAllEntities()
    {
        清单< T> allEntities =新的List< T>();
        尝试
        {
            Microsoft.WindowsAzure.Storage.Table.TableContinuationToken tableContinuationToken = NULL;
            做
            {
                VAR queryResponse = _tableReference.ExecuteQuerySegmented< T>(NULL,tableContinuationToken,NULL,NULL);
                tableContinuationToken = queryResponse.ContinuationToken;
                allEntities.AddRange(queryResponse.Results);
            }
            而(tableContinuationToken!= NULL);        }
        赶上(异常前)
        {
            抛出新DALException(_tableName,_dataContext.BaseUri.OriginalString,前的错误,而查询的数据发生);
        }
        返回allEntities;
    }

}

但有错误:


  

错误121T必须是一个非抽象类型与公共参数构造函数,才能在泛型类型或方法Microsoft.WindowsAzure.Storage.Table.CloudTable.ExecuteQuerySegmented

解决方案

因为你正在运行到延续令牌你只得到900结果传回的原因。默认情况下,以表服务的单个请求将最多1000的实体返回。这可能是低于1000单位(甚至是0),但从未超过1000如果有可用的多个实体,则​​该表服务返回延续标记应该使用获取下一组的实体。

所以你的code应该寻找延续标记,并应继续,直到时间标记由表服务返回获取的实体。请看看下面的示例code:

 私人的IEnumerable< T> FetchAllEntities()
{
    清单< T> allEntities =新的List< T>();
    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
    CloudTable表= storageAccount.CreateCloudTableClient()GetTableReference(MyTable的);
    Microsoft.WindowsAzure.Storage.Table.TableContinuationToken tableContinuationToken = NULL;
    做
    {
        VAR queryResponse = table.ExecuteQuerySegmented< T>(NULL,tableContinuationToken,NULL,NULL);
        tableContinuationToken = queryResponse.ContinuationToken;
        allEntities.AddRange(queryResponse.Results);
    }
    而(tableContinuationToken!= NULL);
    返回allEntities;
}

更新

有关你的错误,请尝试更改以下

 公共类TableRepository< T> :IRepository< T>
    其中T:TableEntity

 公共类TableRepository< T> :IRepository< T>
    其中T:TableEntity,新的()

注意除了新() TableEntity

What is the best way to copy all the rows from one table to another table?
I tried below code to get all the rows in a table:

    TableServiceContext _dataContext;
    public IEnumerable<T> GetAllEntities()
    {
        IQueryable<T> query = null;
        try
        {
            query = _dataContext.CreateQuery<T>(_tableName);
        }
        catch (Exception ex)
        {

        }
        return query.ToArray();
    }

but it doesnt get rows more than around 900. I have few hundreds of thousands of rows.
Updated Code:

 public class TableRepository<T> : IRepository<T> 
    where T : TableEntity
{
    protected readonly string _tableName;
    protected readonly TableServiceContext _dataContext;
    protected readonly CloudTable _tableReference;

    public TableRepository(string tableName, CloudTableClient tableClient)
    {
        _tableName = tableName;
        _dataContext = tableClient.GetTableServiceContext();
        _tableReference = tableClient.GetTableReference(tableName);
        _dataContext.ResolveType = ResolveEntityType;
        _dataContext.MergeOption = System.Data.Services.Client.MergeOption.NoTracking;
    }

    public IEnumerable<T> GetAllEntities()
    {
        List<T> allEntities = new List<T>();
        try
        {
            Microsoft.WindowsAzure.Storage.Table.TableContinuationToken tableContinuationToken = null;
            do
            {
                var queryResponse = _tableReference.ExecuteQuerySegmented<T>(null, tableContinuationToken, null, null);
                tableContinuationToken = queryResponse.ContinuationToken;
                allEntities.AddRange(queryResponse.Results);
            }
            while (tableContinuationToken != null);

        }
        catch (Exception ex)
        {
            throw new DALException(_tableName,_dataContext.BaseUri.OriginalString, "An error occured while querying data", ex);
        }
        return allEntities;
    }

}

but with error:

Error 121 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TElement' in the generic type or method 'Microsoft.WindowsAzure.Storage.Table.CloudTable.ExecuteQuerySegmented

解决方案

The reason you're getting only 900 results back is because you're running into continuation tokens. By default a single request to table service will return a maximum of 1000 entities. It could be less than 1000 entities (and even 0) but never more than 1000. If there are more entities available, then the table service returns a continuation token which should be used to fetch next set of entities.

So your code should look for the continuation token and should keep on fetching the entities till the time token is returned by table service. Do take a look at the sample code below:

private IEnumerable<T> FetchAllEntities()
{
    List<T> allEntities = new List<T>();
    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
    CloudTable table = storageAccount.CreateCloudTableClient().GetTableReference("MyTable");
    Microsoft.WindowsAzure.Storage.Table.TableContinuationToken tableContinuationToken = null;
    do
    {
        var queryResponse = table.ExecuteQuerySegmented<T>(null, tableContinuationToken, null, null);
        tableContinuationToken = queryResponse.ContinuationToken;
        allEntities.AddRange(queryResponse.Results);
    }
    while (tableContinuationToken != null);
    return allEntities;
}

Update

For your error, try changing the following

public class TableRepository<T> : IRepository<T> 
    where T : TableEntity

to

public class TableRepository<T> : IRepository<T> 
    where T : TableEntity, new()

notice the addition of new() after TableEntity.

这篇关于所有行复制到Azure的表的存储另一表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆