Java Code Examples for com.streamsets.pipeline.api.Field#createDatetime()
The following examples show how to use
com.streamsets.pipeline.api.Field#createDatetime() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptTypedNullObject.java From datacollector with Apache License 2.0 | 5 votes |
/** * Receive a scriptOject and find out if the scriptObect is one of the NULL_** * object defined in this class. If so, create a new field with the type * and null value, then return the field. * If the scriptObject is not one of the typed null object, it returns a * new field with string converted from the value. * @param scriptObject: ScriptObject, this might be one of the Typed Null object. * @return */ public static Field getTypedNullFieldFromScript(Object scriptObject) { Field field; if(scriptObject == NULL_BOOLEAN) field = Field.create(Field.Type.BOOLEAN, null); else if(scriptObject == NULL_CHAR) field = Field.create(Field.Type.CHAR, null); else if(scriptObject == NULL_BYTE) field = Field.create(Field.Type.BYTE, null); else if(scriptObject == NULL_SHORT) field = Field.create(Field.Type.SHORT, null); else if (scriptObject == NULL_INTEGER) field = Field.create(Field.Type.INTEGER, null); else if(scriptObject == NULL_LONG) field = Field.create(Field.Type.LONG, null); else if (scriptObject == NULL_FLOAT) field = Field.create(Field.Type.FLOAT, null); else if(scriptObject == NULL_DOUBLE) field = Field.create(Field.Type.DOUBLE, null); else if(scriptObject == NULL_DATE) field = Field.createDate(null); else if(scriptObject == NULL_DATETIME) field = Field.createDatetime(null); else if(scriptObject == NULL_TIME) field = Field.createTime(null); else if(scriptObject == NULL_DECIMAL) field = Field.create(Field.Type.DECIMAL, null); else if(scriptObject == NULL_BYTE_ARRAY) field = Field.create(Field.Type.BYTE_ARRAY, null); else if(scriptObject == NULL_STRING) field = Field.create(Field.Type.STRING, null); else if(scriptObject == NULL_LIST) field = Field.create(Field.Type.LIST, null); else if(scriptObject == NULL_MAP) field = Field.create(Field.Type.MAP, null); else //this scriptObject is not Null typed field. Return null. field = null; return field; }
Example 2
Source File: JythonSource.java From datacollector with Apache License 2.0 | 5 votes |
@Override protected Field convertPrimitiveObject(Object scriptObject) { Field field; if (scriptObject instanceof Boolean) { field = Field.create((Boolean) scriptObject); } else if (scriptObject instanceof Character) { field = Field.create((Character) scriptObject); } else if (scriptObject instanceof Byte) { field = Field.create((Byte) scriptObject); } else if (scriptObject instanceof Short) { field = Field.create((Short) scriptObject); } else if (scriptObject instanceof Integer) { field = Field.create((Integer) scriptObject); } else if (scriptObject instanceof Long) { field = Field.create((Long) scriptObject); } else if (scriptObject instanceof BigInteger) { // special handling for Jython LONG type field = Field.create(((BigInteger) scriptObject).longValue()); } else if (scriptObject instanceof Float) { field = Field.create((Float) scriptObject); } else if (scriptObject instanceof Double) { field = Field.create((Double) scriptObject); } else if (scriptObject instanceof Date) { field = Field.createDatetime((Date) scriptObject); } else if (scriptObject instanceof BigDecimal) { field = Field.create((BigDecimal) scriptObject); } else if (scriptObject instanceof String) { field = Field.create((String) scriptObject); } else if (scriptObject instanceof byte[]) { field = Field.create((byte[]) scriptObject); } else if (scriptObject instanceof ScriptObjectFactory.ScriptFileRef) { field = Field.create(getFileRefFromScriptFileRef((ScriptObjectFactory.ScriptFileRef) scriptObject)); } else { field = ScriptTypedNullObject.getTypedNullFieldFromScript(scriptObject); if (field == null) { // unable to find field type from scriptObject. Return null String. field = Field.create(scriptObject.toString()); } } return field; }
Example 3
Source File: JythonProcessor.java From datacollector with Apache License 2.0 | 5 votes |
@Override protected Field convertPrimitiveObject(Object scriptObject) { Field field; if (scriptObject instanceof Boolean) { field = Field.create((Boolean) scriptObject); } else if (scriptObject instanceof Character) { field = Field.create((Character) scriptObject); } else if (scriptObject instanceof Byte) { field = Field.create((Byte) scriptObject); } else if (scriptObject instanceof Short) { field = Field.create((Short) scriptObject); } else if (scriptObject instanceof Integer) { field = Field.create((Integer) scriptObject); } else if (scriptObject instanceof Long) { field = Field.create((Long) scriptObject); } else if (scriptObject instanceof BigInteger) { // special handling for Jython LONG type field = Field.create(((BigInteger) scriptObject).longValue()); } else if (scriptObject instanceof Float) { field = Field.create((Float) scriptObject); } else if (scriptObject instanceof Double) { field = Field.create((Double) scriptObject); } else if (scriptObject instanceof Date) { field = Field.createDatetime((Date) scriptObject); } else if (scriptObject instanceof BigDecimal) { field = Field.create((BigDecimal) scriptObject); } else if (scriptObject instanceof String) { field = Field.create((String) scriptObject); } else if (scriptObject instanceof byte[]) { field = Field.create((byte[]) scriptObject); } else if (scriptObject instanceof ScriptFileRef) { field = Field.create(getFileRefFromScriptFileRef((ScriptFileRef)scriptObject)); } else { field = ScriptTypedNullObject.getTypedNullFieldFromScript(scriptObject); if (field == null) { // unable to find field type from scriptObject. Return null String. field = Field.create(scriptObject.toString()); } } return field; }
Example 4
Source File: MapRDBCDCSource.java From datacollector with Apache License 2.0 | 5 votes |
private Field generateField(Value value) { switch (value.getType()) { case INT: return Field.create(value.getInt()); case LONG: return Field.create(value.getLong()); case SHORT: return Field.create(value.getShort()); case BOOLEAN: return Field.create(value.getBoolean()); case DECIMAL: return Field.create(value.getDecimal()); case BYTE: return Field.create(value.getByte()); case DATE: return Field.createDate(value.getDate().toDate()); case FLOAT: return Field.create(value.getFloat()); case DOUBLE: return Field.create(value.getDouble()); case STRING: return Field.create(value.getString()); case MAP: return Field.create(value.getMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, this::generateField))); case TIME: return Field.createTime(value.getTime().toDate()); case ARRAY: return Field.create(value.getList().stream().map(this::generateField).collect(Collectors.toList())); case BINARY: return Field.create(value.getBinary().array()); case TIMESTAMP: return Field.createDatetime(value.getTimestamp().toDate()); case INTERVAL: return Field.create(value.getInterval().getTimeInMillis()); default: throw new IllegalArgumentException("Unsupported type " + value.getType().toString()); } }
Example 5
Source File: ScriptObjectFactory.java From datacollector with Apache License 2.0 | 4 votes |
protected Field convertPrimitiveObject(Object scriptObject) { Field field; if (scriptObject instanceof Boolean) { field = Field.create((Boolean) scriptObject); } else if (scriptObject instanceof Character) { field = Field.create((Character) scriptObject); } else if (scriptObject instanceof Byte) { field = Field.create((Byte) scriptObject); } else if (scriptObject instanceof Short) { field = Field.create((Short) scriptObject); } else if (scriptObject instanceof Integer) { field = Field.create((Integer) scriptObject); } else if (scriptObject instanceof Long) { field = Field.create((Long) scriptObject); } else if (scriptObject instanceof Float) { field = Field.create((Float) scriptObject); } else if (scriptObject instanceof Double) { field = Field.create((Double) scriptObject); } else if (scriptObject instanceof Date) { field = Field.createDatetime((Date) scriptObject); } else if (scriptObject instanceof BigDecimal) { field = Field.create((BigDecimal) scriptObject); } else if (scriptObject instanceof String) { field = Field.create((String) scriptObject); } else if (scriptObject instanceof byte[]) { field = Field.create((byte[]) scriptObject); } else if (scriptObject instanceof ScriptFileRef) { field = Field.create(getFileRefFromScriptFileRef((ScriptFileRef)scriptObject)); } else { field = ScriptTypedNullObject.getTypedNullFieldFromScript(scriptObject); if (field == null) { // unable to find field type from scriptObject. Return null String. if (scriptObject == null) { field = Field.create(Field.Type.STRING, null); } else { field = Field.create(scriptObject.toString()); } } } return field; }
Example 6
Source File: FileRefUtil.java From datacollector with Apache License 2.0 | 4 votes |
public static Field createFieldForMetadata(Object metadataObject) { if (metadataObject == null) { return Field.create(""); } if (metadataObject instanceof Boolean) { return Field.create((Boolean) metadataObject); } else if (metadataObject instanceof Character) { return Field.create((Character) metadataObject); } else if (metadataObject instanceof Byte) { return Field.create((Byte) metadataObject); } else if (metadataObject instanceof Short) { return Field.create((Short) metadataObject); } else if (metadataObject instanceof Integer) { return Field.create((Integer) metadataObject); } else if (metadataObject instanceof Long) { return Field.create((Long) metadataObject); } else if (metadataObject instanceof Float) { return Field.create((Float) metadataObject); } else if (metadataObject instanceof Double) { return Field.create((Double) metadataObject); } else if (metadataObject instanceof Date) { return Field.createDatetime((Date) metadataObject); } else if (metadataObject instanceof BigDecimal) { return Field.create((BigDecimal) metadataObject); } else if (metadataObject instanceof String) { return Field.create((String) metadataObject); } else if (metadataObject instanceof byte[]) { return Field.create((byte[]) metadataObject); } else if (metadataObject instanceof Collection) { Iterator iterator = ((Collection)metadataObject).iterator(); List<Field> fields = new ArrayList<>(); while (iterator.hasNext()) { fields.add(createFieldForMetadata(iterator.next())); } return Field.create(fields); } else if (metadataObject instanceof Map) { boolean isListMap = (metadataObject instanceof LinkedHashMap); Map<String, Field> fieldMap = isListMap? new LinkedHashMap<>() : new HashMap<>(); Map<Object, Object> map = (Map)metadataObject; for (Map.Entry<Object, Object> entry : map.entrySet()) { fieldMap.put(entry.getKey().toString(), createFieldForMetadata(entry.getValue())); } return isListMap? Field.create(Field.Type.LIST_MAP, fieldMap) : Field.create(fieldMap); } else { return Field.create(metadataObject.toString()); } }
Example 7
Source File: TestBigQueryTarget.java From datacollector with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") private Field createField(Object object) { Field field; if (object instanceof Map) { Map<String, Object> mapObject = (Map<String, Object>)object; return Field.create(mapObject.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> createField(e.getValue())))); } else if (object instanceof List) { List<Object> listObject = (List<Object>)object; return Field.create(listObject.stream().map(this::createField).collect(Collectors.toList())); } else if (object instanceof Boolean) { field = Field.create((Boolean) object); } else if (object instanceof Character) { field = Field.create((Character) object); } else if (object instanceof Byte) { field = Field.create((Byte) object); } else if (object instanceof Short) { field = Field.create((Short) object); } else if (object instanceof Integer) { field = Field.create((Integer) object); } else if (object instanceof Long) { field = Field.create((Long) object); } else if (object instanceof Float) { field = Field.create((Float) object); } else if (object instanceof Double) { field = Field.create((Double) object); } else if (object instanceof Date) { field = Field.createDatetime((Date) object); } else if (object instanceof BigDecimal) { field = Field.create((BigDecimal) object); } else if (object instanceof String) { field = Field.create((String) object); } else if (object instanceof byte[]) { field = Field.create((byte[]) object); } else if (object instanceof FileRef){ field = Field.create((FileRef)object); } else { throw new IllegalArgumentException(Utils.format("Cannot convert object type '{}' to field", object.getClass())); } return field; }
Example 8
Source File: JsonUtil.java From datacollector with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public static Field jsonToField(Object json) throws IOException { Field field; if (json == null) { field = Field.create(Field.Type.STRING, null); } else if (json instanceof List) { List jsonList = (List) json; List<Field> list = new ArrayList<>(jsonList.size()); for (Object element : jsonList) { list.add(jsonToField(element)); } field = Field.create(list); } else if (json instanceof Map) { Map<String, Object> jsonMap = (Map<String, Object>) json; Map<String, Field> map = new LinkedHashMap<>(); for (Map.Entry<String, Object> entry : jsonMap.entrySet()) { map.put(entry.getKey(), jsonToField(entry.getValue())); } field = Field.create(map); } else if (json instanceof String) { field = Field.create((String) json); } else if (json instanceof Boolean) { field = Field.create((Boolean) json); } else if (json instanceof Character) { field = Field.create((Character) json); } else if (json instanceof Byte) { field = Field.create((Byte) json); } else if (json instanceof Short) { field = Field.create((Short) json); } else if (json instanceof Integer) { field = Field.create((Integer) json); } else if (json instanceof Long) { field = Field.create((Long) json); } else if (json instanceof Float) { field = Field.create((Float) json); } else if (json instanceof Double) { field = Field.create((Double) json); } else if (json instanceof byte[]) { field = Field.create((byte[]) json); } else if (json instanceof Date) { field = Field.createDatetime((Date) json); } else if (json instanceof BigDecimal) { field = Field.create((BigDecimal) json); } else if (json instanceof UUID) { field = Field.create(json.toString()); } else { throw new IOException(Utils.format("Not recognized type '{}', value '{}'", json.getClass(), json)); } return field; }
Example 9
Source File: MapRDBCDCSource.java From datacollector with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") private Field generateField(Object value) { if(value instanceof Integer) { return Field.create((Integer) value); } else if(value instanceof List) { return Field.create(((List<Object>) value).stream().map(this::generateField).collect(Collectors.toList())); } else if(value instanceof byte[]) { return Field.create((byte[]) value); } else if(value instanceof ByteBuffer) { return Field.create(((ByteBuffer) value).array()); } else if(value instanceof Long) { return Field.create((Long) value); } else if(value instanceof Short) { return Field.create((Short) value); } else if(value instanceof Boolean) { return Field.create((Boolean) value); } else if(value instanceof BigDecimal) { return Field.create((BigDecimal) value); } else if(value instanceof Byte) { return Field.create((Byte) value); } else if(value instanceof Float) { return Field.create((Float) value); } else if(value instanceof Double) { return Field.create((Double) value); } else if(value instanceof String) { return Field.create((String) value); } else if(value instanceof OTime) { return Field.createTime(((OTime) value).toDate()); } else if(value instanceof ODate) { return Field.createDate(((ODate) value).toDate()); } else if(value instanceof OTimestamp) { return Field.createDatetime(((OTimestamp)value).toDate()); } else if(value instanceof OInterval) { return Field.create(((OInterval)value).getTimeInMillis()); } else if(value instanceof Map.Entry) { return generateField(((Map.Entry) value).getValue()); } else if(value instanceof Map) { return Field.create(((Map<String, Object>) value).entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, this::generateField))); } else { throw new IllegalArgumentException("Unsupported type " + value.getClass().toString()); } }
Example 10
Source File: FieldTypeConverterProcessor.java From datacollector with Apache License 2.0 | 4 votes |
private Field convertStringToTargetType( Field field, Field.Type targetType, Locale dataLocale, String dateMask, int scale, DecimalScaleRoundingStrategy decimalScaleRoundingStrategy, DateTimeFormatter dateTimeFormatter ) throws ParseException { String stringValue = field.getValueAsString(); switch(targetType) { case BOOLEAN: return Field.create(Field.Type.BOOLEAN, Boolean.valueOf(stringValue.trim())); case BYTE: return Field.create(NumberFormat.getInstance(dataLocale).parse(stringValue).byteValue()); case BYTE_ARRAY: return Field.create(stringValue.getBytes(StandardCharsets.UTF_8)); case CHAR: return Field.create(stringValue.charAt(0)); case DATE: java.text.DateFormat dateFormat = new SimpleDateFormat(dateMask, Locale.ENGLISH); return Field.createDate(dateFormat.parse(stringValue.trim())); case DATETIME: java.text.DateFormat dateTimeFormat = new SimpleDateFormat(dateMask, Locale.ENGLISH); return Field.createDatetime(dateTimeFormat.parse(stringValue.trim())); case TIME: java.text.DateFormat timeFormat = new SimpleDateFormat(dateMask, Locale.ENGLISH); return Field.createTime(timeFormat.parse(stringValue.trim())); case ZONED_DATETIME: return Field.createZonedDateTime(ZonedDateTime.parse(stringValue.trim(), dateTimeFormatter)); case DECIMAL: NumberFormat decimalFormat = NumberFormat.getInstance(dataLocale); DecimalFormat df = (DecimalFormat) decimalFormat; df.setParseBigDecimal(true); Number decimal = df.parse(stringValue.trim()); BigDecimal bigDecimal = adjustScaleIfNeededForDecimalConversion(new BigDecimal(decimal.toString()), scale, decimalScaleRoundingStrategy); Field decimalField = Field.create(Field.Type.DECIMAL, bigDecimal); decimalField.setAttribute(HeaderAttributeConstants.ATTR_PRECISION, String.valueOf(bigDecimal.precision())); decimalField.setAttribute(HeaderAttributeConstants.ATTR_SCALE, String.valueOf(bigDecimal.scale())); return decimalField; case DOUBLE: return Field.create(NumberFormat.getInstance(dataLocale).parse(stringValue.trim()).doubleValue()); case FLOAT: return Field.create(NumberFormat.getInstance(dataLocale).parse(stringValue.trim()).floatValue()); case INTEGER: return Field.create(NumberFormat.getInstance(dataLocale).parse(stringValue.trim()).intValue()); case LONG: return Field.create(NumberFormat.getInstance(dataLocale).parse(stringValue.trim()).longValue()); case SHORT: return Field.create(NumberFormat.getInstance(dataLocale).parse(stringValue.trim()).shortValue()); case FILE_REF: throw new IllegalArgumentException(Utils.format("Cannot convert String value to type {}", targetType)); default: return field; } }