Java Code Examples for com.streamsets.pipeline.api.Field#createDate()

The following examples show how to use com.streamsets.pipeline.api.Field#createDate() . 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 vote down vote up
/**
 * 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: Cells.java    From datacollector with Apache License 2.0 5 votes vote down vote up
static Field parseCell(Cell cell, FormulaEvaluator evaluator) throws ExcelUnsupportedCellTypeException {
  CellType cellType = cell.getCellTypeEnum();
  // set the cellType of a formula cell to its cached formula result type in order to process it as its result type
  boolean isFormula = cell.getCellTypeEnum().equals(CellType.FORMULA);
  if (isFormula) {
    cellType = cell.getCachedFormulaResultTypeEnum();
  }

  switch (cellType) {
    case STRING:
      return Field.create(cell.getStringCellValue());
    case NUMERIC:
      Double rawValue = cell.getNumericCellValue();  // resolves formulas automatically and gets value without cell formatting
      String displayValue = isFormula ? evaluator.evaluate(cell).formatAsString() : dataFormatter.formatCellValue(cell);
      boolean numericallyEquivalent = false;
      try {
        numericallyEquivalent = Double.parseDouble(displayValue) == rawValue;
      } catch (NumberFormatException e) { }

      if (DateUtil.isCellDateFormatted(cell)) {
        // It's a date, not a number
        java.util.Date dt = cell.getDateCellValue();
        // if raw number is < 1 then it's a time component only, otherwise date.
        return rawValue < 1 ? Field.createTime(dt) : Field.createDate(dt);
      }

      // some machinations to handle integer values going in without decimal vs. with .0 for rawValue
      return Field.create(numericallyEquivalent ? new BigDecimal(displayValue) : BigDecimal.valueOf(rawValue));

    case BOOLEAN:
      return Field.create(cell.getBooleanCellValue());
    case BLANK:
      return Field.create("");
    default:
      throw new ExcelUnsupportedCellTypeException(cell, cellType);
  }
}
 
Example 3
Source File: MapRDBCDCSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
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 4
Source File: JsonCharDataParser.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected Field jsonToField(Object json, long offset) throws DataParserException {
  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, offset));
    }
    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(), offset));
    }
    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.createDate((Date) json);
  } else if (json instanceof BigDecimal) {
    field = Field.create((BigDecimal) json);
  } else if (json instanceof BigInteger) {
    field = Field.create(new BigDecimal((BigInteger) json));
  } else {
    throw new DataParserException(Errors.JSON_PARSER_01, readerId, offset, json.getClass().getSimpleName());
  }
  return field;
}
 
Example 5
Source File: SdkJsonDataFormatParserService.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private static Field jsonToField(Object json) {
  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.createDate((Date) json);
  } else if (json instanceof BigDecimal) {
    field = Field.create((BigDecimal) json);
  } else if (json instanceof BigInteger) {
    field = Field.create(new BigDecimal((BigInteger) json));
  } else {
    throw new IllegalArgumentException("Unknown type: " + json.getClass().getSimpleName());
  }
  return field;
}
 
Example 6
Source File: MapRDBCDCSource.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@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 7
Source File: FieldTypeConverterProcessor.java    From datacollector with Apache License 2.0 4 votes vote down vote up
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;
  }
}