Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Last active August 10, 2023 15:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dazfuller/4036464eea346f3182cddfe1436c034c to your computer and use it in GitHub Desktop.
Save dazfuller/4036464eea346f3182cddfe1436c034c to your computer and use it in GitHub Desktop.
Exporting data from a database to Parquet files in .NET (Core). This demo application targets a SQL Server database but the code could be re-used to target other database solutions.
{
"OutputFile": "data.parquet",
"ConnectionStrings": {
"local": "Server=<server name>;Initial Catalog=<database>;Integrated Security=SSPI"
}
}

MIT License

Copyright (c) 2018 Darren Fuller

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Parquet.Net" Version="2.1.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.2" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Parquet;
using Parquet.Data;
namespace ParquetDBDemo
{
public static class Program
{
public static async Task Main(string[] args)
{
// Open the configuration file and read in the required configuration
var configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
var configuration = configBuilder.Build();
var connectionString = configuration.GetConnectionString("local");
var outputFile = configuration["OutputFile"];
using (var connection = new SqlConnection(connectionString))
{
try
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
// Read in all the movie data
command.CommandText = "SELECT * FROM dbo.movies";
command.CommandType = CommandType.Text;
using (var reader = await command.ExecuteReaderAsync())
{
// Get the column name and data type information for building up the parquet schema
var columns = Enumerable.Range(0, reader.FieldCount)
.Select(o => new {FieldName = reader.GetName(o), DataType = reader.GetFieldType(o)})
.ToList();
var schema = new List<DataField>(columns.Count);
// Generate the Parquet schema from the column information, making sure that nullable types
// are handled by making each field capable of holding null values
schema.AddRange(from column in columns
let columnType = column.DataType.IsClass
? column.DataType
: typeof(Nullable<>).MakeGenericType(column.DataType)
let fieldType = typeof(DataField<>).MakeGenericType(columnType)
select (DataField) Activator.CreateInstance(fieldType, column.FieldName));
var dataset = new Parquet.Data.DataSet(schema);
var rowCount = 0;
// Read each record from the database
while (await reader.ReadAsync())
{
// Convert the current record into an object array, swapping DBNULL values for null before adding
// the record to the parquet dataset
var row = Enumerable.Range(0, reader.FieldCount)
.Select(o => reader.GetValue(o) == DBNull.Value ? null : reader.GetValue(o))
.ToArray();
dataset.Add(row);
rowCount++;
}
Console.WriteLine($"Records processed: {rowCount}");
// Output the dataset to a parquet file on disk
if (File.Exists(outputFile))
{
File.Delete(outputFile);
}
using (var writer = new ParquetWriter(File.OpenWrite(outputFile)))
{
writer.Write(dataset, CompressionMethod.Snappy);
}
}
}
}
catch (SqlException e)
{
Console.WriteLine(e);
throw;
}
finally
{
connection.Close();
}
}
}
}
}
@Moniezz
Copy link

Moniezz commented Sep 1, 2021

where can i get the stream of parquet if needed to be uploaded as blob in azure?

@dazfuller
Copy link
Author

where can i get the stream of parquet if needed to be uploaded as blob in azure?

If you're uploading existing parquet files to Azure Blob Storage then you're better of using FileStream to read the file as a byte array and copy it up, that makes sure you don't run into issues if the data is compressed using gzip or snappy.

@GVBhaskardev
Copy link

Many Thanks, this code perfectly works , how ever for 5gb of sql data it takes nearly 65 minutes.
Can you suggest any performance suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment