I want to read the file in the middle of the pipeline instead from the origin. Can we do that using groovy script .
Solved
Read a file using groovy scripting stage
Best answer by saleempothiwala
I dont know what your exact use case is but if you want to read file in Groovy here are the steps:
- Make sure you add the entry for the data file location in Security file. Something along the lines:
// groovy source code
grant codebase "file:///groovy/script" {
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "/tmp/*", "read";
};
- Then place your data file in the location specified above /tmp/. In my case I created a test.txt that contained the following
Hi
How are you?
Good!
- I then added the following code in Groovy Evaluator
text = ""
File file = new File("/tmp/test.txt")
def line, noOfLines = 0;
file.withReader { reader ->
while ((line = reader.readLine()) != null) {
text= text + " " + line
// Read the test.txt file in 1 variable
}
}
records = sdc.records
for (record in records) {
try {
record.value.text = text
sdc.output.write(record)
} catch (e) {
// Write a record to the error pipeline
sdc.log.error(e.toString(), e)
sdc.error.write(record, e.toString())
}
}
This then generates preview output for my Groovy Evaluator like this

I hope this helps.
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.