Search in sources :

Example 6 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project flink by apache.

the class AvroRowDeSerializationSchemaTest method testSpecificSerializeFromClassSeveralTimes.

@Test
public void testSpecificSerializeFromClassSeveralTimes() throws IOException {
    final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> testData = AvroTestUtils.getSpecificTestData();
    final AvroRowSerializationSchema serializationSchema = new AvroRowSerializationSchema(testData.f0);
    final AvroRowDeserializationSchema deserializationSchema = new AvroRowDeserializationSchema(testData.f0);
    serializationSchema.serialize(testData.f2);
    serializationSchema.serialize(testData.f2);
    final byte[] bytes = serializationSchema.serialize(testData.f2);
    final Row actual = deserializationSchema.deserialize(bytes);
    assertEquals(testData.f2, actual);
}
Also used : SpecificRecord(org.apache.avro.specific.SpecificRecord) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 7 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project flink by apache.

the class AvroRowDeSerializationSchemaTest method testSpecificDeserializeFromSchemaSeveralTimes.

@Test
public void testSpecificDeserializeFromSchemaSeveralTimes() throws IOException {
    final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> testData = AvroTestUtils.getSpecificTestData();
    final String schemaString = testData.f1.getSchema().toString();
    final AvroRowSerializationSchema serializationSchema = new AvroRowSerializationSchema(schemaString);
    final AvroRowDeserializationSchema deserializationSchema = new AvroRowDeserializationSchema(schemaString);
    final byte[] bytes = serializationSchema.serialize(testData.f2);
    deserializationSchema.deserialize(bytes);
    deserializationSchema.deserialize(bytes);
    final Row actual = deserializationSchema.deserialize(bytes);
    assertEquals(testData.f2, actual);
}
Also used : SpecificRecord(org.apache.avro.specific.SpecificRecord) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 8 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project registry by hortonworks.

the class DefaultAvroSerDesHandler method handlePayloadSerialization.

@Override
public void handlePayloadSerialization(OutputStream outputStream, Object input) {
    try {
        Schema schema = AvroUtils.computeSchema(input);
        Schema.Type schemaType = schema.getType();
        if (Schema.Type.BYTES.equals(schemaType)) {
            // incase of byte arrays, no need to go through avro as there is not much to optimize and avro is expecting
            // the payload to be ByteBuffer instead of a byte array
            outputStream.write((byte[]) input);
        } else if (Schema.Type.STRING.equals(schemaType)) {
            // get UTF-8 bytes and directly send those over instead of using avro.
            outputStream.write(input.toString().getBytes("UTF-8"));
        } else {
            BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
            DatumWriter<Object> writer;
            boolean isSpecificRecord = input instanceof SpecificRecord;
            if (isSpecificRecord) {
                writer = new SpecificDatumWriter<>(schema);
            } else {
                writer = new GenericDatumWriter<>(schema);
            }
            writer.write(input, encoder);
            encoder.flush();
        }
    } catch (IOException e) {
        throw new AvroRetryableException(e);
    } catch (RuntimeException e) {
        throw new AvroException(e);
    }
}
Also used : GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) DatumWriter(org.apache.avro.io.DatumWriter) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) BinaryEncoder(org.apache.avro.io.BinaryEncoder) AvroException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) AvroRetryableException(com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 9 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project spf4j by zolyfarkas.

the class SpecificRecordAppender method writeSerializationError.

@SuppressFBWarnings("ITC_INHERITANCE_TYPE_CHECKING")
static void writeSerializationError(final Object object, final StringBuilder sb, final Exception ex) throws IOException {
    if (STRICT_SERIALIZATION) {
        if (ex instanceof IOException) {
            throw (IOException) ex;
        } else if (ex instanceof RuntimeException) {
            throw (RuntimeException) ex;
        } else {
            throw new IllegalStateException(ex);
        }
    }
    sb.setLength(0);
    sb.append("{\"SerializationError\":\n");
    try (AppendableOutputStream bos = new AppendableOutputStream(sb, Charsets.UTF_8)) {
        JThrowable at = Converters.convert(ex);
        Schema schema = at.getSchema();
        SpecificDatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(schema);
        JsonEncoder jsonEncoder = EF.jsonEncoder(schema, bos, true);
        writer.write(at, jsonEncoder);
        jsonEncoder.flush();
    }
    sb.append(",\n");
    sb.append("\"ObjectAsString\":\n\"");
    EscapeJsonStringAppendableWrapper escaper = new EscapeJsonStringAppendableWrapper(sb);
    escaper.append(object.toString());
    sb.append("\"}");
}
Also used : JThrowable(org.spf4j.base.avro.JThrowable) JsonEncoder(org.apache.avro.io.JsonEncoder) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) AppendableOutputStream(org.spf4j.io.AppendableOutputStream) EscapeJsonStringAppendableWrapper(org.spf4j.base.EscapeJsonStringAppendableWrapper) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 10 with SpecificRecord

use of org.apache.avro.specific.SpecificRecord in project samza by apache.

the class AzureBlobAvroWriter method startNextBlob.

private void startNextBlob(Optional<IndexedRecord> optionalIndexedRecord) throws IOException {
    if (currentBlobWriterComponents != null) {
        LOG.info("Starting new blob as current blob size is " + currentBlobWriterComponents.azureBlobOutputStream.getSize() + " and max blob size is " + maxBlobSize + " or number of records is " + recordsInCurrentBlob + " and max records in blob is " + maxRecordsPerBlob);
        currentBlobWriterComponents.dataFileWriter.flush();
        currentBlobWriterComponents.azureBlobOutputStream.releaseBuffer();
        recordsInCurrentBlob = 0;
    }
    // optionalIndexedRecord is the first message in this case.
    if (datumWriter == null) {
        if (optionalIndexedRecord.isPresent()) {
            IndexedRecord record = optionalIndexedRecord.get();
            schema = record.getSchema();
            if (record instanceof SpecificRecord) {
                datumWriter = new SpecificDatumWriter<>(schema);
            } else {
                datumWriter = new GenericDatumWriter<>(schema);
            }
        } else {
            throw new IllegalStateException("Writing without schema setup.");
        }
    }
    String blobURL;
    if (useRandomStringInBlobName) {
        blobURL = String.format(BLOB_NAME_RANDOM_STRING_AVRO, blobURLPrefix, UTC_FORMATTER.format(System.currentTimeMillis()), UUID.randomUUID().toString().substring(0, 8), compression.getFileExtension());
    } else {
        blobURL = String.format(BLOB_NAME_AVRO, blobURLPrefix, UTC_FORMATTER.format(System.currentTimeMillis()), compression.getFileExtension());
    }
    LOG.info("Creating new blob: {}", blobURL);
    BlockBlobAsyncClient blockBlobAsyncClient = containerAsyncClient.getBlobAsyncClient(blobURL).getBlockBlobAsyncClient();
    DataFileWriter<IndexedRecord> dataFileWriter = new DataFileWriter<>(datumWriter);
    AzureBlobOutputStream azureBlobOutputStream;
    try {
        azureBlobOutputStream = new AzureBlobOutputStream(blockBlobAsyncClient, blobThreadPool, metrics, blobMetadataGeneratorFactory, blobMetadataGeneratorConfig, streamName, flushTimeoutMs, maxBlockFlushThresholdSize, compression);
    } catch (Exception e) {
        throw new SamzaException("Unable to create AzureBlobOutputStream", e);
    }
    dataFileWriter.create(schema, azureBlobOutputStream);
    dataFileWriter.setFlushOnEveryBlock(false);
    this.currentBlobWriterComponents = new BlobWriterComponents(dataFileWriter, azureBlobOutputStream, blockBlobAsyncClient);
    allBlobWriterComponents.add(this.currentBlobWriterComponents);
    LOG.info("Created new blob: {}", blobURL);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) DataFileWriter(org.apache.avro.file.DataFileWriter) SamzaException(org.apache.samza.SamzaException) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException) SpecificRecord(org.apache.avro.specific.SpecificRecord) BlockBlobAsyncClient(com.azure.storage.blob.specialized.BlockBlobAsyncClient)

Aggregations

SpecificRecord (org.apache.avro.specific.SpecificRecord)16 IOException (java.io.IOException)7 Row (org.apache.flink.types.Row)7 Schema (org.apache.avro.Schema)6 Test (org.junit.Test)6 BinaryEncoder (org.apache.avro.io.BinaryEncoder)4 IndexedRecord (org.apache.avro.generic.IndexedRecord)3 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)3 AvroException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException)2 AvroRetryableException (com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroRetryableException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 EncoderFactory (org.apache.avro.io.EncoderFactory)2 JsonEncoder (org.apache.avro.io.JsonEncoder)2 SamzaException (org.apache.samza.SamzaException)2 AppendableOutputStream (org.spf4j.io.AppendableOutputStream)2 BlockBlobAsyncClient (com.azure.storage.blob.specialized.BlockBlobAsyncClient)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)1 SchemaId (com.linkedin.databus2.schemas.SchemaId)1 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)1