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());
}
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);
}
}
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");
}
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());
}
Aggregations