Search in sources :

Example 6 with ByteBufferInputStream

use of co.cask.common.io.ByteBufferInputStream in project cdap by caskdata.

the class FlowletProgramRunner method createInputDatumDecoder.

private <T> Function<ByteBuffer, T> createInputDatumDecoder(final TypeToken<T> dataType, final Schema schema, final SchemaCache schemaCache) {
    final ReflectionDatumReader<T> datumReader = new ReflectionDatumReader<>(schema, dataType);
    final ByteBufferInputStream byteBufferInput = new ByteBufferInputStream(null);
    final BinaryDecoder decoder = new BinaryDecoder(byteBufferInput);
    return new Function<ByteBuffer, T>() {

        @Nullable
        @Override
        public T apply(ByteBuffer input) {
            byteBufferInput.reset(input);
            try {
                final Schema sourceSchema = schemaCache.get(input);
                Preconditions.checkNotNull(sourceSchema, "Fail to find source schema.");
                return datumReader.read(decoder, sourceSchema);
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }

        @Override
        public String toString() {
            return Objects.toStringHelper(this).add("dataType", dataType).add("schema", schema).toString();
        }
    };
}
Also used : Function(com.google.common.base.Function) Schema(co.cask.cdap.api.data.schema.Schema) ByteBufferInputStream(co.cask.common.io.ByteBufferInputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Example 7 with ByteBufferInputStream

use of co.cask.common.io.ByteBufferInputStream in project cdap by caskdata.

the class StreamEventCodec method decodePayload.

/**
   * Reverse of {@link #encodePayload(StreamEvent)}.
   *
   * @param payload The byte array containing the queue payload.
   * @return A {@link StreamEvent} reconstructed from payload.
   */
public StreamEvent decodePayload(byte[] payload) {
    ByteBuffer buffer = ByteBuffer.wrap(payload);
    SchemaHash schemaHash = new SchemaHash(buffer);
    Preconditions.checkArgument(schemaHash.equals(STREAM_EVENT_SCHEMA.getSchemaHash()), "Schema from payload not matching StreamEvent schema.");
    Decoder decoder = new BinaryDecoder(new ByteBufferInputStream(buffer));
    try {
        StreamEventData data = StreamEventDataCodec.decode(decoder);
        // Read the timestamp
        long timestamp = decoder.readLong();
        return new StreamEvent(data, timestamp);
    } catch (IOException e) {
        // It should never happens, otherwise something very wrong.
        throw Throwables.propagate(e);
    }
}
Also used : SchemaHash(co.cask.cdap.api.data.schema.SchemaHash) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) ByteBufferInputStream(co.cask.common.io.ByteBufferInputStream) IOException(java.io.IOException) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Decoder(co.cask.cdap.common.io.Decoder) StreamEventData(co.cask.cdap.api.stream.StreamEventData) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Aggregations

ByteBufferInputStream (co.cask.common.io.ByteBufferInputStream)7 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)6 IOException (java.io.IOException)5 ByteBuffer (java.nio.ByteBuffer)4 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)3 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)3 Schema (co.cask.cdap.api.data.schema.Schema)2 MetricValues (co.cask.cdap.api.metrics.MetricValues)2 Decoder (co.cask.cdap.common.io.Decoder)2 Test (org.junit.Test)2 StreamEventData (co.cask.cdap.api.stream.StreamEventData)1 StreamEventCodec (co.cask.cdap.common.stream.StreamEventCodec)1 DequeueResult (co.cask.cdap.data2.queue.DequeueResult)1 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)1 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)1 RawMessage (co.cask.cdap.messaging.data.RawMessage)1 StreamId (co.cask.cdap.proto.id.StreamId)1 TopicId (co.cask.cdap.proto.id.TopicId)1 BodyConsumer (co.cask.http.BodyConsumer)1 Function (com.google.common.base.Function)1