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

The following examples show how to use com.streamsets.pipeline.api.Field#getValueAsShort() . 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: FieldValueReplacerProcessor.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private int compareIt(Field field, String stringValue, String matchingField) {
  try {
    switch (field.getType()) {
      case BYTE:
        if (field.getValueAsByte() == (Byte) convertToType(stringValue, field.getType(), matchingField)) {
          return 0;
        } else if (field.getValueAsByte() < (Byte) convertToType(stringValue, field.getType(), matchingField)) {
          return -1;
        } else {
          return 1;
        }

      case SHORT:
        if (field.getValueAsShort() == (Short) convertToType(stringValue, field.getType(), matchingField)) {
          return 0;
        } else if (field.getValueAsShort() < (Short) convertToType(stringValue, field.getType(), matchingField)) {
          return -1;
        } else {
          return 1;
        }

      case INTEGER:
        if (field.getValueAsInteger() == (Integer) convertToType(stringValue, field.getType(), matchingField)) {
          return 0;
        } else if (field.getValueAsInteger() < (Integer) convertToType(stringValue, field.getType(), matchingField)) {
          return -1;
        } else {
          return 1;
        }

      case LONG:
        if (field.getValueAsLong() == (Long) convertToType(stringValue, field.getType(), matchingField)) {
          return 0;
        } else if (field.getValueAsLong() < (Long) convertToType(stringValue, field.getType(), matchingField)) {
          return -1;
        } else {
          return 1;
        }

      case FLOAT:
        if (field.getValueAsFloat() == (Float) convertToType(stringValue, field.getType(), matchingField)) {
          return 0;
        } else if (field.getValueAsFloat() < (Float) convertToType(stringValue, field.getType(), matchingField)) {
          return -1;
        } else {
          return 1;
        }

      case DOUBLE:
        if (field.getValueAsDouble() == (Double) convertToType(stringValue, field.getType(), matchingField)) {
          return 0;
        } else if (field.getValueAsDouble() < (Double) convertToType(stringValue, field.getType(), matchingField)) {
          return -1;
        } else {
          return 1;
        }

      case STRING:
        return field.getValueAsString().compareTo(stringValue);

      default:
        throw new IllegalArgumentException(Utils.format(Errors.VALUE_REPLACER_03.getMessage(), field.getType(), matchingField));
    }
  } catch (Exception e) {
    if (e instanceof IllegalArgumentException) {
      throw (IllegalArgumentException)e;
    } else {
      throw new IllegalArgumentException(Utils.format(Errors.VALUE_REPLACER_03.getMessage(), field.getType()), e);
    }
  }
}
 
Example 2
Source File: MapRJsonTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private DocumentMutation populateDocumentMutation(Record rec) throws OnRecordErrorException {
  DocumentMutation documentMutation = MapRJsonDocumentLoader.createDocumentMutation();
  boolean replace = mapRJsonConfigBean.setOrReplace == SetOrReplace.REPLACE;

  if(rec != null && (rec.get().getType() == Field.Type.LIST_MAP || rec.get().getType() == Field.Type.MAP)) {
    Map<String, Field> fields = rec.get().getValueAsMap();
    for(Map.Entry<String, Field> entry : fields.entrySet()) {
      String path = entry.getKey();
      Field field = entry.getValue();

      //don't add the keyField to the document mutation set. that gets added later
      if(entry.getKey().equals(mapRJsonConfigBean.keyField)) {
        continue;
      }
      switch(field.getType()) {
        case DOUBLE:
          double d = field.getValueAsDouble();
          documentMutation = (replace ? documentMutation.setOrReplace(path, d) : documentMutation.set(path, d));
          break;
        case FLOAT:
          float f = field.getValueAsFloat();
          documentMutation = (replace ? documentMutation.setOrReplace(path, f) : documentMutation.set(path, f));
          break;
        case INTEGER:
          int i = field.getValueAsInteger();
          documentMutation = (replace ? documentMutation.setOrReplace(path, i) : documentMutation.set(path, i));
          break;
        case SHORT:
          short s = field.getValueAsShort();
          documentMutation = (replace ? documentMutation.setOrReplace(path, s) : documentMutation.set(path, s));
          break;
        case LONG:
        case DATE:
        case TIME:
        case DATETIME:
          long l = field.getValueAsLong();
          documentMutation = (replace ? documentMutation.setOrReplace(path, l) : documentMutation.set(path, l));
          break;
        case STRING:
          String st = field.getValueAsString();
          documentMutation = (replace ? documentMutation.setOrReplace(path, st) : documentMutation.set(path, st));
          break;
        case BYTE_ARRAY:
          byte[] ba = field.getValueAsByteArray();
          documentMutation = (replace ? documentMutation.setOrReplace(path, ByteBuffer.wrap(ba)) : documentMutation.set(path, ByteBuffer.wrap(ba)));
          break;
        case BOOLEAN:
        case MAP:
        case LIST:
        case LIST_MAP:
        case CHAR:
        case BYTE:
        default:
          throw new OnRecordErrorException(rec, Errors.MAPR_JSON_14, field.getType().name());
      }
    }
  }

  return documentMutation;
}