Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Last active July 2, 2024 12:37
Show Gist options
  • 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();
}
}
}
}
}
@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.

@flarco
Copy link

flarco commented Jul 2, 2024

You can do this easily with sling, check out: https://github.com/slingdata-io/sling-cli

# set connection via env var
export mssql='sqlserver://...'

# test connection
sling conns test mssql

# run export for many tables
sling run --src-conn mssql --src-stream 'my_schema.*' --tgt-object 'file://{stream_schema}/{stream_table}.parquet'

# run export for one table
sling run --src-conn mssql --src-stream 'my_schema.my_table' --tgt-object 'file://my_folder/my_table.parquet'

# run export for custom SQL
sling run --src-conn mssql --src-stream 'select col1, col2 from my_schema.my_table where col3 > 0' --tgt-object 'file://my_folder/my_table.parquet'

@dazfuller
Copy link
Author

Yeah, there are a few ways of doing this now which didn't exist in 2018 when I first did this. You could use JDBC direct on something like Databricks. A tool like Sling. DuckDB is another choice, as is something like Azure Data Factory, or even SqlPackage

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