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

The following examples show how to use com.streamsets.pipeline.api.Field#getValueAsByteArray() . 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: TestAvroTypeUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateFixedField() throws Exception {
  byte[] bytes = new byte[16];
  for (int i = 0; i < 16; i++) {
    bytes[i] = (byte) i;
  }

  String schema = "{\"type\": \"fixed\", \"size\": 16, \"name\": \"md5\"}";
  Schema avroSchema = new Schema.Parser().parse(schema);
  GenericData.Fixed fixed = new GenericData.Fixed(avroSchema, bytes);

  Record record = RecordCreator.create();
  Field field = AvroTypeUtil.avroToSdcField(record, avroSchema, fixed, false);

  Assert.assertEquals(Field.Type.BYTE_ARRAY, field.getType());
  byte[] valueAsByteArray = field.getValueAsByteArray();
  Assert.assertEquals(16, valueAsByteArray.length);
  for (int i = 0; i < 16; i++) {
    Assert.assertEquals(i, valueAsByteArray[i]);
  }

  record.set(field);
  Object avroObject = AvroTypeUtil.sdcRecordToAvro(record, avroSchema, new HashMap<String, Object>());
  Assert.assertTrue(avroObject instanceof GenericData.Fixed);
  GenericData.Fixed result = (GenericData.Fixed) avroObject;

  byte[] bytes1 = result.bytes();
  for (int i = 0; i < 16; i++) {
    Assert.assertEquals(i, bytes1[i]);
  }

}
 
Example 2
Source File: FieldEncrypter.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * Does data type conversions in preparation for encryption.
 *
 * @param field {@link Field} to encrypt
 * @param context (AAD)
 * @return byte array to encrypt
 */
public byte[] prepareEncrypt(Field field, Map<String, String> context) {
  context.put(SDC_FIELD_TYPE, field.getType().name());

  if (field.getType() == Field.Type.BYTE_ARRAY) {
    return field.getValueAsByteArray();
  } else {
    // Treat all other data as strings
    return field.getValueAsString().getBytes(Charsets.UTF_8);
  }
}
 
Example 3
Source File: MapRJsonTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static byte [] convertToByteArray(Field field, Record rec) throws OnRecordErrorException {

  switch (field.getType()) {
    case DOUBLE:
      return Bytes.toBytes(field.getValueAsDouble());

    case FLOAT:
      return Bytes.toBytes(field.getValueAsFloat());

    case INTEGER:
      return Bytes.toBytes(field.getValueAsInteger());

    case SHORT:
      return Bytes.toBytes(field.getValueAsShort());

    case LONG:
    case DATE:
    case TIME:
    case DATETIME:
      return Bytes.toBytes(field.getValueAsLong());

    case STRING:
      return Bytes.toBytes(field.getValueAsString());

    case BYTE_ARRAY:
      return field.getValueAsByteArray();

    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());
  }
}
 
Example 4
Source File: BigtableTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private byte[] convertToBinary(Field field, Record record) throws OnRecordErrorException {
  byte[] value;
  switch (field.getType()) {
    case BOOLEAN:
      value = Bytes.toBytes(field.getValueAsBoolean());
      break;
    case BYTE:
      value = Bytes.toBytes(field.getValueAsByte());
      break;
    case BYTE_ARRAY:
      value = field.getValueAsByteArray();
      break;
    case CHAR:
      value = Bytes.toBytes(field.getValueAsChar());
      break;
    case DATE:
      throw new OnRecordErrorException(record,
          Errors.BIGTABLE_12,
          Field.Type.DATE.name(),
          BigtableStorageType.BINARY.name()
      );
    case TIME:
      throw new OnRecordErrorException(record,
          Errors.BIGTABLE_12,
          Field.Type.TIME.name(),
          BigtableStorageType.BINARY.name()
      );
    case DATETIME:
      throw new OnRecordErrorException(record,
          Errors.BIGTABLE_12,
          Field.Type.DATETIME.name(),
          BigtableStorageType.BINARY.name()
      );
    case DECIMAL:
      value = Bytes.toBytes(field.getValueAsDecimal());
      break;
    case DOUBLE:
      value = Bytes.toBytes(field.getValueAsDouble());
      break;
    case FLOAT:
      value = Bytes.toBytes(field.getValueAsFloat());
      break;
    case INTEGER:
      value = Bytes.toBytes(field.getValueAsInteger());
      break;
    case LIST:
      throw new OnRecordErrorException(record,
          Errors.BIGTABLE_12,
          Field.Type.LIST.name(),
          BigtableStorageType.BINARY.name()
      );
    case LIST_MAP:
      throw new OnRecordErrorException(record,
          Errors.BIGTABLE_12,
          Field.Type.LIST_MAP.name(),
          BigtableStorageType.BINARY.name()
      );
    case LONG:
      value = Bytes.toBytes(field.getValueAsLong());
      break;
    case MAP:
      throw new OnRecordErrorException(Errors.BIGTABLE_12,
          Field.Type.MAP.name(),
          BigtableStorageType.BINARY.name(),
          record
      );
    case SHORT:
      value = Bytes.toBytes(field.getValueAsShort());
      break;
    case STRING:
      throw new OnRecordErrorException(record,
          Errors.BIGTABLE_12,
          Field.Type.STRING.name(),
          BigtableStorageType.BINARY.name()
      );
    default:
      throw new OnRecordErrorException(record, Errors.BIGTABLE_13, field.toString());
  }
  return value;
}
 
Example 5
Source File: FieldEncrypter.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public byte[] prepareDecrypt(Field field) {
  return field.getValueAsByteArray();
}
 
Example 6
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;
}
 
Example 7
Source File: AbstractHBaseProducer.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private byte[] convertToBinary(Field field, Record record) throws OnRecordErrorException {
  byte[] value;
  switch (field.getType()) {
    case BOOLEAN:
      value = Bytes.toBytes(field.getValueAsBoolean());
      break;
    case BYTE:
      value = Bytes.toBytes(field.getValueAsByte());
      break;
    case BYTE_ARRAY:
      value = field.getValueAsByteArray();
      break;
    case CHAR:
      value = Bytes.toBytes(field.getValueAsChar());
      break;
    case DATE:
      throw new OnRecordErrorException(record, Errors.HBASE_12, Field.Type.DATE.name(), StorageType.BINARY.name());
    case TIME:
      throw new OnRecordErrorException(record, Errors.HBASE_12, Field.Type.TIME.name(), StorageType.BINARY.name());
    case DATETIME:
      throw new OnRecordErrorException(record,
          Errors.HBASE_12,
          Field.Type.DATETIME.name(),
          StorageType.BINARY.name()
      );
    case DECIMAL:
      value = Bytes.toBytes(field.getValueAsDecimal());
      break;
    case DOUBLE:
      value = Bytes.toBytes(field.getValueAsDouble());
      break;
    case FLOAT:
      value = Bytes.toBytes(field.getValueAsFloat());
      break;
    case INTEGER:
      value = Bytes.toBytes(field.getValueAsInteger());
      break;
    case LIST:
      throw new OnRecordErrorException(record, Errors.HBASE_12, Field.Type.LIST.name(), StorageType.BINARY.name());
    case LIST_MAP:
      throw new OnRecordErrorException(record,
          Errors.HBASE_12,
          Field.Type.LIST_MAP.name(),
          StorageType.BINARY.name()
      );
    case LONG:
      value = Bytes.toBytes(field.getValueAsLong());
      break;
    case MAP:
      throw new OnRecordErrorException(Errors.HBASE_12, Field.Type.MAP.name(), StorageType.BINARY.name(), record);
    case SHORT:
      value = Bytes.toBytes(field.getValueAsShort());
      break;
    case STRING:
      throw new OnRecordErrorException(record, Errors.HBASE_12, Field.Type.STRING.name(), StorageType.BINARY.name());
    default:
      throw new FieldConversionException("This shouldn't happen: Conversion not defined for " + field.toString());
  }
  return value;
}