Class for creating a template BsonDocument using SSIS input column specification. Supports nested documents, when using dot notation in SSIS parameters. Arrays are not supported.
        public void TestBsonInsertDocument_Create()
        {
            List<DestinationColumnInfo> cols = new List<DestinationColumnInfo>();
            cols.Add(new DestinationColumnInfo { BufferColumnIndex = 0, ColumnName = "a", DataType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_I4 });
            cols.Add(new DestinationColumnInfo { BufferColumnIndex = 1, ColumnName = "b.b1", DataType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_DATE });
            cols.Add(new DestinationColumnInfo { BufferColumnIndex = 2, ColumnName = "c", DataType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_R8 });
            cols.Add(new DestinationColumnInfo { BufferColumnIndex = 3, ColumnName = "b.b2", DataType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_R8 });

            BsonInsertDocument template = new BsonInsertDocument(cols);
            BsonDocument doc = template.Create();

            Assert.IsNotNull(doc["a"]);
            Assert.IsNotNull(doc["b"]);
            Assert.IsNotNull(doc["b"].AsBsonDocument["b1"]);
        }
Ejemplo n.º 2
0
        public override void ProcessInput(int inputID, PipelineBuffer buffer)
        {
            bool pbCancel = false;

            if (String.IsNullOrEmpty(ComponentMetaData.CustomPropertyCollection[MONGO_COLL].Value))
            {
                ComponentMetaData.FireError(0, "ProcessInput", "Missing MongoDB collection name", "", 0, out pbCancel);
                return;
            }
            int iBatchSize;

            if (!Int32.TryParse(ComponentMetaData.CustomPropertyCollection[MONGO_BATCH_SIZE].Value, out iBatchSize))
            {
                ComponentMetaData.FireError(0, "ProcessInput", "Failed to parse MongoDB insert batch size", "", 0, out pbCancel);
                return;
            }
            MongoCollection     collection = _db.GetCollection(ComponentMetaData.CustomPropertyCollection[MONGO_COLL].Value);
            List <BsonDocument> batch      = new List <BsonDocument>();
            int j = 1;

            // Create an empty template document using input column specification
            BsonInsertDocument template = new BsonInsertDocument(_colInfos);
            BsonDocument       doc      = template.Create();

            while (buffer.NextRow())
            {
                doc = template.DeepClone();
                for (int i = 0; i < _colInfos.Count; i++)
                {
                    doc.InsertDotNotated(_colInfos[i], CreateBsonValue(buffer[_colInfos[i].BufferColumnIndex], _colInfos[i]));
                }
                batch.Add(doc);
                j++;
                if (j > iBatchSize)
                {
                    collection.InsertBatch(batch);
                    ComponentMetaData.IncrementPipelinePerfCounter(103, (uint)batch.Count);
                    j     = 1;
                    batch = new List <BsonDocument>();
                }
            }

            if (batch.Count > 0)
            {
                collection.InsertBatch(batch);
                ComponentMetaData.IncrementPipelinePerfCounter(103, (uint)batch.Count);
            }
        }
        public override void ProcessInput(int inputID, PipelineBuffer buffer)
        {
            bool pbCancel = false;
            if (String.IsNullOrEmpty(ComponentMetaData.CustomPropertyCollection[MONGO_COLL].Value))
            {
                ComponentMetaData.FireError(0, "ProcessInput", "Missing MongoDB collection name", "", 0, out pbCancel);
                return;
            }
            int iBatchSize;
            if (!Int32.TryParse(ComponentMetaData.CustomPropertyCollection[MONGO_BATCH_SIZE].Value, out iBatchSize))
            {
                ComponentMetaData.FireError(0, "ProcessInput", "Failed to parse MongoDB insert batch size", "", 0, out pbCancel);
                return;
            }
            MongoCollection collection = _db.GetCollection(ComponentMetaData.CustomPropertyCollection[MONGO_COLL].Value);
            List<BsonDocument> batch = new List<BsonDocument>();
            int j = 1;

            // Create an empty template document using input column specification
            BsonInsertDocument template = new BsonInsertDocument(_colInfos);
            BsonDocument doc = template.Create();

            while (buffer.NextRow())
            {
                doc = template.DeepClone();
                for (int i = 0; i < _colInfos.Count; i++)
                {
                    doc.InsertDotNotated(_colInfos[i], CreateBsonValue(buffer[_colInfos[i].BufferColumnIndex], _colInfos[i]));
                }
                batch.Add(doc);
                j++;
                if (j > iBatchSize)
                {
                    collection.InsertBatch(batch);
                    ComponentMetaData.IncrementPipelinePerfCounter(103, (uint)batch.Count);
                    j = 1;
                    batch = new List<BsonDocument>();
                }
            }

            if (batch.Count > 0)
            {
                collection.InsertBatch(batch);
                ComponentMetaData.IncrementPipelinePerfCounter(103, (uint)batch.Count);
            }
        }