CustomJavaWriter

Available in Community Designer

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

Short Description

CustomJavaWriter executes user-defined Java code.

Component Same input metadata Sorted inputs Inputs Outputs Each to all outputs Java CTL Auto-propagated metadata
CustomJavaWriter--0-n0-n-
yes
no
no

Icon

Ports

Number of ports depends on the java code.

Metadata

CustomJavaWriter does not propagate metadata.

CustomJavaWriter has no metadata templates.

Requirements on metadata depend on user-defined transformation.

CustomJavaWriter Attributes

AttributeReqDescriptionPossible values
Basic
Algorithm [1] Runnable transformation in Java defined in the graph. 
Algorithm URL[1]External file defining the runnable transformation in Java. 
Algorithm class[1]External runnable transformation class. 
Algorithm source charset 

Encoding of external file defining the transformation.

The default encoding depends on DEFAULT_SOURCE_CODE_CHARSET in defaultProperties.

E.g. UTF-8

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

Details

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

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

You can use Public Clover API in this component. Documentation on general parts of custom java components and Public Clover API is described in CustomJavaComponent.

Java Interfaces for CustomJavaWriter

Transformation required by the component must extend org.jetel.component.AbstractGenericTransform class.

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

Examples

Writing Maps and Lists

Create a component being able to write 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 users to explicitly specify Charset.

Compatibility

CustomJavaWriter is available since CloverETL 4.1.0-M1.

See also

CustomJavaComponent
CustomJavaReader
CustomJavaTransformer
Common Properties of Components
Specific Attribute Types
Others Comparison