コード例 #1
0
        static void WriteCsv(CsvZipPackage csvz, string csv)
        {
            Console.WriteLine($"Processing {Path.GetFileName(csv)}: ");
            var data     = CsvDataReader.Create(csv);
            var analyzer = new SchemaAnalyzer();

            Console.Write($"  Analyzing. ");
            var sw     = Stopwatch.StartNew();
            var result = analyzer.Analyze(data);

            Console.WriteLine(sw.Elapsed.ToString());

            var schema    = result.GetSchema();
            var csvSchema = new CsvSchema(schema);

            var csvOpts = new CsvDataReaderOptions {
                Schema = csvSchema
            };

            Console.Write($"  Writing.   ");
            data = CsvDataReader.Create(csv, csvOpts);
            sw   = Stopwatch.StartNew();
            var entry = csvz.CreateEntry(Path.GetFileName(csv));

            entry.WriteData(data);
            Console.WriteLine(sw.Elapsed.ToString());
        }
コード例 #2
0
        public override int Execute(CommandContext context, AnalyzeSettings settings)
        {
            var filename = settings.File;
            var output   = settings.Schema ?? filename + ".schema";

            Stream oStream =
                output == "."
                                ? Console.OpenStandardOutput()
                                : File.Create(output);

            var tr = new StreamReader(filename);

            for (int i = 0; i < settings.Skip; i++)
            {
                tr.ReadLine();
            }

            var csv = CsvDataReader.Create(tr);
            var a   = new SchemaAnalyzer(new SchemaAnalyzerOptions {
                AnalyzeRowCount = settings.Lines
            });
            var re = a.Analyze(csv);
            var sb = re.GetSchemaBuilder();

            foreach (var col in sb)
            {
                // TODO: think more about how to handle columns size
                col.ColumnSize = null;
            }
            var schema = sb.Build();

            using var tw = new StreamWriter(oStream);
            tw.Write(schema.ToString().Replace(",", "," + Environment.NewLine));
            return(0);
        }