本文整理汇总了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;
}
}
示例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;
}
示例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]);
}
}
示例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);
}
}
示例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);
}
示例6: stringColumn
import org.apache.avro.generic.GenericData; //导入方法依赖的package包/类
@Override
public GenericFixed stringColumn(String value) {
return new GenericData.Fixed(avroSchema, value.getBytes());
}
示例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);
}
示例8: VisitableFixed
import org.apache.avro.generic.GenericData; //导入方法依赖的package包/类
VisitableFixed(GenericData.Fixed value, TraversalPath path) {
super(value, path);
}