Search in sources :

Example 1 with StreamEventDecoder

use of co.cask.cdap.api.stream.StreamEventDecoder in project cdap by caskdata.

the class StreamInputFormatTest method testStringStreamEventDecoder.

@Test
public void testStringStreamEventDecoder() {
    String body = "Testing";
    StreamEvent event = new StreamEvent(ImmutableMap.<String, String>of(), Charsets.UTF_8.encode(body));
    StreamEventDecoder<LongWritable, String> decoder = new StringStreamEventDecoder();
    StreamEventDecoder.DecodeResult<LongWritable, String> result = new StreamEventDecoder.DecodeResult<>();
    result = decoder.decode(event, result);
    Assert.assertEquals(event.getTimestamp(), result.getKey().get());
    Assert.assertEquals(body, result.getValue());
}
Also used : IdentityStreamEventDecoder(co.cask.cdap.data.stream.decoder.IdentityStreamEventDecoder) StringStreamEventDecoder(co.cask.cdap.data.stream.decoder.StringStreamEventDecoder) StreamEventDecoder(co.cask.cdap.api.stream.StreamEventDecoder) BytesStreamEventDecoder(co.cask.cdap.data.stream.decoder.BytesStreamEventDecoder) TextStreamEventDecoder(co.cask.cdap.data.stream.decoder.TextStreamEventDecoder) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) LongWritable(org.apache.hadoop.io.LongWritable) StringStreamEventDecoder(co.cask.cdap.data.stream.decoder.StringStreamEventDecoder) Test(org.junit.Test)

Example 2 with StreamEventDecoder

use of co.cask.cdap.api.stream.StreamEventDecoder in project cdap by caskdata.

the class AbstractStreamInputFormat method createStreamEventDecoder.

@SuppressWarnings("unchecked")
protected StreamEventDecoder<K, V> createStreamEventDecoder(Configuration conf) {
    Class<? extends StreamEventDecoder> decoderClass = getDecoderClass(conf);
    Preconditions.checkNotNull(decoderClass, "Failed to load stream event decoder %s", conf.get(DECODER_TYPE));
    try {
        // to format the stream body.
        if (decoderClass.isAssignableFrom(FormatStreamEventDecoder.class)) {
            try {
                RecordFormat<StreamEvent, V> bodyFormat = getInitializedFormat(conf);
                return (StreamEventDecoder<K, V>) new FormatStreamEventDecoder(bodyFormat);
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to get the stream body format.");
            }
        } else {
            return (StreamEventDecoder<K, V>) decoderClass.newInstance();
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : IdentityStreamEventDecoder(co.cask.cdap.data.stream.decoder.IdentityStreamEventDecoder) StringStreamEventDecoder(co.cask.cdap.data.stream.decoder.StringStreamEventDecoder) StreamEventDecoder(co.cask.cdap.api.stream.StreamEventDecoder) BytesStreamEventDecoder(co.cask.cdap.data.stream.decoder.BytesStreamEventDecoder) TextStreamEventDecoder(co.cask.cdap.data.stream.decoder.TextStreamEventDecoder) FormatStreamEventDecoder(co.cask.cdap.data.stream.decoder.FormatStreamEventDecoder) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) FormatStreamEventDecoder(co.cask.cdap.data.stream.decoder.FormatStreamEventDecoder)

Example 3 with StreamEventDecoder

use of co.cask.cdap.api.stream.StreamEventDecoder in project cdap by caskdata.

the class SparkBatchSourceFactory method createInputRDD.

@SuppressWarnings("unchecked")
private <K, V> JavaPairRDD<K, V> createInputRDD(JavaSparkExecutionContext sec, JavaSparkContext jsc, String inputName, Class<K> keyClass, Class<V> valueClass) {
    if (streams.containsKey(inputName)) {
        Input.StreamInput streamInput = streams.get(inputName);
        FormatSpecification formatSpec = streamInput.getBodyFormatSpec();
        if (formatSpec != null) {
            return (JavaPairRDD<K, V>) sec.fromStream(streamInput.getName(), formatSpec, streamInput.getStartTime(), streamInput.getEndTime(), StructuredRecord.class);
        }
        String decoderType = streamInput.getDecoderType();
        if (decoderType == null) {
            return (JavaPairRDD<K, V>) sec.fromStream(streamInput.getName(), streamInput.getStartTime(), streamInput.getEndTime(), valueClass);
        } else {
            try {
                Class<StreamEventDecoder<K, V>> decoderClass = (Class<StreamEventDecoder<K, V>>) Thread.currentThread().getContextClassLoader().loadClass(decoderType);
                return sec.fromStream(streamInput.getName(), streamInput.getStartTime(), streamInput.getEndTime(), decoderClass, keyClass, valueClass);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    }
    if (inputFormatProviders.containsKey(inputName)) {
        InputFormatProvider inputFormatProvider = inputFormatProviders.get(inputName);
        Configuration hConf = new Configuration();
        hConf.clear();
        for (Map.Entry<String, String> entry : inputFormatProvider.getInputFormatConfiguration().entrySet()) {
            hConf.set(entry.getKey(), entry.getValue());
        }
        ClassLoader classLoader = Objects.firstNonNull(currentThread().getContextClassLoader(), getClass().getClassLoader());
        try {
            @SuppressWarnings("unchecked") Class<InputFormat> inputFormatClass = (Class<InputFormat>) classLoader.loadClass(inputFormatProvider.getInputFormatClassName());
            return jsc.newAPIHadoopRDD(hConf, inputFormatClass, keyClass, valueClass);
        } catch (ClassNotFoundException e) {
            throw Throwables.propagate(e);
        }
    }
    if (datasetInfos.containsKey(inputName)) {
        DatasetInfo datasetInfo = datasetInfos.get(inputName);
        return sec.fromDataset(datasetInfo.getDatasetName(), datasetInfo.getDatasetArgs());
    }
    // which make sure one and only one of those source type will be specified.
    throw new IllegalStateException("Unknown source type");
}
Also used : InputFormatProvider(co.cask.cdap.api.data.batch.InputFormatProvider) Configuration(org.apache.hadoop.conf.Configuration) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) StructuredRecord(co.cask.cdap.api.data.format.StructuredRecord) StreamEventDecoder(co.cask.cdap.api.stream.StreamEventDecoder) Input(co.cask.cdap.api.data.batch.Input) InputFormat(org.apache.hadoop.mapreduce.InputFormat) JavaPairRDD(org.apache.spark.api.java.JavaPairRDD) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with StreamEventDecoder

use of co.cask.cdap.api.stream.StreamEventDecoder in project cdap by caskdata.

the class StreamInputFormatTest method testIdentityStreamEventDecoder.

@Test
public void testIdentityStreamEventDecoder() {
    ImmutableMap.Builder<String, String> headers = ImmutableMap.builder();
    headers.put("key1", "value1");
    headers.put("key2", "value2");
    ByteBuffer buffer = Charsets.UTF_8.encode("testdata");
    StreamEvent event = new StreamEvent(headers.build(), buffer, System.currentTimeMillis());
    StreamEventDecoder<LongWritable, StreamEvent> decoder = new IdentityStreamEventDecoder();
    StreamEventDecoder.DecodeResult<LongWritable, StreamEvent> result = new StreamEventDecoder.DecodeResult<>();
    result = decoder.decode(event, result);
    Assert.assertEquals(new LongWritable(event.getTimestamp()), result.getKey());
    Assert.assertEquals(event, result.getValue());
}
Also used : IdentityStreamEventDecoder(co.cask.cdap.data.stream.decoder.IdentityStreamEventDecoder) StringStreamEventDecoder(co.cask.cdap.data.stream.decoder.StringStreamEventDecoder) StreamEventDecoder(co.cask.cdap.api.stream.StreamEventDecoder) BytesStreamEventDecoder(co.cask.cdap.data.stream.decoder.BytesStreamEventDecoder) TextStreamEventDecoder(co.cask.cdap.data.stream.decoder.TextStreamEventDecoder) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) IdentityStreamEventDecoder(co.cask.cdap.data.stream.decoder.IdentityStreamEventDecoder) LongWritable(org.apache.hadoop.io.LongWritable) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

StreamEventDecoder (co.cask.cdap.api.stream.StreamEventDecoder)4 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)3 BytesStreamEventDecoder (co.cask.cdap.data.stream.decoder.BytesStreamEventDecoder)3 IdentityStreamEventDecoder (co.cask.cdap.data.stream.decoder.IdentityStreamEventDecoder)3 StringStreamEventDecoder (co.cask.cdap.data.stream.decoder.StringStreamEventDecoder)3 TextStreamEventDecoder (co.cask.cdap.data.stream.decoder.TextStreamEventDecoder)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 LongWritable (org.apache.hadoop.io.LongWritable)2 Test (org.junit.Test)2 Input (co.cask.cdap.api.data.batch.Input)1 InputFormatProvider (co.cask.cdap.api.data.batch.InputFormatProvider)1 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)1 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)1 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 FormatStreamEventDecoder (co.cask.cdap.data.stream.decoder.FormatStreamEventDecoder)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Configuration (org.apache.hadoop.conf.Configuration)1