Search in sources :

Example 81 with Utf8

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;
}
Also used : Utf8(org.apache.avro.util.Utf8)

Example 82 with Utf8

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;
}
Also used : Schema(org.apache.avro.Schema) GenericData(org.apache.avro.generic.GenericData) GenericArray(org.apache.avro.generic.GenericArray) Field(org.apache.avro.Schema.Field) Utf8(org.apache.avro.util.Utf8) ArrayList(java.util.ArrayList) List(java.util.List) GenericRecord(org.apache.avro.generic.GenericRecord) HashMap(java.util.HashMap) Map(java.util.Map)

Example 83 with Utf8

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]);
    }
}
Also used : Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 84 with Utf8

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));
}
Also used : Utf8(org.apache.avro.util.Utf8) Record(org.apache.avro.generic.GenericData.Record)

Example 85 with Utf8

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")));
}
Also used : Utf8(org.apache.avro.util.Utf8)

Aggregations

Utf8 (org.apache.avro.util.Utf8)123 Test (org.junit.Test)34 WebPage (org.apache.gora.examples.generated.WebPage)32 GenericRecord (org.apache.avro.generic.GenericRecord)17 Schema (org.apache.avro.Schema)14 GenericData (org.apache.avro.generic.GenericData)13 ByteBuffer (java.nio.ByteBuffer)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Employee (org.apache.gora.examples.generated.Employee)11 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Field (org.apache.avro.Schema.Field)6 Record (org.apache.avro.generic.GenericData.Record)5 File (java.io.File)4 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)4 Metadata (org.apache.gora.examples.generated.Metadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Iterator (java.util.Iterator)3 List (java.util.List)3