|
| 1 | +using Cosmos.DataTransfer.Interfaces; |
| 2 | +using Cosmos.DataTransfer.ParqExtension.Settings; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Parquet.Schema; |
| 6 | +using Parquet; |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +using Parquet.Data; |
| 9 | +using System.Data; |
| 10 | + |
| 11 | +namespace Cosmos.DataTransfer.ParquetExtension |
| 12 | +{ |
| 13 | + [Export(typeof(IDataSinkExtension))] |
| 14 | + public class ParqDataSinkExtension : IDataSinkExtension |
| 15 | + { |
| 16 | + public string DisplayName => "Parquet"; |
| 17 | + public List<ParquetDataCol> parquetDataCols = new List<ParquetDataCol>(); |
| 18 | + public async Task WriteAsync(IAsyncEnumerable<IDataItem> dataItems, IConfiguration config, IDataSourceExtension dataSource, ILogger logger, CancellationToken cancellationToken) |
| 19 | + { |
| 20 | + var settings = config.Get<ParquetSinkSettings>(); |
| 21 | + settings.Validate(); |
| 22 | + if (settings != null && settings.FilePath != null) |
| 23 | + { |
| 24 | + logger.LogInformation("Writing to file '{FilePath}", settings.FilePath); |
| 25 | + |
| 26 | + await foreach (var item in dataItems.WithCancellation(cancellationToken)) |
| 27 | + { |
| 28 | + ProcessColumns(item); |
| 29 | + } |
| 30 | + var schema = CreateSchema(); |
| 31 | + CreateParquetColumns(); |
| 32 | + await SaveFile(schema, settings.FilePath, cancellationToken); |
| 33 | + logger.LogInformation("Completed writing data to file '{FilePath}'", settings.FilePath); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private void ProcessColumns(IDataItem item) |
| 38 | + { |
| 39 | + var itemcolumns = item.GetFieldNames(); |
| 40 | + foreach (var col in itemcolumns) |
| 41 | + { |
| 42 | + var current = parquetDataCols.FirstOrDefault(c => c.ColumnName == col); |
| 43 | + var colval = item.GetValue(col); |
| 44 | + var coltype = System.Type.Missing.GetType(); |
| 45 | + if (colval != null) |
| 46 | + { |
| 47 | + coltype = colval.GetType(); |
| 48 | + } |
| 49 | + if (current == null) |
| 50 | + { |
| 51 | + var newcol = new ParquetDataCol(col, coltype); |
| 52 | + newcol.ColumnData.Add(colval); |
| 53 | + parquetDataCols.Add(newcol); |
| 54 | + } |
| 55 | + else if (coltype != System.Type.Missing.GetType() && current.ColumnType != coltype) |
| 56 | + { |
| 57 | + if (current != null) |
| 58 | + { |
| 59 | + current.ColumnType = coltype; |
| 60 | + if (coltype != null) |
| 61 | + { |
| 62 | + current.ParquetDataType = new DataField(col, coltype, true); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + if (current != null) |
| 67 | + { |
| 68 | + current.ColumnData.Add(colval); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void CreateParquetColumns() |
| 74 | + { |
| 75 | + for (var i = 0; i < parquetDataCols.Count; i++) |
| 76 | + { |
| 77 | + |
| 78 | + var current = parquetDataCols[i]; |
| 79 | + switch (current.ParquetDataType.ClrType.Name) |
| 80 | + { |
| 81 | + case "String": |
| 82 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<String?>().ToArray()); |
| 83 | + break; |
| 84 | + case "Int32": |
| 85 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<Int32?>().ToArray()); |
| 86 | + break; |
| 87 | + case "Int16": |
| 88 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<Int16?>().ToArray()); |
| 89 | + break; |
| 90 | + case "Int64": |
| 91 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<Int64?>().ToArray()); |
| 92 | + break; |
| 93 | + case "DateTime": |
| 94 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<DateTime?>().ToArray()); |
| 95 | + break; |
| 96 | + case "Boolean": |
| 97 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<Boolean?>().ToArray()); |
| 98 | + break; |
| 99 | + case "Float": |
| 100 | + current.ParquetDataColumn = new Parquet.Data.DataColumn(current.ParquetDataType, current.ColumnData.Cast<float?>().ToArray()); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private ParquetSchema CreateSchema() |
| 107 | + { |
| 108 | + var arr = new List<Field>(); |
| 109 | + for (var i = 0; i < parquetDataCols.Count; i++) |
| 110 | + { |
| 111 | + arr.Add(parquetDataCols[i].ParquetDataType); |
| 112 | + } |
| 113 | + return new ParquetSchema(arr); |
| 114 | + } |
| 115 | + |
| 116 | + private async Task<bool> SaveFile(ParquetSchema schema, string filepath, CancellationToken cancellationToken) |
| 117 | + { |
| 118 | + using Stream fs = File.OpenWrite(filepath); |
| 119 | + using ParquetWriter writer = await ParquetWriter.CreateAsync(schema, fs); |
| 120 | + using ParquetRowGroupWriter groupWriter = writer.CreateRowGroup(); |
| 121 | + foreach (var col in parquetDataCols) |
| 122 | + { |
| 123 | + if (col.ParquetDataColumn != null) |
| 124 | + { |
| 125 | + await groupWriter.WriteColumnAsync(col.ParquetDataColumn, cancellationToken); |
| 126 | + } |
| 127 | + } |
| 128 | + return true; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments