use of org.apache.avro.util.Utf8 in project hadoop by apache.
the class TaskAttemptUnsuccessfulCompletionEvent method getDatum.
public Object getDatum() {
if (datum == null) {
datum = new TaskAttemptUnsuccessfulCompletion();
datum.setTaskid(new Utf8(attemptId.getTaskID().toString()));
datum.setTaskType(new Utf8(taskType.name()));
datum.setAttemptId(new Utf8(attemptId.toString()));
datum.setFinishTime(finishTime);
datum.setHostname(new Utf8(hostname));
if (rackName != null) {
datum.setRackname(new Utf8(rackName));
}
datum.setPort(port);
datum.setError(new Utf8(error));
datum.setStatus(new Utf8(status));
datum.setCounters(EventWriter.toAvro(counters));
datum.setClockSplits(AvroArrayUtils.toAvro(ProgressSplitsBlock.arrayGetWallclockTime(allSplits)));
datum.setCpuUsages(AvroArrayUtils.toAvro(ProgressSplitsBlock.arrayGetCPUTime(allSplits)));
datum.setVMemKbytes(AvroArrayUtils.toAvro(ProgressSplitsBlock.arrayGetVMemKbytes(allSplits)));
datum.setPhysMemKbytes(AvroArrayUtils.toAvro(ProgressSplitsBlock.arrayGetPhysMemKbytes(allSplits)));
}
return datum;
}
use of org.apache.avro.util.Utf8 in project core by s4.
the class AvroSerDeser method copyRecord.
public static GenericRecord copyRecord(Map<String, Object> record, Schema schema, GenericRecord avroRecord) {
Map<String, Field> fieldMap = schema.getFields();
for (String fieldName : record.keySet()) {
Field field = fieldMap.get(fieldName);
if (field == null) {
// not in schema, ignore
continue;
}
Schema fieldSchema = field.schema();
if (fieldSchema.getType().equals(Schema.Type.UNION)) {
List<Schema> actualSchemas = fieldSchema.getTypes();
// this should only contain two entries, the actual type and
// NULL
fieldSchema = null;
for (Schema actualSchema : actualSchemas) {
if (actualSchema.getType().equals(Schema.Type.NULL)) {
continue;
}
fieldSchema = actualSchema;
break;
}
if (fieldSchema == null) {
return avroRecord;
}
}
Object value = record.get(fieldName);
if (value == null) {
continue;
}
if (fieldSchema.getType().equals(Schema.Type.STRING)) {
avroRecord.put(fieldName, new Utf8((String) value));
} else if (fieldSchema.getType().equals(Schema.Type.RECORD)) {
if (!(value instanceof Map)) {
// schema mismatch
continue;
}
avroRecord.put(fieldName, copyRecord((Map<String, Object>) record.get(fieldName), fieldSchema, new GenericData.Record(fieldSchema)));
} else if (fieldSchema.getType().equals(Schema.Type.ARRAY)) {
if (!(value instanceof List)) {
// schema mismatch
continue;
}
List<Map<String, Object>> list = (List<Map<String, Object>>) record.get(fieldName);
GenericArray<GenericRecord> avroArray = new GenericData.Array<GenericRecord>(list.size());
avroRecord.put(fieldName, avroArray);
copyArray(list, fieldSchema.getElementType(), avroArray);
} else if (fieldSchema.getType().equals(Schema.Type.ENUM) || fieldSchema.getType().equals(Schema.Type.UNION) || fieldSchema.getType().equals(Schema.Type.BYTES) || fieldSchema.getType().equals(Schema.Type.MAP)) {
// we don't properly handle that right now
continue;
} else {
avroRecord.put(fieldName, value);
}
}
return avroRecord;
}
use of org.apache.avro.util.Utf8 in project voldemort by voldemort.
the class StringSerializerTest method testStringSerializer.
@Test
public void testStringSerializer() {
String[] schemaInfos = { "utf8", "utf-8", "UTF8", "UTF-8", null };
for (String schemaInfo : schemaInfos) {
Serializer<Object> ser = getStringSerializer(schemaInfo);
assertInverse(ser, "abc");
assertInverse(ser, null);
assertSerializationFails(ser, new Utf8("foobar"));
assertSerializationFails(ser, 'C');
assertSerializationFails(ser, 123);
assertSerializationFails(ser, new Integer(123));
assertSerializationFails(ser, new byte[5]);
}
}
use of org.apache.avro.util.Utf8 in project voldemort by voldemort.
the class AvroGenericSerializerTest method testRoundtripAvroWithGenericRecord.
public void testRoundtripAvroWithGenericRecord() throws Exception {
String jsonSchema = "{\"name\": \"Compact Disk\", \"type\": \"record\", " + "\"fields\": [" + "{\"name\": \"name\", \"type\": \"string\", \"order\": \"ascending\"}" + "]}";
AvroGenericSerializer serializer = new AvroGenericSerializer(jsonSchema);
Record record = new Record(Schema.parse(jsonSchema));
// we need to use a Utf8 instance to map to a String.
record.put("name", new Utf8("Hello"));
byte[] bytes = serializer.toBytes(record);
assertTrue(serializer.toObject(bytes).equals(record));
}
use of org.apache.avro.util.Utf8 in project voldemort by voldemort.
the class AvroGenericSerializerTest method testRoundtripAvroWithString.
public void testRoundtripAvroWithString() throws Exception {
String jsonSchema = "{\"name\": \"Str\", \"type\": \"string\"}";
AvroGenericSerializer serializer = new AvroGenericSerializer(jsonSchema);
byte[] bytes = serializer.toBytes(new Utf8("BAR"));
byte[] bytes2 = serializer.toBytes(new Utf8("BAR"));
assertEquals(ByteUtils.compare(bytes, bytes2), 0);
assertTrue(serializer.toObject(bytes).equals(new Utf8("BAR")));
}
Aggregations