๐Ÿ’ปTech/๐Ÿ’งnifi

[NiFi] Groovy-Java๋กœ ์—ฌ๋Ÿฌ ๋‚ ์งœ ๋ฝ‘๋Š” ๋ฐฉ๋ฒ•

_viper_ 2022. 9. 22. 11:14
๋ฐ˜์‘ํ˜•

NiFi์—์„œ ์ œ๊ณตํ•˜๋Š” Groovy Script๋Š” ์ž๋ฐ” ํ˜ธํ™˜์ด ๊ฑฐ์˜ ๋˜๋ฏ€๋กœ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜์—ฌ๋„ ์ž˜ ๋Œ์•„๊ฐ

  1. Create ExecuteScript Processor
  2. PROPERTIES > Select Groovy > Write Script Body
  3. Script Contents
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//attribute์˜ ๋‚ ์งœ(yyyymmdd) ๊ธฐ์ค€๋ถ€ํ„ฐ -30์ผ๊นŒ์ง€ ์ถ”์ถœ 
flowFile = session.get();
if(!flowFile) return;
dt = flowFile.getAttribute('dt');
session.transfer(flowFile, REL_SUCCESS);

for(int i=0; i<31; i++){
	DateFormat dateFormat = null;
	Date date = new Date();

	try {
		dateFormat = new SimpleDateFormat("yyyyMMdd");
		date = dateFormat.parse(part_dt);
	}catch(Exception e){
		System.out.println(e.getMessage());
	}
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(Calendar.DATE, -i);
	
	dt = dateFormat.format(cal.getTime());

	flowFile = session.create();
	newFlowFile = session.putAttribute(flowFile, 'dt', dt);
	session.transfer(newFlowFile, REL_SUCCESS);
}