Solved

Read a file using groovy scripting stage

  • 23 September 2022
  • 7 replies
  • 87 views

I want to read the file in the middle of the pipeline instead from  the origin. Can we do that using groovy script . 

icon

Best answer by saleempothiwala 26 September 2022, 22:29

View original

7 replies

Userlevel 3
Badge +1

@Subha 

can you please elaborate your case in detail?

We can use scripting to read files but need to know your need ,so i can suggest on that.

 

 

 

I want to read a text file in middle of the pipeline . So i want to use the groovy evaluator stage and write a groovy script . Can you suggest on the script.

Userlevel 3
Badge

@Subha,

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.

Userlevel 3
Badge

To add entry to the security file

 

Deployment → Edit → Next → Advance Configurations → Security Policy

Userlevel 3
Badge +1

@Subha 

 

To add on Saleem’s input , you can read files as given below to handle ASCII/special characters correctly.

Please check if UTF-8 works as it is else you can use “"ISO-8859-1" to handle the ASCII characters in the file.

 

new File("/tmp/test.txt").withReader('UTF-8') {

{ reader ->

def line

while ((line = reader.readLine()) != null)

{

text= text + " " + line

}

}

Userlevel 3
Badge

Thanks for the input, @Bikram 

Thanks @saleempothiwala  i was looking exactly the same requirement . Thanks for your answer . this solved my issue

Reply