Ejemplo n.º 1
0
        public void DelimitedHeaderWithMultipleFields()
        {
            var factory     = new DelimitedColumnFactory();
            var testSubject = new DelimitedHeader(
                new[] { "field1:int", "field2:bool", "field3" }.Select(factory.Create));

            Assert.Equal(typeof(int), testSubject.DelimitedColumns[0].Type);
            Assert.Equal(typeof(bool), testSubject.DelimitedColumns[1].Type);
            Assert.Equal(typeof(string), testSubject.DelimitedColumns[2].Type);

            Assert.Equal("field1", testSubject[0]);
            Assert.Equal("field2", testSubject[1]);
            Assert.Equal("field3", testSubject[2]);

            Assert.Equal(0, testSubject["field1"]);
            Assert.Equal(1, testSubject["field2"]);
            Assert.Equal(2, testSubject["field3"]);

            Assert.Equal(3, testSubject.DelimitedColumns.Length);
        }
Ejemplo n.º 2
0
        public void SqlBulkCopy_DelimitedParser()
        {
            string path = Path.Combine(
                Directory.GetCurrentDirectory(),
                Path.GetRandomFileName());

            path = Path.ChangeExtension(path, "mdf");

            // Use LocalDB as the temporary database for the test.  Substitute with a
            // connection string more appropriate to your environment.
            string connectionString = $@"Server=(localdb)\MSSQLLocalDB;AttachDbFilename={path};Trusted_Connection=True";

            // Build up some fake Cosmos output.
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("1,http://support.microsoft.com/kb/100,100,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/101,500,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/200,700,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/321,902,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/732,199,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/376,112,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/546,414,0.01,0.25,0.5,0.75,2.1,5.01");

            // Read in the fake Cosmos output.  Instead of using this stream, why not use
            //  -> new CosmosStream("http://cosmos05.osdinfra.net:88/cosmos/0365exp.adhoc/my/stream.csv");

            var factory         = new DelimitedColumnFactory();
            var delimitedHeader = new DelimitedHeader(
                factory.Create(new[] { "ID:long", "Uri", "Count:long", "Min:double", "P25:double", "P50:double", "P75:double", "P99:double", "Max:double" }));

            var settings = new DelimitedParserSettings();

            settings.DelimitedHeader = delimitedHeader;
            settings.Delimiter       = ',';

            var parser = DelimitedParser.Create(settings, sb.ToStream());

            // Ensure the database is created before bulk loading the data.
            using (var context = new Context(connectionString))
            {
                Assert.False(context.WebMetrics.Any());
            }

            // Bulk load the data.
            using (var transactionScope = new TransactionScope(
                       TransactionScopeOption.Required,
                       new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connectionString))
                {
                    sqlBulkCopy.DestinationTableName = "WebMetrics";
                    sqlBulkCopy.BatchSize            = 10000;

                    sqlBulkCopy.WriteToServer(parser);
                    transactionScope.Complete();
                }

            // Read data back out of the DB...party on it.
            using (var context = new Context(connectionString))
            {
                Assert.Equal(7, context.WebMetrics.Count());

                // There are seven unique IDs.
                var set = new HashSet <long>(context.WebMetrics.Select(x => x.ID));
                Assert.Equal(7, set.Count);

                Assert.True(context.WebMetrics.All(x => x.Min == 0.01));
                Assert.True(context.WebMetrics.All(x => x.Max == 5.01));
            }
        }