CustomJavaReader

Available in Community Designer

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

Short Description

CustomJavaReader executes a user-defined Java code.

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

Ports

Number of ports depends on the Java code.

Metadata

CustomJavaReader does not propagate metadata.

CustomJavaReader has no metadata templates.

Requirements on metadata depend on a user-defined transformation.

CustomJavaReader Attributes

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

Encoding of the 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

CustomJavaReader executes the Java transformation. CustomJavaReader is a more specific CustomJavaComponent and it is focused on reading data.

There are other similar Java components: CustomJavaWriter, CustomJavaTransformer and CustomJavaComponent. All these components use a 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 CustomJavaReader

A transformation required by the component must extend 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.

Examples

Generating Random Bytes
Reading Records with Header and Variable-length Field

Generating Random Bytes

Generate user-defined number of sequences of random bytes, each sequence is of 32 bytes.

Solution

Use CustomJavaReader to create a new user-defined component.

Add the new custom attribute RecordCount to the component and set up a value to the attribute, e.g. to 5.

Define the transformation.

package jk;

import java.security.SecureRandom;
import org.jetel.component.AbstractGenericTransform;
import org.jetel.data.DataRecord;

/**
 * This is an example custom reader. It shows how you can create records using a
 * data source.
 */
public class CustomJavaReaderExample01 extends AbstractGenericTransform {

	@Override
	public void execute() {
		Integer recordCount = getProperties().getIntProperty("RecordCount");

		// Output record prepared for writing to output port 0.
		DataRecord record = outRecords[0];

		SecureRandom secureRandom = new SecureRandom();

		for (int i = 0; i < recordCount; ++i) {
			byte[] b = new byte[32];

			secureRandom.nextBytes(b);
			record.getField("field1").setValue(b);
			writeRecordToPort(0, record);
			record.reset();
		}
	}
}

Metadata on the output port must have one field named field1. The field has byte data type.

Reading Records with Header and Variable-length Field

Read binary data from a file. Each record has one byte header and a variable length payload. The length of the payload is defined by the integer value of the header. Read the length and payload from each record. The file to be read has to be configured by the component attribute.

Example of source data:

!123456789012345678901234567890123"abcdefghijklmnopqrstuvwxyz78901234
Solution

Add the FileUrl attribute to the component.

package jk;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.jetel.component.AbstractGenericTransform;
import org.jetel.data.DataRecord;
import org.jetel.exception.JetelRuntimeException;

/**
 * This is an example custom reader. It shows how you can read data from file
 * with specific format.
 */
public class CustomJavaReaderExample02 extends AbstractGenericTransform {

	@Override
	public void execute() {
		DataRecord record = outRecords[0];
		String fileUrl = getProperties().getStringProperty("FileUrl");
		File file = getFile(fileUrl);

		try (FileInputStream inputStream = new FileInputStream(file)) {
			for (int length = inputStream.read(); length > 0; length = inputStream.read()) {
				byte[] b = new byte[length];
				inputStream.read(b, 0, length);

				record.getField("field1").setValue(length);
				record.getField("field2").setValue(>b);
				writeRecordToPort(0, record);
				record.reset();
			}
		} catch (IOException e) {
			throw new JetelRuntimeException(e);
		}
	}
}

Note: input data is in binary format. The first header contains a character ! which corresponds to the value 33. Thirty-three characters follow. The header of the second record has the value 34 which is displayed as a character ". Thirty-four bytes follow.

Metadata on the output port must have two fields named field1 and field2 with the types integer and byte.

Best Practices

If the Algorithm URL attribute is used, we recommend to explicitly specify Algorithm source charset.

Compatibility

CustomJavaReader is available since CloverETL 4.1.0-M1.

See also

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