Search in sources :

Example 76 with Utf8

use of org.apache.avro.util.Utf8 in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_HappyPath.

/**
   * Verifies that getMetadata() returns the expected GenericRecord for the event's
   * metadata and that it has the expected fields and values in it.
   */
@Test
public void testGetMetadata_HappyPath() throws Exception {
    LOG.info("starting testGetMetadata_HappyPath()");
    // build the event's metadata and then the event
    DbusEventPart metadataPart = createMetadataPart();
    DbusEvent event = createEvent(metadataPart);
    // create a metadata schema set that correctly corresponds to the metadata
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, // METADATA_SCHEMA_VERSION
    metadataPart.getSchemaVersion(), // METADATA_SCHEMA_CHECKSUM
    new SchemaId(metadataPart.getSchemaDigest()), CORRECT_METADATA_SCHEMA, // preserve original string
    true);
    // now create the decoder and use it to extract and decode the event's metadata
    DbusEventAvroDecoder eventDecoder = createDecoder(metadataSchemaSet);
    try {
        GenericRecord reuse = null;
        GenericRecord decodedMetadata = eventDecoder.getMetadata(event, reuse);
        Assert.assertNotNull(decodedMetadata, "getMetadata() returned null GenericRecord;");
        Utf8 etag = (Utf8) decodedMetadata.get("etag");
        Assert.assertEquals(etag.toString(), "dunno what an etag is");
        Integer flags = (Integer) decodedMetadata.get("flags");
        Assert.assertEquals(flags, null, "expected flags to be null");
        Long expires = (Long) decodedMetadata.get("expires");
        Assert.assertNotNull(expires, "expected expires to have a value;");
        Assert.assertEquals(expires.longValue(), 1366150681);
        Utf8 nonexistentField = (Utf8) decodedMetadata.get("nonexistentField");
        Assert.assertNull(nonexistentField, "unexpected value for 'nonexistentField';");
    } catch (Exception ex) {
        Assert.fail("unexpected error decoding metadata: " + ex);
    }
    LOG.info("leaving testGetMetadata_HappyPath()");
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) SchemaId(com.linkedin.databus2.schemas.SchemaId) Utf8(org.apache.avro.util.Utf8) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 77 with Utf8

use of org.apache.avro.util.Utf8 in project pinot by linkedin.

the class AvroRecordReader method getGenericRow.

private GenericRow getGenericRow(GenericRecord rawRecord, GenericRow row) {
    for (final Field field : _dataStream.getSchema().getFields()) {
        FieldSpec spec = _schemaExtractor.getSchema().getFieldSpecFor(field.name());
        if (spec == null) {
            continue;
        }
        Object value = rawRecord.get(field.name());
        if (value == null) {
            incrementNullCountFor(field.name());
            if (spec.isSingleValueField()) {
                value = spec.getDefaultNullValue();
            } else {
                value = transformAvroArrayToObjectArray((Array) value, spec);
            }
        } else {
            if (value instanceof Utf8) {
                value = ((Utf8) value).toString();
            }
            if (value instanceof Array) {
                value = transformAvroArrayToObjectArray((Array) value, spec);
            }
        }
        row.putField(field.name(), value);
    }
    return row;
}
Also used : Array(org.apache.avro.generic.GenericData.Array) Field(org.apache.avro.Schema.Field) Utf8(org.apache.avro.util.Utf8) FieldSpec(com.linkedin.pinot.common.data.FieldSpec)

Example 78 with Utf8

use of org.apache.avro.util.Utf8 in project pinot by linkedin.

the class AvroRecordReader method transformAvroArrayToObjectArray.

public static Object[] transformAvroArrayToObjectArray(Array arr, FieldSpec spec) {
    if (arr == null) {
        return new Object[] { getDefaultNullValue(spec) };
    }
    if (arr.size() == 0) {
        return new Object[] { getDefaultNullValue(spec) };
    }
    final Object[] ret = new Object[arr.size()];
    final Iterator iterator = arr.iterator();
    int i = 0;
    while (iterator.hasNext()) {
        Object value = iterator.next();
        if (value instanceof Record) {
            value = ((Record) value).get(0);
        }
        if (value instanceof Utf8) {
            value = ((Utf8) value).toString();
        }
        if (value == null) {
            value = getDefaultNullValue(spec);
        }
        ret[i++] = value;
    }
    return ret;
}
Also used : Iterator(java.util.Iterator) Utf8(org.apache.avro.util.Utf8) Record(org.apache.avro.generic.GenericData.Record) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 79 with Utf8

use of org.apache.avro.util.Utf8 in project pinot by linkedin.

the class SegmentTestUtils method transformAvroArrayToObjectArray.

private static Object[] transformAvroArrayToObjectArray(Array arr) {
    if (arr == null) {
        return new Object[0];
    }
    final Object[] ret = new Object[arr.size()];
    final Iterator iterator = arr.iterator();
    int i = 0;
    while (iterator.hasNext()) {
        Object value = iterator.next();
        if (value instanceof Record) {
            value = ((Record) value).get(0);
        }
        if (value instanceof Utf8) {
            value = ((Utf8) value).toString();
        }
        ret[i++] = value;
    }
    return ret;
}
Also used : Iterator(java.util.Iterator) Utf8(org.apache.avro.util.Utf8) Record(org.apache.avro.generic.GenericData.Record) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 80 with Utf8

use of org.apache.avro.util.Utf8 in project hadoop by apache.

the class JobFinishedEvent method getDatum.

public Object getDatum() {
    if (datum == null) {
        datum = new JobFinished();
        datum.setJobid(new Utf8(jobId.toString()));
        datum.setFinishTime(finishTime);
        datum.setFinishedMaps(finishedMaps);
        datum.setFinishedReduces(finishedReduces);
        datum.setFailedMaps(failedMaps);
        datum.setFailedReduces(failedReduces);
        datum.setMapCounters(EventWriter.toAvro(mapCounters, "MAP_COUNTERS"));
        datum.setReduceCounters(EventWriter.toAvro(reduceCounters, "REDUCE_COUNTERS"));
        datum.setTotalCounters(EventWriter.toAvro(totalCounters, "TOTAL_COUNTERS"));
    }
    return datum;
}
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