-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split out MDA into a separate library
- Loading branch information
Showing
10 changed files
with
153 additions
and
93 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.Analysis; | ||
using Parquet.Data; | ||
using Parquet.Data.Analysis; | ||
using Parquet.Schema; | ||
|
||
namespace Parquet { | ||
/// <summary> | ||
/// Defines extension methods to simplify Parquet usage | ||
/// </summary> | ||
public static class AnalysisExtensions { | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="inputStream"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public static async Task<DataFrame> ReadParquetAsDataFrameAsync( | ||
this Stream inputStream, CancellationToken cancellationToken = default) { | ||
using ParquetReader reader = await ParquetReader.CreateAsync(inputStream, cancellationToken: cancellationToken); | ||
|
||
var dfcs = new List<DataFrameColumn>(); | ||
//var readableFields = reader.Schema.DataFields.Where(df => df.MaxRepetitionLevel == 0).ToList(); | ||
List<DataField> readableFields = reader.Schema.Fields | ||
.Select(df => df as DataField) | ||
.Where(df => df != null) | ||
.Cast<DataField>() | ||
.ToList(); | ||
var columns = new List<DataFrameColumn>(); | ||
|
||
for(int rowGroupIndex = 0; rowGroupIndex < reader.RowGroupCount; rowGroupIndex++) { | ||
using ParquetRowGroupReader rgr = reader.OpenRowGroupReader(rowGroupIndex); | ||
|
||
for(int dataFieldIndex = 0; dataFieldIndex < readableFields.Count; dataFieldIndex++) { | ||
DataColumn dc = await rgr.ReadColumnAsync(readableFields[dataFieldIndex], cancellationToken); | ||
|
||
if(rowGroupIndex == 0) { | ||
dfcs.Add(DataFrameMapper.ToDataFrameColumn(dc)); | ||
} else { | ||
DataFrameMapper.AppendValues(dfcs[dataFieldIndex], dc); | ||
} | ||
} | ||
} | ||
|
||
return new DataFrame(dfcs); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="df"></param> | ||
/// <param name="outputStream"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public static async Task WriteAsync(this DataFrame df, Stream outputStream, CancellationToken cancellationToken = default) { | ||
// create schema | ||
var schema = new ParquetSchema( | ||
df.Columns.Select(col => new DataField(col.Name, col.DataType.GetNullable()))); | ||
|
||
using ParquetWriter writer = await ParquetWriter.CreateAsync(schema, outputStream, cancellationToken: cancellationToken); | ||
using ParquetRowGroupWriter rgw = writer.CreateRowGroup(); | ||
|
||
int i = 0; | ||
foreach(DataFrameColumn? col in df.Columns) { | ||
if(col == null) | ||
throw new InvalidOperationException("unexpected null column"); | ||
|
||
Array data = DataFrameMapper.GetTypedDataFast(col); | ||
var parquetColumn = new DataColumn(schema.DataFields[i], data); | ||
|
||
await rgw.WriteColumnAsync(parquetColumn, cancellationToken); | ||
|
||
i += 1; | ||
} | ||
} | ||
|
||
static Type GetNullable(this Type t) { | ||
TypeInfo ti = t.GetTypeInfo(); | ||
|
||
if(ti.IsClass) { | ||
return t; | ||
} | ||
|
||
Type nt = typeof(Nullable<>); | ||
return nt.MakeGenericType(t); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net6.0;netstandard2.1;netstandard2.0</TargetFrameworks> | ||
<Company></Company> | ||
<PackageId>Parquet.Net.Data.Analysis</PackageId> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<WarningsAsErrors /> | ||
<DocumentationFile>bin\Debug\$(TargetFramework)\Parquet.Net.Data.Analysis.xml</DocumentationFile> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
<PackageTags>apache parquet dotnet core net c# f# windows linux macos ios android xboxone xbox</PackageTags> | ||
<LangVersion>latest</LangVersion> | ||
<Nullable>enable</Nullable> | ||
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Parquet\Parquet.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Data.Analysis" Version="0.21.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Update="DataFrameMapper.cs"> | ||
<DependentUpon>DataFrameMapper.tt</DependentUpon> | ||
<DesignTime>True</DesignTime> | ||
<AutoGen>True</AutoGen> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="DataFrameMapper.tt"> | ||
<LastGenOutput>DataFrameMapper.cs</LastGenOutput> | ||
<Generator>TextTemplatingFileGenerator</Generator> | ||
</None> | ||
</ItemGroup> | ||
|
||
|
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters