Skip to main content

How to use StreamSets Data Protector function within Groovy Processor ?

  • December 22, 2021
  • 0 replies
  • 169 views

AkshayJadhav
StreamSets Employee
Forum|alt.badge.img

Data Protector function usage are limited within the Data Protector Stages. And are not available out side of these stages.

However, there are certain scenarios where you want to leverage these functions outside of these DP stages. The way we can achieve this via the Groovy processor. 

 

To access the Data Protector functions via the Groovy processor, Upload the following Data Protector jars as external libraries for the Groovy processor: ( You can fin this lib from streamsets-datacollector-dataprotector-lib package) 

* streamsets-datacollector-dataprotector-lib-1.9.0.jar

* streamsets-datacollector-dataprotector-api-1.9.0.jar

* re2j-1.2.jar

* javafaker-0.16.jar

* automaton-1.11-8.jar

 

Then update /etc/sdc/sdc-security.policy to set the Groovy permissions grant to the following:

grant codebase "file:/groovy/script" {
permission java.security.AllPermission;
};

 

And now we can use this function, Snippet of groovy code:

import com.streamsets.protector.el.XegerEL

fields = [
 'ACCTNBR': [type: 'int', length: 9],
 'STMTACCTNBR': [type: 'int', length: 5],
 'ACCTDESC': [type: 'str', length: 80]
]

sdc.records.each() { record ->
 try {
 record.value.each() { field,value ->
 if (fields.containsKey(field)) {
 switch(fields[field]['type']) {
 case 'int':
 format = sprintf('[0-9]{%s}', fields[field]['length'])
 record.value[field] = XegerEL.deterministic(format, record.value[field]).toInteger()
 break
 case 'str':
 format = sprintf('[a-z][A-Z]{%s}', fields[field]['length'])
 record.value[field] = XegerEL.deterministic(format, record.value[field]).toString()
 break
 }
 }
 }
 sdc.output.write(record)
 } catch (e) {
 sdc.log.error(e.toString(), e)
 sdc.error.write(record, e.toString())
 }
}

Example ( Attached Demo pipeline as well )

Did this topic help you find an answer to your question?
This topic has been closed for comments