Search in sources :

Example 16 with Encoder

use of org.apache.avro.io.Encoder in project camel by apache.

the class AvroDataFormat method marshal.

public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
    // the schema should be from the graph class name
    Schema useSchema = actualSchema != null ? actualSchema : loadSchema(graph.getClass().getName());
    DatumWriter<Object> datum = new SpecificDatumWriter<Object>(useSchema);
    Encoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
    datum.write(graph, encoder);
    encoder.flush();
}
Also used : Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 17 with Encoder

use of org.apache.avro.io.Encoder in project avro-kafka-storm by ransilberman.

the class MainTest method testGenericRecord.

@Test
public void testGenericRecord() throws IOException, InterruptedException {
    Schema.Parser parser = new Schema.Parser();
    Schema schema = parser.parse(getClass().getResourceAsStream("LPEvent.avsc"));
    GenericRecord datum = new GenericData.Record(schema);
    datum.put("revision", 1L);
    datum.put("siteId", "28280110");
    datum.put("eventType", "PLine");
    datum.put("timeStamp", System.currentTimeMillis());
    datum.put("sessionId", "123456II");
    Map<String, Schema> unions = new HashMap<String, Schema>();
    List<Schema> typeList = schema.getField("subrecord").schema().getTypes();
    for (Schema sch : typeList) {
        unions.put(sch.getName(), sch);
    }
    GenericRecord plineDatum = new GenericData.Record(unions.get("pline"));
    plineDatum.put("text", "How can I help you?");
    plineDatum.put("lineType", 1);
    plineDatum.put("repId", "REPID12345");
    datum.put("subrecord", plineDatum);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
    Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    writer.write(datum, encoder);
    encoder.flush();
    out.close();
    Message message = new Message(out.toByteArray());
    Properties props = new Properties();
    props.put("zk.connect", zkConnection);
    Producer<Message, Message> producer = new kafka.javaapi.producer.Producer<Message, Message>(new ProducerConfig(props));
    producer.send(new ProducerData<Message, Message>(topic, message));
}
Also used : Message(kafka.message.Message) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) Properties(java.util.Properties) Producer(kafka.javaapi.producer.Producer) Encoder(org.apache.avro.io.Encoder) ProducerConfig(kafka.producer.ProducerConfig) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 18 with Encoder

use of org.apache.avro.io.Encoder in project voldemort by voldemort.

the class AvroGenericSerializer method toBytes.

public byte[] toBytes(Object object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(output);
    GenericDatumWriter<Object> datumWriter = null;
    try {
        datumWriter = new GenericDatumWriter<Object>(typeDef);
        datumWriter.write(object, encoder);
        encoder.flush();
    } catch (IOException e) {
        throw new SerializationException(e);
    } finally {
        SerializationUtils.close(output);
    }
    return output.toByteArray();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 19 with Encoder

use of org.apache.avro.io.Encoder in project voldemort by voldemort.

the class AvroSpecificSerializer method toBytes.

public byte[] toBytes(T object) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(output);
    SpecificDatumWriter<T> datumWriter = null;
    try {
        datumWriter = new SpecificDatumWriter<T>(clazz);
        datumWriter.write(object, encoder);
        encoder.flush();
    } catch (IOException e) {
        throw new SerializationException(e);
    } finally {
        SerializationUtils.close(output);
    }
    return output.toByteArray();
}
Also used : SerializationException(voldemort.serialization.SerializationException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) BinaryEncoder(org.apache.avro.io.BinaryEncoder) Encoder(org.apache.avro.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 20 with Encoder

use of org.apache.avro.io.Encoder in project cdap by caskdata.

the class ClientMessagingService method performWriteRequest.

/**
   * Makes a request to the server for writing to the messaging system
   *
   * @param request contains information about what to write
   * @param publish {@code true} to make publish call, {@code false} to make store call.
   * @return the response from the server
   * @throws IOException if failed to perform the write operation
   * @throws TopicNotFoundException if the topic to write to does not exist
   */
private HttpResponse performWriteRequest(StoreRequest request, boolean publish) throws IOException, TopicNotFoundException {
    GenericRecord record = new GenericData.Record(Schemas.V1.PublishRequest.SCHEMA);
    if (request.isTransactional()) {
        record.put("transactionWritePointer", request.getTransactionWritePointer());
    }
    record.put("messages", convertPayloads(request));
    // Encode the request as avro
    ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
    DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
    datumWriter.write(record, encoder);
    // Make the publish request
    String writeType = publish ? "publish" : "store";
    TopicId topicId = request.getTopicId();
    HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(os.toByteBuffer()).build();
    HttpResponse response = remoteClient.execute(httpRequest);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to " + writeType + " message to topic " + topicId);
    return response;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) Encoder(org.apache.avro.io.Encoder) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) GenericRecord(org.apache.avro.generic.GenericRecord) TopicId(co.cask.cdap.proto.id.TopicId) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

Encoder (org.apache.avro.io.Encoder)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)12 GenericRecord (org.apache.avro.generic.GenericRecord)12 IOException (java.io.IOException)10 BinaryEncoder (org.apache.avro.io.BinaryEncoder)10 Schema (org.apache.avro.Schema)7 SerializationException (voldemort.serialization.SerializationException)5 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)3 AvroAdapter (com.linkedin.data.avro.AvroAdapter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Producer (kafka.javaapi.producer.Producer)2 Message (kafka.message.Message)2 ProducerConfig (kafka.producer.ProducerConfig)2 Test (org.junit.Test)2 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)1 TopicId (co.cask.cdap.proto.id.TopicId)1 HttpRequest (co.cask.common.http.HttpRequest)1