A lightweight .NET library for producing, receiving, and transporting Apache NiFi FlowFiles. This utility provides support for FlowFile V1 (Tar archive), FlowFile V3 (Binary stream), and Site-to-Site (S2S) protocol communication.
- FlowFile V3 Support: Create and unpack binary-encoded FlowFiles (V3) with full attribute and content preservation.
- System.IO.Pipelines: High-performance, low-allocation I/O support for V3 packing and unpacking.
- FlowFile V1 Support: Create FlowFiles using the Tar-based V1 format.
- Transport Mechanisms:
- ListenHTTP: Post FlowFiles directly to NiFi's
ListenHTTPprocessor. - Site-to-Site (S2S): Push data to NiFi Input Ports using the native S2S protocol.
- ListenHTTP: Post FlowFiles directly to NiFi's
- Resilience & Retries: Built-in support for .NET 8 Resilience Pipelines (Polly) to handle transient failures in S2S transactions.
- Structured Logging: Integrated with
Microsoft.Extensions.Loggingfor deep diagnostics. - Asynchronous Design: Fully async/await compliant for high-performance streaming.
- Multi-targeting: Supports .NET 8.0, 9.0, and 10.0.
The library is divided into two primary services to separate concerns between data formatting and network transport:
IFlowFileService/FlowFileService: Responsible for the low-level serialization and deserialization of NiFi FlowFile formats (V1 and V3). Supports bothStreamandSystem.IO.Pipelines.INifiTransportService/NifiTransportService: Responsible for high-level communication protocols, utilizing theIFlowFileServicefor payload preparation.
NifiKit: The core library.Services/: Contains the interface and implementation for FlowFile and Transport services.Models/: Contains theNifiPackagedata model.
Nifi.App: A console application (placeholder for usage and testing).
- .NET 8.0, 9.0, or 10.0 SDK
Represents a single NiFi FlowFile. It provides fluent helper methods for setting attributes and content.
using NifiKit.Models;
var package = new NifiPackage()
.AddAttribute("filename", "test.txt")
.AddAttribute("mime.type", "text/plain")
.SetContent(Encoding.UTF8.GetBytes("Hello NiFi"));using NifiKit.Services;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Retry;
var services = new ServiceCollection();
// 1. Add standard Logging
services.AddLogging(builder => builder.AddConsole());
// 2. Add HttpClient with Resilience
services.AddHttpClient<INifiTransportService, NifiTransportService>()
.AddStandardResilienceHandler(); // Handles transient HTTP errors
// 3. Configure a named Resilience Pipeline for the entire S2S transaction
services.AddResiliencePipeline("NifiS2S", builder =>
{
builder.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = 3,
Delay = TimeSpan.FromSeconds(2),
BackoffType = DelayBackoffType.Exponential
});
});
services.AddSingleton<IFlowFileService, FlowFileService>();
services.AddSingleton<INifiTransportService, NifiTransportService>();
var provider = services.BuildServiceProvider();
var transportService = provider.GetRequiredService<INifiTransportService>();using System.IO.Pipelines;
// Unpacking from a PipeReader
await foreach (var package in flowFileService.UnpackFlowFilesV3Async(pipeReader))
{
// Process package...
}
// Packing to a PipeWriter
await flowFileService.WriteFlowFileV3Async(package, pipeWriter);The S2S protocol allows for reliable, high-volume data transfer to NiFi Input Ports.
var packages = new List<NifiPackage> { ... };
bool success = await transportService.SendViaS2SAsync(
"http://nifi-server:8080",
"MyInputPort",
packages
);await foreach (var package in flowFileService.UnpackFlowFilesV3Async(inputStream))
{
Console.WriteLine($"Received file: {package.attributes["filename"]}");
// Process package.content...
}The implementation of FlowFile formats and protocols in this library is based on the official Apache NiFi source code.
The library implements the NiFi FlowFile V3 specification as defined in the NiFi source:
- Magic Header:
NiFiFF3 - Attributes Count: 2-byte or 6-byte encoded length.
- Attributes: Key-value pairs with length-prefixed strings.
- Content Length: 8-byte long.
- Content: Raw binary data.
FlowFile V1 uses the standard Tar archive format containing two files:
flowfile.attributes: A key-value properties file.flowfile.content: The actual payload.
This project includes a GitHub Action to automatically publish the library to NuGet when a new version tag is pushed.
- Create a NuGet API Key.
- Add the key as a secret in your GitHub repository:
- Name:
NUGET_API_KEY - Value: (Your API Key)
- Name:
To trigger a new release, create and push a tag:
git tag v1.0.0
git push origin v1.0.0