From d664fdaf6b5cc0b0e375bcbfa0acedd78278a336 Mon Sep 17 00:00:00 2001 From: Krish Date: Tue, 25 Feb 2025 17:46:44 +0530 Subject: [PATCH 1/4] Added custom message property --- .../helloworld/common/PluginConstants.java | 2 ++ .../source/HelloWorldBatchSourceConfig.java | 16 ++++++++++++++++ .../source/HelloWorldInputFormatProvider.java | 1 + .../source/HelloWorldRecordReader.java | 2 ++ 4 files changed, 21 insertions(+) diff --git a/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java b/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java index e9435a4..32830cf 100644 --- a/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java +++ b/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java @@ -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"; 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"; 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))); } diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java index e5a7cad..0786fa6 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java @@ -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; + + public HelloWorldBatchSourceConfig(){ + if(message==null || message.isEmpty()) + { + message=PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE; + } + } + 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); @@ -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; } } diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java index fa807ed..10fc6ef 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java @@ -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()); } @Override diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java index 06e5f59..1e3ff7a 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java @@ -13,12 +13,14 @@ public class HelloWorldRecordReader extends RecordReader { 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 From c87babf53ee82e9994e1a0cad046c775fb6b3635 Mon Sep 17 00:00:00 2001 From: Krish Date: Tue, 25 Feb 2025 21:22:53 +0530 Subject: [PATCH 2/4] Fixed conflicts --- .../helloworld/common/PluginConstants.java | 13 +++++++++++-- .../source/HelloWorldInputFormatProvider.java | 4 ++-- .../helloworld/source/HelloWorldRecordReader.java | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java b/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java index 32830cf..d436507 100644 --- a/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java +++ b/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java @@ -3,12 +3,21 @@ import io.cdap.cdap.api.data.schema.Schema; public final class PluginConstants { + + // Plugin public static final String PLUGIN_NAME = "HelloWorld"; + + // Property: frequency public static final String PROPERTY_NAME_FREQUENCY = "frequency"; - public static final String PROPERTY_NAME_CUSTOM_MESSAGE="Custom Message"; - public static final String PROPERTY_CONFIG_FREQUENCY = "cdap.hello.world.config.frequency"; + public static final String PROPERTY_CONFIG_KEY_FREQUENCY = "cdap.hello.world.config.frequency"; public static final int PROPERTY_DEFAULT_FREQUENCY = 1; + + public static final String PROPERTY_NAME_USER_MESSAGE="User Message"; + public static String PROPERTY_CONFIG_KEY_MESSAGE="cdap.hello.world.config.message"; public static final String PROPERTY_CONFIG_DEFAULT_MESSAGE = "Hello world, this is a custom message"; + + + // Output 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))); } diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java index 10fc6ef..40e6a6e 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java @@ -12,8 +12,8 @@ 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()); + configMap.put(PluginConstants.PROPERTY_CONFIG_KEY_FREQUENCY, Integer.toString(config.getFrequency())); + configMap.put(PluginConstants.PROPERTY_CONFIG_KEY_MESSAGE,config.getMessage()); } @Override diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java index 1e3ff7a..bdaa76f 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java @@ -19,8 +19,8 @@ public class HelloWorldRecordReader extends RecordReader { 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); + frequency = conf.getInt(PluginConstants.PROPERTY_CONFIG_KEY_FREQUENCY, 1); + message=conf.get(PluginConstants.PROPERTY_CONFIG_KEY_MESSAGE,PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE); } @Override From 6a07d76856eb7b6f9a4dcca6b1b2e39285b0931d Mon Sep 17 00:00:00 2001 From: Krish Date: Tue, 25 Feb 2025 21:24:20 +0530 Subject: [PATCH 3/4] Added files --- .../helloworld/source/HelloWorldBatchSourceConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java index 0786fa6..3a0cd30 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java @@ -15,7 +15,7 @@ public class HelloWorldBatchSourceConfig extends PluginConfig { public Integer frequency; - @Name(PluginConstants.PROPERTY_NAME_CUSTOM_MESSAGE) + @Name(PluginConstants.PROPERTY_NAME_USER_MESSAGE) @Description("Custom message") @Nullable public String message=PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE; From 77a6e9d6de34a15277cf2b3b39d3651cd79cc164 Mon Sep 17 00:00:00 2001 From: Krish Date: Tue, 25 Feb 2025 21:27:28 +0530 Subject: [PATCH 4/4] Resolve conflicts --- .../helloworld/common/PluginConstants.java | 5 +++++ .../helloworld/source/HelloWorldBatchSourceConfig.java | 8 +------- .../helloworld/source/HelloWorldInputFormatProvider.java | 4 +++- .../helloworld/source/HelloWorldRecordReader.java | 5 ++++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java b/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java index d436507..d1958b7 100644 --- a/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java +++ b/src/main/java/com/pushpendersaini/helloworld/common/PluginConstants.java @@ -18,6 +18,11 @@ public final class PluginConstants { // Output + + public static final String PROPERTY_NAME_USER_MESSAGE="User Message"; + public static final String PROPERTY_CONFIG_KEY_MESSAGE = "cdap.hello.world.config.message"; + public static final String PROPERTY_CONFIG_DEFAULT_MESSAGE = "Hello world, this is a custom message"; + 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))); } diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java index 3a0cd30..298d97b 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldBatchSourceConfig.java @@ -18,14 +18,8 @@ public class HelloWorldBatchSourceConfig extends PluginConfig { @Name(PluginConstants.PROPERTY_NAME_USER_MESSAGE) @Description("Custom message") @Nullable - public String message=PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE; + public String message; - public HelloWorldBatchSourceConfig(){ - if(message==null || message.isEmpty()) - { - message=PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE; - } - } public void validate(FailureCollector failureCollector) { if (frequency != null && frequency < 1) { diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java index 40e6a6e..3156843 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldInputFormatProvider.java @@ -12,8 +12,10 @@ public class HelloWorldInputFormatProvider implements InputFormatProvider { public HelloWorldInputFormatProvider(HelloWorldBatchSourceConfig config) { configMap = new HashMap<>(); + configMap.put(PluginConstants.PROPERTY_CONFIG_KEY_FREQUENCY, Integer.toString(config.getFrequency())); - configMap.put(PluginConstants.PROPERTY_CONFIG_KEY_MESSAGE,config.getMessage()); + configMap.put(PluginConstants.PROPERTY_CONFIG_KEY_MESSAGE, config.getMessage()); + } @Override diff --git a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java index bdaa76f..e2fbfc5 100644 --- a/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java +++ b/src/main/java/com/pushpendersaini/helloworld/source/HelloWorldRecordReader.java @@ -19,8 +19,11 @@ public class HelloWorldRecordReader extends RecordReader { public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException { Configuration conf = taskAttemptContext.getConfiguration(); // Plugin configuration + frequency = conf.getInt(PluginConstants.PROPERTY_CONFIG_KEY_FREQUENCY, 1); - message=conf.get(PluginConstants.PROPERTY_CONFIG_KEY_MESSAGE,PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE); + + message = conf.get(PluginConstants.PROPERTY_CONFIG_KEY_MESSAGE,PluginConstants.PROPERTY_CONFIG_DEFAULT_MESSAGE); + } @Override