fix(TDI-46727): Cannot pass a global variable of type date to a spark subjob using tRunJob after R2021-06 (#4602) (#4653)

This commit is contained in:
wang wei
2021-10-09 10:29:33 +08:00
committed by GitHub
parent 2b4d8e3081
commit 0412ef4d97

View File

@@ -12,6 +12,7 @@
// ============================================================================
package routines.system;
import java.text.ParseException;
import java.util.Date;
import routines.TalendDate;
@@ -31,6 +32,8 @@ public class RuntimeUtils {
public static String getRuntimeType(Object o) {
return o.getClass().getName();
}
private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* This function is in order to check the Date type in tRunJob when transmit the context to child job.
@@ -45,11 +48,40 @@ public class RuntimeUtils {
// when tRunJob transmit the date to child job, it should format with "yyyy-MM-dd HH:mm:ss"
if (isDateType(o)) {
return TalendDate.formatDate("yyyy-MM-dd HH:mm:ss", (Date) o); //$NON-NLS-1$
return TalendDate.formatDate(DEFAULT_DATE_PATTERN, (Date) o); //$NON-NLS-1$
}
return o;
}
public static Date getDate(String pattern, String dateString) {
if (dateString == null || dateString.isEmpty()) {
return null;
}
// when tRunJob transmit the date to child job:
//case 1: pass date string with pattern : yyyy-MM-dd HH:mm:ss
//case 2: pass date long value
//so here process two cases both and avoid exception
try {
return new java.text.SimpleDateFormat(pattern).parse(dateString);
} catch(ParseException e) {
//ignore exception
}
try {
return new Date(Long.valueOf(dateString));
} catch(NumberFormatException e) {
//ignore exception
//System.err.println(String.format("Null value will be used for context parameter as can't parse this to date: %s", dateString));
}
return null;
}
public static Date getDate(String dateString) {
return getDate(DEFAULT_DATE_PATTERN, dateString);
}
public static void main(String[] args) {
int i = 10;