Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
public final class PluginConstants {
public static final String PLUGIN_NAME = "HelloWorld";
public static final String PROPERTY_NAME_FREQUENCY = "frequency";
public static final String PROPERTY_NAME_CUSTOM_MESSAGE="Custom Message";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name can be better, this is too generic !

public static final String PROPERTY_CONFIG_FREQUENCY = "cdap.hello.world.config.frequency";
public static final int PROPERTY_DEFAULT_FREQUENCY = 1;
public static final String PROPERTY_CONFIG_DEFAULT_MESSAGE = "Hello world, this is a custom message";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the default message same as before for backwards compatibility!

public static final String PLUGIN_OUT_VALUE = "Hello World!";
public static final Schema PLUGIN_OUT_SCHEMA = Schema.recordOf("data", Schema.Field.of("message", Schema.of(Schema.Type.STRING)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@
import io.cdap.cdap.api.plugin.PluginConfig;
import io.cdap.cdap.etl.api.FailureCollector;

import javax.annotation.Nullable;

public class HelloWorldBatchSourceConfig extends PluginConfig {

@Name(PluginConstants.PROPERTY_NAME_FREQUENCY)
@Description("Number of times the plugin says hello world.")
public Integer frequency;


@Name(PluginConstants.PROPERTY_NAME_CUSTOM_MESSAGE)
@Description("Custom message")
@Nullable
public String message=PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never set values directly using = in config files, config values are set by platform based on user input on web UI.


public HelloWorldBatchSourceConfig(){
if(message==null || message.isEmpty())
{
message=PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE;
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment here , this can be removed !

public void validate(FailureCollector failureCollector) {
if (frequency != null && frequency < 1) {
failureCollector.addFailure("Property cannot be lower than 1.", "Use a frequency value of equal to or more than 1.").withConfigProperty(PluginConstants.PROPERTY_NAME_FREQUENCY);
Expand All @@ -21,5 +36,6 @@ public void validate(FailureCollector failureCollector) {
public int getFrequency() {
return frequency == null ? PluginConstants.PROPERTY_DEFAULT_FREQUENCY : frequency;
}
public String getMessage(){ return (message == null || message.isEmpty()) ? PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE: message; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix formatting !


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class HelloWorldInputFormatProvider implements InputFormatProvider {
public HelloWorldInputFormatProvider(HelloWorldBatchSourceConfig config) {
configMap = new HashMap<>();
configMap.put(PluginConstants.PROPERTY_CONFIG_FREQUENCY, Integer.toString(config.getFrequency()));
configMap.put(PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE, config.getMessage());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a key, although not required it's best to follow the standard, eg : cdap.hello.world.config.frequency

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one can be cdap.hello.world.config.message

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class HelloWorldRecordReader extends RecordReader<NullWritable, String> {

private int frequency;
private int countProcessed = 0;
private String message;

@Override
public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
Configuration conf = taskAttemptContext.getConfiguration();
// Plugin configuration
frequency = conf.getInt(PluginConstants.PROPERTY_CONFIG_FREQUENCY, 1);
message = conf.get(PluginConstants.PROPERTY_NAME_CUSTOM_MESSAGE,PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE);
}

@Override
Expand Down