com.hazelcast.jet.pipeline

## Class SinkBuilder<W,T>

- java.lang.Object
- - com.hazelcast.jet.pipeline.SinkBuilder<W,T>

- Type Parameters:
    `W` - type of the writer object
    `T` - type of the items the sink will accept

      public final class SinkBuilder<W,T>
      extends Object

  See `sinkBuilder(String, DistributedFunction)`.

- - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Sink<T>` | `build()` Creates and returns the `Sink` with the components you supplied to this builder.
    `SinkBuilder<W,T>` | `destroyFn(DistributedConsumer<? super W> destroyFn)` Sets the function that will destroy the writer and perform any cleanup.
    `SinkBuilder<W,T>` | `flushFn(DistributedConsumer<? super W> flushFn)` Sets the function that implements the sink's flushing behavior.
    `SinkBuilder<W,T>` | `preferredLocalParallelism(int preferredLocalParallelism)` Sets the local parallelism of the sink.
    `<T_NEW> SinkBuilder<W,T_NEW>` | `receiveFn(DistributedBiConsumer<? super W,? super T_NEW> receiveFn)` Sets the function Jet will call upon receiving an item.
    `static <W> SinkBuilder<W,Void>` | `sinkBuilder(String name, DistributedFunction<Processor.Context,? extends W> createFn)` Returns a builder object that offers a step-by-step fluent API to build a custom `Sink` for the Pipeline API.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### sinkBuilder

          @Nonnull
          public static <W> SinkBuilder<W,Void> sinkBuilder(@Nonnull
                                                                     String name,
                                                                     @Nonnull
                                                                     DistributedFunction<Processor.Context,? extends W> createFn)

      Returns a builder object that offers a step-by-step fluent API to build a custom `Sink` for the Pipeline API. It allows you to keep a single-threaded, stateful writer object in each instance of a Jet worker dedicated to driving the sink. Its primary intended purpose is to serve as the holder of references to external resources and optional buffers. Keep in mind that only the writer object may be stateful; the functions you provide must hold no mutable state of their own.

      These are the callback functions you can provide to implement the sink's behavior:

      1. `createFn` creates the writer. Gets the processor context as argument which can be used to obtain local Jet instance, global processor index etc. It will be called once for each worker thread. This component is required.
      2. `onReceiveFn` gets notified of each item the sink receives and (typically) passes it to the writer. This component is required.
      3. `flushFn` flushes the writer. This component is optional.
      4. `destroyFn` destroys the writer. This component is optional.
      The returned sink will be non-cooperative and will have preferred local parallelism of 1. It doesn't participate in the fault-tolerance protocol, which means you can't remember across a job restart which items you already received. The sink will still receive each item at least once, thus complying with the at-least-once processing guarantee. If the sink is idempotent (suppresses duplicate items), it will also be compatible with the exactly-once guarantee.
      Type Parameters:
        `W` - type of the writer object

    - #### receiveFn

          @Nonnull
          public <T_NEW> SinkBuilder<W,T_NEW> receiveFn(@Nonnull
                                                                 DistributedBiConsumer<? super W,? super T_NEW> receiveFn)

      Sets the function Jet will call upon receiving an item. The function receives two arguments: the writer object (as provided by the `createFn` and the received item. Its job is to push the item to the writer.
      Type Parameters:
        `T_NEW` - type of the items the sink will accept
      Parameters:
        `receiveFn` - the "add item to the writer" function

    - #### flushFn

          @Nonnull
          public SinkBuilder<W,T> flushFn(@Nonnull
                                                   DistributedConsumer<? super W> flushFn)

      Sets the function that implements the sink's flushing behavior. If your writer is buffered, instead of relying on some automatic flushing policy you can provide this function so Jet can choose the best moment to flush.

      You are not required to provide this function in case your implementation doesn't need it.

      Parameters:
        `flushFn` - the optional "flush the writer" function

    - #### destroyFn

          @Nonnull
          public SinkBuilder<W,T> destroyFn(@Nonnull
                                                     DistributedConsumer<? super W> destroyFn)

      Sets the function that will destroy the writer and perform any cleanup. The function is called when the job has been completed or cancelled. Jet guarantees that no new items will be received in between the last call to `flushFn` and the call to `destroyFn`.

      You are not required to provide this function in case your implementation doesn't need it.

      Parameters:
        `destroyFn` - the optional "destroy the writer" function

    - #### preferredLocalParallelism

          @Nonnull
          public SinkBuilder<W,T> preferredLocalParallelism(int preferredLocalParallelism)

      Sets the local parallelism of the sink. On each member of the cluster Jet will create this many parallel processors for the sink. To identify each processor instance, your `createFn` can consult `procContext.totalParallelism()` and `procContext.globalProcessorIndex()`. Jet calls `createFn` exactly once with each `globalProcessorIndex` from 0 to `totalParallelism - 1`.

      The default value of this property is 1.

    - #### build

          @Nonnull
          public Sink<T> build()

      Creates and returns the `Sink` with the components you supplied to this builder.

Copyright © 2018. All rights reserved.