Migrating Azure Table Storage to Cosmos DB

Are there any tools to migrate Azure Table storage to Cosmos DB for access using the Table API? I found the Data Migration Tool , but it only supports accessing the table storage via the Document API.

UPDATE: I tried the Data Factory copy data function to copy from Table Storage to Document DB (now I think it is cosomosDb). But it didn't copy the data to the Cosmos database, although the Data Factory pipeline says that it copied this and that amount of data, but I don't see any objects in the Target Cosmos Db table.

What's the best way to migrate to Cosmos database and use tabular APIs?

+3


source to share


1 answer


What's the best way to migrate to Cosmos database and use tabular APIs?

If I understand correctly, you want to migrate Azure Table Storage to the Cosmos API database. If this is the case and the program is possible, we can do it using the Azure Table Windows Azure Storage Premium SDK. This is a preview. Below is my demo code, it works correctly on the side.

1. We need to implement TableEntity and add our property settings. propertycase sensitive

    public class CustomerEntity : TableEntity
    {

        public CustomerEntity() { }
        public Type tableEntityProperty { get; set; } 
        ...
    }

      

2.Add Get table function



public static CloudTable GetTable(string connectionstring,string tableName)
        {
            CloudStorageAccount destStorageAccount = CloudStorageAccount.Parse(connectionstring);
            CloudTableClient destTableClient = destStorageAccount.CreateCloudTableClient();
            CloudTable destTable = destTableClient.GetTableReference(tableName);
            destTable.CreateIfNotExists();
            return destTable;
        }

      

3. Try to query table items from the Azure storage table and add them to the Azure cosmos table.

  var sourceConnectionString = "DefaultEndpointsProtocol=https;AccountName=storageName;AccountKey=yourkey;EndpointSuffix=core.windows.net";
  var destConnectionstring = "DefaultEndpointsProtocol=https;AccountName=cosmostableAPIAccount;AccountKey=yourkey;TableEndpoint=https://tomtableapi.documents.azure.com";

  CloudTable sourceTable = GetTable(sourceConnectionString, "source table");
  CloudTable destTable = GetTable(destConnectionstring, "dest table");
  TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>();

     foreach (CustomerEntity entity in sourceTable.ExecuteQuery(query))
     {
          TableOperation insertOperation = TableOperation.Insert(entity);
          // Execute the insert operation.
          destTable.Execute(insertOperation);
     }

      

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.DocumentDB" version="1.14.0" targetFramework="net462" />
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net462" />
  <package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net462" />
  <package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net462" />
  <package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net462" />
  <package id="Microsoft.OData.Core" version="7.2.0" targetFramework="net462" />
  <package id="Microsoft.OData.Edm" version="7.2.0" targetFramework="net462" />
  <package id="Microsoft.Spatial" version="7.2.0" targetFramework="net462" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.3" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net462" />
  <package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net462" />
  <package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net462" />
  <package id="System.Linq.Queryable" version="4.0.0" targetFramework="net462" />
  <package id="System.Net.Requests" version="4.0.11" targetFramework="net462" />
  <package id="System.Spatial" version="5.8.2" targetFramework="net462" />
  <package id="WindowsAzure.Storage-PremiumTable" version="0.1.0-preview" targetFramework="net462" />
</packages>

      

+2


source







All Articles