Version

    CustomJavaWriter

    Short Description
    Ports
    Metadata
    CustomJavaWriter Attributes
    Details
    Examples
    Best Practices
    Compatibility
    See also

    Short Description

    CustomJavaWriter executes a user-defined Java code.

    ComponentSame input metadataSorted inputsInputsOutputsEach to all outputsJavaCTLAuto-propagated metadata
    CustomJavaWriter--0-n0-n-
    yes
    no
    no

    Ports

    The number of ports depends on the Java code.

    Metadata

    By default, CustomJavaWriter does not propagate metadata. Metadata propagation can be configured using the Metadata propagation algorithm and Metadata propagation algorithm class attributes.

    CustomJavaWriter has no metadata templates.

    Requirements on metadata depend on a user-defined transformation.

    CustomJavaWriter Attributes

    AttributeReqDescriptionPossible values
    Basic
    Algorithm[1]A runnable transformation in Java defined in the graph. 
    Algorithm URL[1]A URL to an external file defining a runnable transformation in Java. 
    Algorithm class[1]An external runnable transformation class. 
    Advanced
    Metadata propagation algorithm An algorithm for metadata propagation written in Java. 
    Metadata propagation algorithm class An external metadata propagation class. 
    Algorithm source charset 

    The encoding of the external file defining the transformation.

    The default encoding depends on DEFAULT_SOURCE_CODE_CHARSET in defaultProperties.

    UTF-8

    [1]  One of these must be set. These transformation attributes must be specified.

    Details

    CustomJavaWriter executes the Java transformation. CustomJavaWriter is a more specific CustomJavaComponent focused on writing data.

    There are other similar Java components: CustomJavaReader, CustomJavaTransformer and CustomJavaComponent. All these components use a transformation defined in Java, they differ in templates being used.

    You can use Public CloverDX API in this component. General parts of custom Java components and Public CloverDX API are described in CustomJavaComponent.

    Java Interfaces for CustomJavaWriter

    A transformation required by the component must extend the org.jetel.component.AbstractGenericTransform class.

    The component has the same Java interface as CustomJavaComponent, but it provides a different Java template. See Java Interfaces for CustomJavaComponent.

    Custom Metadata Propagation for CustomJavaWriter

    CustomJavaWriter can propagate metadata using a custom algorithm. Metadata propagation algorithm must extend the org.jetel.component.GenericMetadataProvider class.

    The component has the same Java interface as CustomJavaComponent, see Custom Metadata Propagation for CustomJavaComponent.

    Examples

    Writing Maps and Lists

    Create a component capable of writing all input metadata fields as strings. Input metadata fields can include maps and lists.

    Solution
    package jk;
    
    import java.io.IOException;
    import java.io.OutputStream;
    
    import org.jetel.component.AbstractGenericTransform;
    import org.jetel.data.DataField;
    import org.jetel.data.DataRecord;
    import org.jetel.exception.JetelRuntimeException;
    
    /**
     * This is an example custom writer. It shows how you can write string
     * representations of fields into file.
     */
    public class CustomJavaWriterExample01 extends AbstractGenericTransform {
        @Override
        public void execute() {
            String fileUrl = getProperties().getStringProperty("FileUrl");
            DataRecord record;
    
            try (OutputStream os = getOutputStream(fileUrl, false)) {
                while ((record = readRecordFromPort(0)) != null) {
                    StringBuffer sb = new StringBuffer();
                    DataField[] dataFields = record.getFields();
    
                    int fieldCount = 1;
                    for (DataField df : dataFields) {
                        sb.append(df.getValue() != null ? df.getValue().toString() : "");
                        if (df.getMetadata().getDelimiter() != null) {
                            sb.append(df.getMetadata().getDelimiter());
                        } else {
                            if (fieldCount != dataFields.length) {
                                sb.append(record.getMetadata().getFieldDelimiter());
                            }
                        }
                        ++fieldCount;
                    }
                    sb.append(record.getMetadata().getRecordDelimiter());
                    os.write(sb.toString().getBytes("UTF-8"));
                }
            } catch (IOException e) {
                throw new JetelRuntimeException(e);
            }
        }
    }

    Best Practices

    If Algorithm URL is used, we recommend to explicitly specify Charset.

    Compatibility

    VersionCompatibility Notice
    4.1.0-M1CustomJavaWriter is available since 4.1.0-M1.
    5.3.0

    CustomJavaWriter can now propagate metadata - added new attributes Metadata propagation algorithm and Metadata propagation algorithm class.