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

The following examples show how to use com.streamsets.pipeline.api.Field#getValueAsFloat() . 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: Matchers.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static Matcher<Field> fieldWithValue(final float value) {
  return new FieldMatcher(Field.Type.FLOAT, value) {
    @Override
    protected Object getValueFromField(Field field) {
      return field.getValueAsFloat();
    }
  };
}
 
Example 2
Source File: ProtobufTypeUtil.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private static Object getValue(
    Descriptors.FieldDescriptor f,
    Field field,
    Record record,
    String protoFieldPath,
    Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
    Map<String, Object> defaultValueMap
) throws DataGeneratorException {
  Object value = null;
  try {
    if (field.getValue() != null) {
      switch (f.getJavaType()) {
        case BOOLEAN:
          value = field.getValueAsBoolean();
          break;
        case BYTE_STRING:
          value = ByteString.copyFrom(field.getValueAsByteArray());
          break;
        case DOUBLE:
          value = field.getValueAsDouble();
          break;
        case ENUM:
          value = f.getEnumType().findValueByName(field.getValueAsString());
          break;
        case FLOAT:
          value = field.getValueAsFloat();
          break;
        case INT:
          value = field.getValueAsInteger();
          break;
        case LONG:
          value = field.getValueAsLong();
          break;
        case STRING:
          value = field.getValueAsString();
          break;
        case MESSAGE:
          Descriptors.Descriptor messageType = f.getMessageType();
          value = sdcFieldToProtobufMsg(
              record, field, protoFieldPath, messageType, messageTypeToExtensionMap, defaultValueMap
          );
          break;
        default:
          throw new DataGeneratorException(Errors.PROTOBUF_03, f.getJavaType().name());
      }
    }
  } catch (IllegalArgumentException e) {
    throw new DataGeneratorException(Errors.PROTOBUF_11, field.getValue(), f.getJavaType().name(), e);
  }
  return value;
}
 
Example 3
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 4
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;
}