Exemplo n.º 1
0
        static async Task HandleChangesAsync(IReadOnlyCollection <object> changes, CancellationToken cancellationToken)
        {
            MongoSink.BlobUploader blobUploader = new MongoSink.BlobUploader(
                blobConnectionString,
                blobContainer);

            MongoSink.MongoSink mongoSink = new MongoSink.MongoSink(destConnectionString,
                                                                    destDBName,
                                                                    destContainerName,
                                                                    insertRetries,
                                                                    blobConnectionString,
                                                                    blobContainer,
                                                                    isUpsert);
            SourceTransformation sourceTransformation = null;

            if (!string.IsNullOrEmpty(sourceKeyTransformationString))
            {
                try
                {
                    sourceTransformation = JsonConvert.DeserializeObject <SourceTransformation>
                                               (sourceKeyTransformationString);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            Transformation transformation = new Transformation(transformationType,
                                                               sourceKeys,
                                                               destinationKey,
                                                               delimiter,
                                                               sourceTransformation,
                                                               swapKeyValues);

            Console.WriteLine("Received data from the source:" + changes.Count.ToString());
            foreach (object item in changes)
            {
                try
                {
                    //Console.WriteLine(item.ToString());
                    JObject objects = JObject.Parse(item.ToString());
                    objects.Remove("_lsn");
                    objects.Remove("_metadata");
                    objects.Remove("_id");
                    string json = objects.ToString();
                    MongoDB.Bson.BsonDocument bdoc = CosmosDbSchemaDecoder.GetBsonDocument(json, false);
                    if (transformationType != "NONE")
                    {
                        transformation.Execute(bdoc);
                    }
                    await mongoSink.InsertAsync(bdoc, source.Token);
                }
                catch (Exception exp)
                {
                    blobUploader.Upload(item.ToString());
                }
            }
        }
 public Transformation(string type,
                       string sourceKeys,
                       string destinationKey,
                       string delimiter,
                       SourceTransformation sourceKeyTransformation,
                       string swapKeyValues)
 {
     this.Type                    = type.ToUpper();
     this.SourceKeys              = sourceKeys;
     this.DestinationKey          = destinationKey;
     this.Delimiter               = delimiter;
     this.SourceKeyTransformation = sourceKeyTransformation;
     this.SwapKeyValues           = swapKeyValues;
 }