예제 #1
0
        public static T ConvertBackBase <T>(this Microsoft.Azure.Cosmos.Table.DynamicTableEntity entry, Type type)
        {
            MethodInfo method  = typeof(Microsoft.Azure.Cosmos.Table.TableEntity).GetMethod("ConvertBack");
            MethodInfo generic = method.MakeGenericMethod(type);

            var returnValue = generic.Invoke(entry, new object[] { new OperationContext() });

            return((T)returnValue);
        }
예제 #2
0
        } // RestoreTableFromBlobDirect

        private string RestoreFromStream(StreamReader InputFileStream, CosmosTable.CloudTable TableDest, string DestinationTableName)
        {
            bool   BatchWritten = true;
            string PartitionKey = String.Empty;

            CosmosTable.TableBatchOperation Batch = new CosmosTable.TableBatchOperation();
            int       BatchSize        = 100;
            int       BatchCount       = 0;
            long      TotalRecordCount = 0;
            TableSpec footer           = null;
            DynamicTableEntityJsonSerializer serializer = new DynamicTableEntityJsonSerializer();

            try
            {
                string InFileLine = InputFileStream.ReadLine();
                while (InFileLine != null)
                {
                    if (InFileLine.Contains("ProcessingMetaData") && InFileLine.Contains("Header"))
                    {
                        System.Console.WriteLine(String.Format("Header {0}", InFileLine));
                    }
                    else if (InFileLine.Contains("ProcessingMetaData") && InFileLine.Contains("Footer"))
                    {
                        footer = JsonConvert.DeserializeObject <TableSpec>(InFileLine);
                        System.Console.WriteLine(String.Format("Footer {0}", InFileLine));
                    }
                    else
                    {
                        CosmosTable.DynamicTableEntity dte2 = serializer.Deserialize(InFileLine);
                        if (String.Empty.Equals(PartitionKey))
                        {
                            PartitionKey = dte2.PartitionKey;
                        }
                        if (dte2.PartitionKey == PartitionKey)
                        {
                            Batch.InsertOrReplace(dte2);
                            BatchCount++;
                            TotalRecordCount++;
                            BatchWritten = false;
                        }
                        else
                        {
                            try
                            {
                                TableDest.ExecuteBatch(Batch);
                                Batch        = new CosmosTable.TableBatchOperation();
                                PartitionKey = dte2.PartitionKey;
                                Batch.InsertOrReplace(dte2);
                                BatchCount = 1;
                                TotalRecordCount++;
                                BatchWritten = false;
                            }
                            catch (Exception ex)
                            {
                                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
                            }
                        }
                        if (BatchCount >= BatchSize)
                        {
                            try
                            {
                                TableDest.ExecuteBatch(Batch);
                                PartitionKey = String.Empty;
                                Batch        = new CosmosTable.TableBatchOperation();
                                BatchWritten = true;
                                BatchCount   = 0;
                            }
                            catch (Exception ex)
                            {
                                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
                            }
                        }
                    }
                    InFileLine = InputFileStream.ReadLine();
                }  // while (InFileLine != null)

                //final batch
                if (!BatchWritten)
                {
                    try
                    {
                        TableDest.ExecuteBatch(Batch);
                        PartitionKey = String.Empty;
                    }
                    catch (Exception ex)
                    {
                        throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
                    }
                }

                if (null == footer)
                {
                    throw new RestoreFailedException(String.Format("Table '{0}' restore failed, no footer record found.", DestinationTableName));
                }
                else if (TotalRecordCount == footer.RecordCount)
                {
                    //OK, do nothing
                }
                else
                {
                    throw new RestoreFailedException(String.Format("Table '{0}' restore failed, records read {1} does not match expected count {2} in footer record.", DestinationTableName, TotalRecordCount, footer.RecordCount));
                }
            }
            catch (Exception ex)
            {
                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
            }

            return(String.Format("Restore to table '{0}' Successful; {1} entries.", DestinationTableName, TotalRecordCount));
        }
예제 #3
0
        /// <summary>
        /// Restore file created by BackupAzureTables to the destination table name specified.
        /// </summary>
        /// <param name="DestinationTableName">Name of the Azure Table to restore to -  may be different than name backed up originally.</param>
        /// <param name="InFilePathName">Complete file name and path containing the data to be restored.</param>
        /// <param name="TimeoutSeconds">Set timeout for table client.</param>
        /// <returns>A string indicating the table restored and record count.</returns>
        public string RestoreTableFromFile(string DestinationTableName, string InFilePathName, int TimeoutSeconds = 30)
        {
            if (String.IsNullOrWhiteSpace(DestinationTableName))
            {
                throw new ParameterSpecException("DestinationTableName is missing.");
            }

            if (Path.GetFullPath(InFilePathName) != InFilePathName)
            {
                throw new ParameterSpecException(String.Format("Invalid file name/path '{0}' specified.", InFilePathName));
            }
            else
            {
                if (!File.Exists(InFilePathName))
                {
                    throw new ParameterSpecException(String.Format("File '{0}' does not exist.", InFilePathName));
                }
            }

            TableSpec footer = null;

            try
            {
                if (!CosmosTable.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureTableConnectionSpec).Password, out CosmosTable.CloudStorageAccount StorageAccount))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                CosmosTable.CloudTableClient client    = CosmosTable.CloudStorageAccountExtensions.CreateCloudTableClient(StorageAccount, new CosmosTable.TableClientConfiguration());
                CosmosTable.CloudTable       TableDest = client.GetTableReference(DestinationTableName);
                TableDest.ServiceClient.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, 0, TimeoutSeconds);
                TableDest.CreateIfNotExists();

                DynamicTableEntityJsonSerializer serializer = new DynamicTableEntityJsonSerializer();

                bool   BatchWritten = true;
                string PartitionKey = String.Empty;
                CosmosTable.TableBatchOperation Batch = new CosmosTable.TableBatchOperation();
                int  BatchSize        = 100;
                int  BatchCount       = 0;
                long TotalRecordCount = 0;

                using (StreamReader InputFileStream = new StreamReader(InFilePathName))
                {
                    string InFileLine = InputFileStream.ReadLine();
                    while (InFileLine != null)
                    {
                        if (InFileLine.Contains("ProcessingMetaData") && InFileLine.Contains("Header"))
                        {
                            System.Console.WriteLine(String.Format("Header {0}", InFileLine));
                        }
                        else if (InFileLine.Contains("ProcessingMetaData") && InFileLine.Contains("Footer"))
                        {
                            footer = JsonConvert.DeserializeObject <TableSpec>(InFileLine);
                            System.Console.WriteLine(String.Format("Footer {0}", InFileLine));
                        }
                        else
                        {
                            CosmosTable.DynamicTableEntity dte2 = serializer.Deserialize(InFileLine);
                            if (String.Empty.Equals(PartitionKey))
                            {
                                PartitionKey = dte2.PartitionKey;
                            }
                            if (dte2.PartitionKey == PartitionKey)
                            {
                                Batch.InsertOrReplace(dte2);
                                BatchCount++;
                                TotalRecordCount++;
                                BatchWritten = false;
                            }
                            else
                            {
                                try
                                {
                                    TableDest.ExecuteBatch(Batch);
                                    Batch        = new CosmosTable.TableBatchOperation();
                                    PartitionKey = dte2.PartitionKey;
                                    Batch.InsertOrReplace(dte2);
                                    BatchCount = 1;
                                    TotalRecordCount++;
                                    BatchWritten = false;
                                }
                                catch (Exception ex)
                                {
                                    throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
                                }
                            }
                            if (BatchCount >= BatchSize)
                            {
                                try
                                {
                                    TableDest.ExecuteBatch(Batch);
                                    PartitionKey = String.Empty;
                                    Batch        = new CosmosTable.TableBatchOperation();
                                    BatchWritten = true;
                                    BatchCount   = 0;
                                }
                                catch (Exception ex)
                                {
                                    throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
                                }
                            }
                        }
                        InFileLine = InputFileStream.ReadLine();
                    }  // while (InFileLine != null)

                    //final batch
                    if (!BatchWritten)
                    {
                        try
                        {
                            TableDest.ExecuteBatch(Batch);
                            PartitionKey = String.Empty;
                        }
                        catch (Exception ex) {
                            throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
                        }
                    }
                } // using (StreamReader

                if (null == footer)
                {
                    throw new RestoreFailedException(String.Format("Table '{0}' restore failed, no footer record found.", DestinationTableName));
                }
                else if (TotalRecordCount == footer.RecordCount)
                {
                    //OK, do nothing
                }
                else
                {
                    throw new RestoreFailedException(String.Format("Table '{0}' restore failed, records read {1} does not match expected count {2} in footer record.", DestinationTableName, TotalRecordCount, footer.RecordCount));
                }

                return(String.Format("Restore to table '{0}' Successful; {1} entries.", DestinationTableName, TotalRecordCount));
            }
            catch (ConnectionException cex)
            {
                throw cex;
            }
            catch (RestoreFailedException rex)
            {
                throw rex;
            }
            catch (Exception ex)
            {
                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
            }
            finally
            {
            }
        }
 public static void SetCosmosTableName(this DynamicTableEntity tblEntity, string value)
 {
     tblEntity.Properties.Add("TableName", new EntityProperty(value));
 }
 public static string GetCosmosTableName(this DynamicTableEntity tblEntity)
 {
     return(tblEntity.Properties["TableName"].StringValue);
 }