當前位置: 首頁>>代碼示例>>Java>>正文


Java GenericData.Fixed方法代碼示例

本文整理匯總了Java中org.apache.avro.generic.GenericData.Fixed方法的典型用法代碼示例。如果您正苦於以下問題:Java GenericData.Fixed方法的具體用法?Java GenericData.Fixed怎麽用?Java GenericData.Fixed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.avro.generic.GenericData的用法示例。


在下文中一共展示了GenericData.Fixed方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getAvroValue

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
private static Object getAvroValue(Object input, Schema schema) {
    if (input instanceof byte[] && Schema.Type.FIXED.equals(schema.getType())) {
        return new GenericData.Fixed(schema, (byte[]) input);
    } else if (input instanceof Map && !((Map) input).isEmpty()) {
        GenericRecord result;
        result = new GenericData.Record(schema);
        for (Map.Entry<String, Object> entry: ((Map<String, Object>) input).entrySet()) {
            result.put(entry.getKey(), getAvroValue(entry.getValue(), schema.getField(entry.getKey()).schema()));
        }
        return result;
    } else if (input instanceof Collection && !((Collection) input).isEmpty()) {
        // for array even though we(Schema in streamline registry) support different types of elements in an array, avro expects an array
        // schema to have elements of same type. Hence, for now we will restrict array to have elements of same type. Other option is convert
        // a  streamline Schema Array field to Record in avro. However, with that the issue is that avro Field constructor does not allow a
        // null name. We could potentiall hack it by plugging in a dummy name like arrayfield, but seems hacky so not taking that path
        List<Object> values = new ArrayList<>(((Collection) input).size());
        for (Object value: (Collection) input) {
            values.add(getAvroValue(value, schema.getElementType()));
        }
        return new GenericData.Array<Object>(schema, values);
    } else {
        return input;
    }
}
 
開發者ID:hortonworks,項目名稱:streamline,代碼行數:25,代碼來源:StreamlineEventSerializer.java

示例2: wrapOption

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private Object wrapOption(Schema schema, Object option) {
  if (schema.getType() == Schema.Type.BYTES && option instanceof String) {
    option = ByteBuffer.wrap(((String) option).getBytes(Charset.defaultCharset()));
  } else if (schema.getType() == Schema.Type.FLOAT && option instanceof Double) {
    option = ((Double) option).floatValue();
  } else if (schema.getType() == Schema.Type.LONG && option instanceof Integer) {
    option = ((Integer) option).longValue();
  } else if (schema.getType() == Schema.Type.ARRAY && option instanceof Collection) {
    option = new GenericData.Array(schema, (Collection) option);
  } else if (schema.getType() == Schema.Type.ENUM && option instanceof String) {
    option = new GenericData.EnumSymbol(schema, (String) option);
  } else if (schema.getType() == Schema.Type.FIXED && option instanceof String) {
    option =
        new GenericData.Fixed(schema, ((String) option).getBytes(Charset.defaultCharset()));
  } else if (schema.getType() == Schema.Type.RECORD && option instanceof Map) {
    Map optionMap = (Map) option;
    GenericRecordBuilder optionBuilder = new GenericRecordBuilder(schema);
    for (Schema.Field field : schema.getFields()) {
      if (optionMap.containsKey(field.name())) {
        optionBuilder.set(field, optionMap.get(field.name()));
      }
    }
    option = optionBuilder.build();
  }
  return option;
}
 
開發者ID:C0urante,項目名稱:avro-random-generator,代碼行數:28,代碼來源:Generator.java

示例3: testCreateFixedField

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@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);

  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]);
  }

}
 
開發者ID:streamsets,項目名稱:datacollector,代碼行數:33,代碼來源:TestAvroTypeUtil.java

示例4: createWrapperForType

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
static private VisitableStructure createWrapperForType(Schema.Type type, Object value, TraversalPath path) {
    switch (type) {
    case INT:
        return new VisitableInt((Integer) value, path);
    case LONG:
        return new VisitableLong((Long) value, path);
    case RECORD:
        return new VisitableRecord((IndexedRecord) value, path);
    case MAP:
        return new VisitableMap((Map<Utf8, Object>) value, path);
    case STRING:
        return new VisitableString(value.toString(), path);
    case BOOLEAN:
        return new VisitableBoolean((Boolean) value, path);
    case FLOAT:
        return new VisitableFloat((Float) value, path);
    case DOUBLE:
        return new VisitableDouble((Double) value, path);
    case NULL:
        return new VisitableNull(path);
    case ENUM:
        return new VisitableString(value.toString(), path);
    case FIXED:
        return new VisitableFixed((GenericData.Fixed) value, path);
    case BYTES:
        return new VisitableBytes((ByteBuffer) value, path);
    // note: UNION is not supported yet
    default:
        throw new IllegalArgumentException("Unsupported Avro data type: " + type);
    }
}
 
開發者ID:Talend,項目名稱:daikon,代碼行數:32,代碼來源:VisitableStructureFactory.java

示例5: apply

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@Override
public GenericFixed apply(GeneratorContext input) {
    if (schema == null) {
        schema = new Schema.Parser().parse(jsonSchema);
    }
    byte[] buffer = new byte[size];
    input.getRandom().nextBytes(buffer);
    return new GenericData.Fixed(schema, buffer);
}
 
開發者ID:Talend,項目名稱:components,代碼行數:10,代碼來源:GeneratorFunctions.java

示例6: stringColumn

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
@Override
public GenericFixed stringColumn(String value) {
    return new GenericData.Fixed(avroSchema, value.getBytes());
}
 
開發者ID:joker1007,項目名稱:embulk-formatter-avro,代碼行數:5,代碼來源:AvroFixedConverter.java

示例7: generateFixed

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
private GenericFixed generateFixed(Schema schema) {
  byte[] bytes = new byte[schema.getFixedSize()];
  random.nextBytes(bytes);
  return new GenericData.Fixed(schema, bytes);
}
 
開發者ID:C0urante,項目名稱:avro-random-generator,代碼行數:6,代碼來源:Generator.java

示例8: VisitableFixed

import org.apache.avro.generic.GenericData; //導入方法依賴的package包/類
VisitableFixed(GenericData.Fixed value, TraversalPath path) {
    super(value, path);
}
 
開發者ID:Talend,項目名稱:daikon,代碼行數:4,代碼來源:VisitableFixed.java


注:本文中的org.apache.avro.generic.GenericData.Fixed方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。