/* * Licensed Materials - Property of HCL Technologies Limited. (c) Copyright HCL Technologies Limited 1996, 2020. */ import java.time.Instant import org.apache.nifi.annotation.behavior.SideEffectFree import org.apache.nifi.annotation.documentation.CapabilityDescription import org.apache.nifi.components.PropertyDescriptor import org.apache.nifi.flowfile.FlowFile import org.apache.nifi.processor.AbstractProcessor import org.apache.nifi.processor.ProcessContext import org.apache.nifi.processor.ProcessSession import org.apache.nifi.processor.Relationship import org.apache.nifi.processor.exception.ProcessException import org.apache.nifi.processor.io.OutputStreamCallback import org.apache.nifi.processor.util.StandardValidators import java.text.SimpleDateFormat import groovy.json.JsonBuilder import groovy.json.JsonSlurper @SideEffectFree @CapabilityDescription("... short description of this custom processor ...") final class NTEProductCustomizationsProcessor extends AbstractProcessor { final Relationship RELATIONSHIP_SUCCESS = new Relationship.Builder().name("success") .description("The flow file with the specified content was successfully transferred").build(); final Relationship RELATIONSHIP_FAILURE = new Relationship.Builder().name("failure") .description("The flow file with the specified content has encountered an error during the transfer").build(); final Relationship RELATIONSHIP_UPDATE = new Relationship.Builder().name("update") .description("The flow file with the specified content for updating was successfully transferred").build(); final Relationship RELATIONSHIP_INVALID = new Relationship.Builder().name("invalid") .description("The flow file contains invalid content").build(); final PropertyDescriptor DELAY_COMPLETION = new PropertyDescriptor.Builder().name("Delay Completion") .description("Amount of time in milliseconds to wait for incoming flowfiles and delay the completion of this processor.") .required(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).addValidator(StandardValidators.INTEGER_VALIDATOR) .expressionLanguageSupported(true).build(); boolean isTransferred = false; @Override final Set<Relationship> getRelationships() { return [RELATIONSHIP_SUCCESS, RELATIONSHIP_FAILURE, RELATIONSHIP_UPDATE, RELATIONSHIP_INVALID] as Set; } @Override final void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { FlowFile lastFlowFile = null; FlowFile flowFile = null; // Amount of time to wait before completing final String delay = context.getProperty(DELAY_COMPLETION).evaluateAttributeExpressions().getValue(); while (true) { isTransferred = false; flowFile = session.get(); if (flowFile == null) { // Wait for 100 more milliseconds to ensure everything has been completed System.sleep(Long.valueOf(delay)); flowFile = session.get(); if (flowFile == null) { break; } } try { // Retrieve incoming flow file final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); session.exportTo(flowFile, bytes); bytes.close(); // Parse JSON content in flow file final String contents = bytes.toString(); final Map json = new JsonSlurper().parseText(contents); // Resulting document final Map document = new HashMap(); final String environment = flowFile.getAttribute("environment.name"); final String time = flowFile.getAttribute("time.id"); final String store = flowFile.getAttribute("param.storeId"); final String index = "." + environment.toLowerCase() + "." + store + ".product." + time; final String catalog = flowFile.getAttribute("master.catalog"); final String language = flowFile.getAttribute("param.langId"); // Update last modified timestamp final Map meta = new HashMap(); meta.put("modified", Instant.now().toString()); document.put("__meta", meta); // ------------------------------------------------ // Add custom logic in Apache Groovy language below // ------------------------------------------------ final String CATENTRY_ID = json.get("CATENTRY_ID"); // id.catentry final String catentry = CATENTRY_ID.trim(); final String SALESPRICERESTRICTED = json.get("SALESPRICERESTRICTED"); if(null != SALESPRICERESTRICTED){ document.put("salesPriceRestricted", SALESPRICERESTRICTED); } final String LISTPRICE = json.get("LISTPRICE"); final String CONTRACT_ID = json.get("CONTRACT_ID"); final Map price = new HashMap(); final Map offer = new HashMap(); if(null != LISTPRICE){ offer.put("usd",Double.valueOf(LISTPRICE)); price.put("offer",offer); final Map list = new HashMap(); list.put("usd",Double.valueOf(LISTPRICE)); price.put("list",list); } if(CONTRACT_ID != null) { final Map contract = new HashMap(); contract.put("usd",Double.valueOf(LISTPRICE)); price.put(CONTRACT_ID.trim(),contract); } if(!prices.isEmpty()){ document.put("prices", price); } final String SALESRANK = json.get("SALESRANK"); if (SALESRANK != null) { final String salesRank = SALESRANK.trim(); final Map p = new HashMap(); p.put("sales_rank", Double.valueOf(salesRank)); document.put("custom", p); } else { final Map p = new HashMap(); p.put("sales_rank", Double.valueOf(0.0)); document.put("custom", p); } String BACKORDERDATE = json.get("BACKORDERDATE"); if(BACKORDERDATE!=null){ String[] splits = BACKORDERDATE.split(" "); BACKORDERDATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.valueOf(splits[0]))); } document.put("backorderDate", BACKORDERDATE); String EXPFULFILLLOW = json.get("EXPFULFILLLOW"); if(EXPFULFILLLOW!=null){ final String expfulfilllow = EXPFULFILLLOW.trim(); document.put("expfulfilllow", Double.valueOf(expfulfilllow)); } String EXPFULFILLHIGH = json.get("EXPFULFILLHIGH"); if(EXPFULFILLHIGH!=null){ final String expfulfillhigh = EXPFULFILLHIGH.trim(); document.put("expfulfillhigh", Double.valueOf(expfulfillhigh)); } final String IS_EXT = json.get("IS_EXT"); final boolean isExt = Boolean.valueOf(IS_EXT); document.put("extendedOffer",isExt); final String IS_BASE_EXT = json.get("IS_BASE_EXT"); final boolean isBaseExt = Boolean.valueOf(IS_BASE_EXT); document.put("extendedBaseOffer",isBaseExt); // id = store + language + catalog + parent final StringBuilder id = new StringBuilder().append(store).append("-") .append(language).append("-").append(catalog).append("-").append(catentry); final StringBuilder output = new StringBuilder(); // { // "update": { // "_id": "1--1-10502-12874", // "retry_on_conflict": 5, // "_source": false // } // } output.append("{ \"update\": { \"_id\": \"").append(id) .append("\", \"_index\": \"").append(index) .append("\", \"retry_on_conflict\": 5, \"_source\": false } }\n"); // { // "doc": { // ..... your custom Json document goes in here // } // } final JsonBuilder builder = new JsonBuilder(document); output.append("{ \"doc\": ").append(builder.toString()).append(" }\n"); // Forward updated flow file to Elasticsearch _bulk endpoint for indexing flowFile = session.write(flowFile, new OutputStreamCallback() { @Override public void process(final OutputStream outputStream) throws IOException { outputStream.write(output.toString().getBytes()); outputStream.flush(); } }); // Persist to provenance lastFlowFile = flowFile; session.getProvenanceReporter().modifyContent(flowFile); session.transfer(flowFile, RELATIONSHIP_UPDATE); } catch (Throwable e) { if (! isTransferred) { final String message = "Unhandled error encountered: " + e.getMessage(); session.putAttribute(flowFile, "logger.message.cause", message); session.transfer(flowFile, RELATIONSHIP_FAILURE); } } } // Reach the end of queue and nothing else to be processed FlowFile successFlowFile = (lastFlowFile != null) ? session.create(lastFlowFile) : session.create(); final StringBuilder message = new StringBuilder(); message.append("{ \"message\": \"NTEProductCustomizationsProcessor completed successfully\" }"); successFlowFile = session.write(successFlowFile, new OutputStreamCallback() { @Override public void process(final OutputStream outputStream) throws IOException { outputStream.write(message.toString().getBytes()); outputStream.flush(); } }); session.transfer(successFlowFile, RELATIONSHIP_SUCCESS); } } processor = new NTEProductCustomizationsProcessor();
Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.
OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.
def name = System.in.newReader().readLine()
println "Hello " + name
Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.
Data type | Description | Range |
---|---|---|
String | To represent text literals | NA |
char | To represent single character literal | NA |
int | To represent whole numbers | -2,147,483,648 to 2,147,483,647 |
short | To represent short numbers | -32,768 to 32,767 |
long | To represent long numbers | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
double | To represent 64 bit floating point numbers | 4.94065645841246544e-324d to 1.79769313486231570e+308d |
float | To represent 32 bit floating point numbers | 1.40129846432481707e-45 to 3.40282346638528860e+38 |
byte | To represent byte value | -128 to 127 |
boolean | To represent boolean values either true or false | True or False |
You can define variables in two ways
data-type variable-name;
[or]
def variable-name;
0.upto(n) {println "$it"}
or
n.times{println "$it"}
where n is the number of loops and 0 specifies the starting index
When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
List allows you to store ordered collection of data values.
def mylist = [1,2,3,4,5];
List Methods | Description |
---|---|
size() | To find size of elements |
sort() | To sort the elements |
add() | To append new value at the end |
contains() | Returns true if this List contains requested value. |
get() | Returns the element of the list at the definite position |
pop() | To remove the last item from the List |
isEmpty() | Returns true if List contains no elements |
minus() | This allows you to exclude few specified elements from the elements of the original |
plus() | This allows you to add few specified elements to the elements of the original |
remove() | To remove the element present at the specific position |
reverse() | To reverse the elements of the original List and creates new list |