-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
389 lines (330 loc) · 17.5 KB
/
Copy pathProgram.cs
File metadata and controls
389 lines (330 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="Madeira Ltd.">
// Copyright (c) Madeira Data Solutions. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace Microsoft.Cyber.CyberSerialization
{
class Program
{
public static IConfigurationRoot configuration;
// This is used for making column list dynamic based on fieldsToCopy
static DataTable CreateTable(string[] fieldsList)
{
DataTable dt = new DataTable();
foreach (string fieldName in fieldsList)
{
dt.Columns.Add(fieldName, typeof(string));
}
return dt;
}
static void Main(string[] args)
{
Console.Clear();
MainAsync(args).Wait();
}
static async Task MainAsync(string[] args)
{
// Create service collection
ServiceCollection serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
// Create service provider
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
string sourceEndPoint = configuration["CosmosDB:sourceEndPoint"]; // "https://<cosmosdbaccount>.documents.azure.com:443/";
string sourceAuthKey = configuration["CosmosDB:sourceAuthKey"]; // "AccessKey";
string sourceDatabase = configuration["CosmosDB:sourceDatabase"]; // "CosmosDbDatabase";
string sourceCollection = configuration["CosmosDB:sourceCollection"]; // "CosmosDbCollection";
string sourceQuery = configuration["CosmosDB:sourceQuery"]; // "SELECT c.Field1, c.Field2, c.Filed3, ... FROM c";
int rowsPerFetch = int.Parse(configuration["CosmosDB:sourceMaxCountPerFetch"]); // 1000;
string targetHost = configuration["SQLServer:targetHost"]; // "<SqlServerName>.database.windows.net";
string targetDatabase = configuration["SQLServer:targetDatabase"]; //"SqlDatabaseName";
string targetUsername = configuration["SQLServer:targetUsername"]; //"SqlUsername";
string targetPassword = configuration["SQLServer:targetPassword"]; //"SqlPassword";
string? targetTable = configuration["SQLServer:targetTable"]; // "SqlTargetStagingTable";
bool truncateTargetTable = bool.Parse(configuration["SQLServer:truncateTargetTable"]); // true
string? targetProcedure = configuration["SQLServer:targetProcedureTVP"]; // "SqlStoredProcedureWithTVP";
string? mergeProcedure = configuration["SQLServer:mergeProcedure"]; // "SqlStoredProcedureForMerge";
int rowsPerChunk = int.Parse(configuration["SQLServer:rowsPerChunk"]); // 5000;
bool useBulkCopy = bool.Parse(configuration["SQLServer:useBulkCopy"]); // true;
int maxRetries = int.Parse(configuration["SQLServer:maxRetries"]); // 10;
int delaySecondsBetweenRetries = int.Parse(configuration["SQLServer:delaySecondsBetweenRetries"]); // 30;
string[] fieldsToCopy = configuration.GetSection("FieldsToCopy").Get<string[]>();
#region Settings Validation
if (String.IsNullOrEmpty(targetProcedure) && !useBulkCopy)
{
throw new Exception("targetProcedure must be specified when BulkCopy is not used");
}
if (String.IsNullOrEmpty(targetTable) && useBulkCopy)
{
throw new Exception("targetTable must be specified when using BulkCopy");
}
#endregion
#region Connect to CosmosDB
DocumentClient client = new DocumentClient(
new Uri(sourceEndPoint),
sourceAuthKey,
new ConnectionPolicy()
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp,
RetryOptions = new RetryOptions()
{
MaxRetryAttemptsOnThrottledRequests = 10,
MaxRetryWaitTimeInSeconds = 30
}
});
Uri sourceDatabaseUri = UriFactory.CreateDatabaseUri(sourceDatabase);
DocumentCollection customSourceContainer = client.CreateDocumentCollectionQuery(sourceDatabaseUri)
.Where(c => c.Id == sourceCollection).AsEnumerable().FirstOrDefault();
FeedOptions option = new FeedOptions
{
EnableCrossPartitionQuery = true,
MaxItemCount = rowsPerFetch,
MaxBufferedItemCount = rowsPerChunk,
EnableScanInQuery = true
};
#endregion
#region Migration Process
DataTable dataTable = CreateTable(fieldsToCopy);
int i = 0;
int errorsCount = 0;
using (var queryable = client.CreateDocumentQuery(customSourceContainer.SelfLink, sourceQuery, option).AsDocumentQuery())
{
#region Connect to Target SQL Server
SqlConnection sqlConnection = new SqlConnection(
string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
targetHost, targetDatabase, targetUsername, targetPassword));
sqlConnection.Open();
sqlConnection.InfoMessage += SqlConnection_InfoMessage;
// Truncate staging table
if (truncateTargetTable && !String.IsNullOrEmpty(targetTable))
{
using (SqlCommand cmd = sqlConnection.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("TRUNCATE TABLE {0}", targetTable);
Console.WriteLine("{1} {2} Truncating {0}...", targetTable, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
cmd.ExecuteNonQuery();
}
}
#endregion
Console.WriteLine("{2} {3} Starting process (chunk size: {0}, fetch size: {1})", rowsPerChunk, rowsPerFetch, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
while (queryable.HasMoreResults)
{
Console.WriteLine("{1} {2} Buffered: {0} (decompressing)", dataTable.Rows.Count, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
foreach (dynamic item in await queryable.ExecuteNextAsync())
{
// Iterate through items
i++;
insertion_start:
// Retry connection if necessary
if (sqlConnection == null || sqlConnection.State != ConnectionState.Open)
{
if (sqlConnection != null) sqlConnection.Dispose();
Thread.Sleep(delaySecondsBetweenRetries * 1000);
sqlConnection = new SqlConnection(
string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
targetHost, targetDatabase, targetUsername, targetPassword));
sqlConnection.Open();
sqlConnection.InfoMessage += SqlConnection_InfoMessage;
}
try
{
// Convert Dynamic Object into a Dictionary
IDictionary<string, object> d = (IDictionary<string, object>)item;
// Construct a new DataRow
DataRow newRow = dataTable.NewRow();
// If fields list was provided, use that
if (fieldsToCopy.Length > 0)
{
foreach (string field in fieldsToCopy)
{
if (d.ContainsKey(field) && d[field] != null)
{
newRow.SetField<string>(field, d[field].ToString());
}
else
{
newRow.SetField<string>(field, String.Empty);
}
}
}
// Otherwise, try to generate row fields based on query fields returned
else
{
foreach (string field in d.Keys)
{
newRow.SetField<string>(field, d[field].ToString());
}
}
// Add the new row in the DataTable
dataTable.Rows.Add(newRow);
// Check if should flush staging data using SqlBulkCopy
if (useBulkCopy && dataTable.Rows.Count >= rowsPerChunk)
{
insertBulk(sqlConnection, targetTable, dataTable);
dataTable.Clear();
mergeStaging(sqlConnection, mergeProcedure);
i = 0;
}
else if (!useBulkCopy)
{
// Check if should flush staging data using Table-Valued-Parameters
// TVP contents, as best practice, should not exceed 1000 rows
if ((dataTable.Rows.Count % 1000) == 0 || dataTable.Rows.Count >= rowsPerChunk)
{
insertChunk(sqlConnection, targetProcedure, dataTable);
if (i >= rowsPerChunk)
{
mergeStaging(sqlConnection, mergeProcedure);
i = 0;
}
dataTable.Clear();
}
}
// Reset errors count if insertion is successful
if (errorsCount > 0)
{
errorsCount = 0;
}
}
catch (Exception e)
{
if (errorsCount < maxRetries)
{
Console.WriteLine("{3} {4} ERROR (attempt {0} out of {1}): {2}", errorsCount, maxRetries, e.Message, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
errorsCount++;
if (sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
goto insertion_start;
}
else
{
Console.WriteLine();
Console.WriteLine("Last item:");
Console.WriteLine("===============================================");
Console.WriteLine(item);
Console.WriteLine("===============================================");
Console.WriteLine();
throw e;
}
}
}
}
// If more data remained in buffer object
Console.WriteLine("{1} {2} Buffered: {0} (finalizing)", dataTable.Rows.Count, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
// Check if should flush staging data using SqlBulkCopy
if (useBulkCopy && dataTable.Rows.Count > 0)
{
insertBulk(sqlConnection, targetTable, dataTable);
}
// Check if should flush staging data using Table-Valued-Parameters
else if (dataTable.Rows.Count > 0)
{
insertChunk(sqlConnection, targetProcedure, dataTable);
}
dataTable.Clear();
mergeStaging(sqlConnection, mergeProcedure);
Console.WriteLine("{0} {1} Migration is complete!", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
sqlConnection.Close();
}
#endregion
}
private static void SqlConnection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
foreach (SqlError item in e.Errors)
{
if (item.Class >= 1 && item.Class <= 9)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{5} {6} Warning {0} Severity {1} Line {2} ({3}): {4}", item.Number, item.Class, item.LineNumber, item.Procedure, item.Message, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
Console.ResetColor();
}
else if (item.Class > 10)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{5} {6} Error {0} Severity {1} Line {2} ({3}): {4}", item.Number, item.Class, item.LineNumber, item.Procedure, item.Message, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
Console.ResetColor();
}
else
{
Console.WriteLine("{1} {2} {0}", item.Message, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
}
}
}
private static void insertChunk(SqlConnection sqlConnection, string targetProcedure, DataTable dataTable)
{
Console.WriteLine("{1} {2} Flushing {0} row(s) using TVP...", dataTable.Rows.Count, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
using (SqlCommand cmd = sqlConnection.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = targetProcedure;
cmd.CommandTimeout = 3600000;
System.Data.SqlClient.SqlParameter sqlParam = cmd.Parameters.AddWithValue("@Batch", dataTable);
sqlParam.SqlDbType = SqlDbType.Structured;
cmd.ExecuteNonQuery();
}
}
private static void mergeStaging(SqlConnection sqlConnection, string? mergeProcedure)
{
if (!String.IsNullOrEmpty(mergeProcedure))
{
Console.WriteLine("{0} {1} Merging...", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
using (SqlCommand cmd = sqlConnection.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = mergeProcedure;
cmd.CommandTimeout = 3600000;
cmd.ExecuteNonQuery();
}
}
}
private static void insertBulk(SqlConnection sqlConnection, string targetTable, DataTable dataTable)
{
Console.WriteLine("{1} {2} Flushing {0} row(s) using BulkCopy...", dataTable.Rows.Count, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString());
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnection))
{
bulkCopy.DestinationTableName = targetTable;
bulkCopy.BulkCopyTimeout = 3600000;
bulkCopy.BatchSize = 1000;
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(dataTable);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
private static void ConfigureServices(IServiceCollection serviceCollection)
{
// Build configuration
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false)
.Build();
// Add access to generic IConfigurationRoot
serviceCollection.AddSingleton<IConfigurationRoot>(configuration);
// Add app
serviceCollection.AddTransient<Program>();
}
}
}