Class JsonMultiStreamLoader<TRecord>
Loads items of type TRecord into multiple streams,
writing one JSON object per stream.
public sealed class JsonMultiStreamLoader<TRecord> : LoaderBase<TRecord, JsonReport>, ILoadWithProgressAndCancellationAsync<TRecord, JsonReport>, ILoadWithProgressAsync<TRecord, JsonReport>, ILoadWithCancellationAsync<TRecord>, ILoadAsync<TRecord>, IReportsItemErrors, IAsyncDisposable, IDisposable, ISupportDryRun where TRecord : notnull
Type Parameters
TRecordThe type of items to load. Must be
notnull.
- Inheritance
-
LoaderBase<TRecord, JsonReport>JsonMultiStreamLoader<TRecord>
- Implements
-
ILoadWithProgressAndCancellationAsync<TRecord, JsonReport>ILoadWithProgressAsync<TRecord, JsonReport>ILoadWithCancellationAsync<TRecord>ILoadAsync<TRecord>IReportsItemErrorsISupportDryRun
- Inherited Members
-
LoaderBase<TRecord, JsonReport>.DisposeAsync()LoaderBase<TRecord, JsonReport>.Dispose()LoaderBase<TRecord, JsonReport>.ReportingIntervalLoaderBase<TRecord, JsonReport>.CurrentItemCountLoaderBase<TRecord, JsonReport>.CurrentSkippedItemCountLoaderBase<TRecord, JsonReport>.CurrentErrorItemCountLoaderBase<TRecord, JsonReport>.MaximumItemCountLoaderBase<TRecord, JsonReport>.SkipItemCountLoaderBase<TRecord, JsonReport>.WorkerResilienceLoaderBase<TRecord, JsonReport>.ErrorPolicy
Examples
var loader = new JsonMultiStreamLoader<Person>
(
person => new JsonNamedDestination(File.Create($"output/{person.Name}.json"), $"output/{person.Name}.json")
);
await loader.LoadAsync(items, cancellationToken);
Remarks
For each item in the input sequence, calls a factory function to obtain a stream, serializes the item as a single JSON object, and disposes the stream. Supply a JsonNamedDestination factory to surface the current destination name in progress reports via CurrentSourceName.
Constructors
JsonMultiStreamLoader(Func<TRecord, Stream>)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class.
[RequiresUnreferencedCode("JSON serialization of unknown types may require types that cannot be statically analyzed. Use the JsonTypeInfo overload for AOT compatibility.")]
[RequiresDynamicCode("JSON serialization of unknown types may require types that cannot be statically analyzed. Use the JsonTypeInfo overload for AOT compatibility.")]
public JsonMultiStreamLoader(Func<TRecord, Stream> streamFactory)
Parameters
streamFactoryFunc<TRecord, Stream>A factory function that receives the item to be written and returns a Stream to write it to. The loader will dispose the stream after writing.
Exceptions
- ArgumentNullException
Thrown when
streamFactoryisnull.
JsonMultiStreamLoader(Func<TRecord, Stream>, ILogger<JsonMultiStreamLoader<TRecord>>)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with diagnostic logging.
[RequiresUnreferencedCode("JSON serialization of unknown types may require types that cannot be statically analyzed. Use the JsonTypeInfo overload for AOT compatibility.")]
[RequiresDynamicCode("JSON serialization of unknown types may require types that cannot be statically analyzed. Use the JsonTypeInfo overload for AOT compatibility.")]
public JsonMultiStreamLoader(Func<TRecord, Stream> streamFactory, ILogger<JsonMultiStreamLoader<TRecord>> logger)
Parameters
streamFactoryFunc<TRecord, Stream>A factory function that receives the item to be written and returns a Stream to write it to. The loader will dispose the stream after writing.
loggerILogger<JsonMultiStreamLoader<TRecord>>The logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamFactoryorloggerisnull.
JsonMultiStreamLoader(Func<TRecord, Stream>, JsonSerializerOptions?, ILogger<JsonMultiStreamLoader<TRecord>>?)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with custom serialization options.
[RequiresUnreferencedCode("JSON serialization of unknown types may require types that cannot be statically analyzed. Use the JsonTypeInfo overload for AOT compatibility.")]
[RequiresDynamicCode("JSON serialization of unknown types may require types that cannot be statically analyzed. Use the JsonTypeInfo overload for AOT compatibility.")]
public JsonMultiStreamLoader(Func<TRecord, Stream> streamFactory, JsonSerializerOptions? options = null, ILogger<JsonMultiStreamLoader<TRecord>>? logger = null)
Parameters
streamFactoryFunc<TRecord, Stream>A factory function that receives the item to be written and returns a Stream to write it to. The loader will dispose the stream after writing.
optionsJsonSerializerOptionsThe JSON serializer options to use for serialization, or
nullfor the serializer default.loggerILogger<JsonMultiStreamLoader<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamFactoryisnull.
JsonMultiStreamLoader(Func<TRecord, Stream>, JsonTypeInfo<TRecord>, ILogger<JsonMultiStreamLoader<TRecord>>?)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with source-generated type metadata for AOT-friendly, reflection-free serialization.
public JsonMultiStreamLoader(Func<TRecord, Stream> streamFactory, JsonTypeInfo<TRecord> typeInfo, ILogger<JsonMultiStreamLoader<TRecord>>? logger = null)
Parameters
streamFactoryFunc<TRecord, Stream>A factory function that receives the item to be written and returns a Stream to write it to. The loader will dispose the stream after writing.
typeInfoJsonTypeInfo<TRecord>The source-generated type metadata for
TRecord.loggerILogger<JsonMultiStreamLoader<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamFactoryortypeInfoisnull.
JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination>)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with a named-destination factory for progress reporting.
public JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination> destinationFactory)
Parameters
destinationFactoryFunc<TRecord, JsonNamedDestination>A factory function that receives the item to be written and returns a JsonNamedDestination containing the stream and an optional name. The loader will dispose the stream after writing.
Exceptions
- ArgumentNullException
Thrown when
destinationFactoryisnull.
JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination>, ILogger<JsonMultiStreamLoader<TRecord>>)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with a named-destination factory and diagnostic logging.
public JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination> destinationFactory, ILogger<JsonMultiStreamLoader<TRecord>> logger)
Parameters
destinationFactoryFunc<TRecord, JsonNamedDestination>A factory function that receives the item to be written and returns a JsonNamedDestination containing the stream and an optional name. The loader will dispose the stream after writing.
loggerILogger<JsonMultiStreamLoader<TRecord>>The logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
destinationFactoryorloggerisnull.
JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination>, JsonSerializerOptions?, ILogger<JsonMultiStreamLoader<TRecord>>?)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with a named-destination factory and custom serialization options.
public JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination> destinationFactory, JsonSerializerOptions? options = null, ILogger<JsonMultiStreamLoader<TRecord>>? logger = null)
Parameters
destinationFactoryFunc<TRecord, JsonNamedDestination>A factory function that receives the item to be written and returns a JsonNamedDestination containing the stream and an optional name. The loader will dispose the stream after writing.
optionsJsonSerializerOptionsThe JSON serializer options to use for serialization, or
nullfor the serializer default.loggerILogger<JsonMultiStreamLoader<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
destinationFactoryisnull.
JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination>, JsonTypeInfo<TRecord>, ILogger<JsonMultiStreamLoader<TRecord>>?)
Initializes a new instance of the JsonMultiStreamLoader<TRecord> class with a named-destination factory and source-generated type metadata for AOT-friendly serialization.
public JsonMultiStreamLoader(Func<TRecord, JsonNamedDestination> destinationFactory, JsonTypeInfo<TRecord> typeInfo, ILogger<JsonMultiStreamLoader<TRecord>>? logger = null)
Parameters
destinationFactoryFunc<TRecord, JsonNamedDestination>A factory function that receives the item to be written and returns a JsonNamedDestination containing the stream and an optional name. The loader will dispose the stream after writing.
typeInfoJsonTypeInfo<TRecord>The source-generated type metadata for
TRecord.loggerILogger<JsonMultiStreamLoader<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
destinationFactoryortypeInfoisnull.
Properties
IsDryRun
Gets or sets a value indicating whether the stage runs in dry-run mode.
public bool IsDryRun { get; set; }
Property Value
- bool
When true, the stage runs the full pipeline but skips the external side effect that mutates the destination or source. Defaults to false.
Remarks
When true, the loader enumerates the source and increments progress counters as usual but skips calling the stream factory and writing any JSON to output streams.
Methods
CreateProgressReport()
Creates a progress report of type TProgress. This gives the derived class the opportunity to implement a custom progress report that is specific to the loading process.
protected override JsonReport CreateProgressReport()
Returns
- JsonReport
Progress of type TProgress
CreateProgressTimer(IProgress<JsonReport>)
Creates the Wolfgang.Etl.Abstractions.IProgressTimer used to drive progress callbacks. Override this method in a derived class to inject a custom timer (for example, a custom implementation that allows manual control in unit tests).
protected override IProgressTimer CreateProgressTimer(IProgress<JsonReport> progress)
Parameters
progressIProgress<JsonReport>The progress sink that will receive callbacks.
Returns
- IProgressTimer
A started Wolfgang.Etl.Abstractions.IProgressTimer instance.
LoadWorkerAsync(IAsyncEnumerable<TRecord>, CancellationToken)
This method is the core implementation of the loading logic and should be overridden by derived classes.
protected override Task LoadWorkerAsync(IAsyncEnumerable<TRecord> items, CancellationToken token)
Parameters
itemsIAsyncEnumerable<TRecord>The items to be loaded to the destination.
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task representing the asynchronous operation.
Remarks
Items may be an empty sequence if no data is available or if the loading fails.
Exceptions
- ArgumentNullException
Argument items is null