use of org.apache.samza.serializers.KVSerde in project samza by apache.
the class TestPartitionByOperatorSpec method testPartitionBy.
@Test
public void testPartitionBy() {
MapFunction<Object, String> keyFn = m -> m.toString();
MapFunction<Object, Object> valueFn = m -> m;
KVSerde<Object, Object> partitionBySerde = KVSerde.of(new NoOpSerde<>(), new NoOpSerde<>());
StreamApplicationDescriptorImpl streamAppDesc = new StreamApplicationDescriptorImpl(appDesc -> {
MessageStream inputStream = appDesc.getInputStream(testInputDescriptor);
inputStream.partitionBy(keyFn, valueFn, partitionBySerde, testRepartitionedStreamName);
}, getConfig());
assertEquals(2, streamAppDesc.getInputOperators().size());
Map<String, InputOperatorSpec> inputOpSpecs = streamAppDesc.getInputOperators();
assertTrue(inputOpSpecs.keySet().contains(String.format("%s-%s-partition_by-%s", testJobName, testJobId, testRepartitionedStreamName)));
InputOperatorSpec inputOpSpec = inputOpSpecs.get(String.format("%s-%s-partition_by-%s", testJobName, testJobId, testRepartitionedStreamName));
assertEquals(String.format("%s-%s-partition_by-%s", testJobName, testJobId, testRepartitionedStreamName), inputOpSpec.getStreamId());
assertTrue(inputOpSpec.getKeySerde() instanceof NoOpSerde);
assertTrue(inputOpSpec.getValueSerde() instanceof NoOpSerde);
assertTrue(inputOpSpec.isKeyed());
assertNull(inputOpSpec.getScheduledFn());
assertNull(inputOpSpec.getWatermarkFn());
InputOperatorSpec originInputSpec = inputOpSpecs.get(testInputDescriptor.getStreamId());
assertTrue(originInputSpec.getRegisteredOperatorSpecs().toArray()[0] instanceof PartitionByOperatorSpec);
PartitionByOperatorSpec reparOpSpec = (PartitionByOperatorSpec) originInputSpec.getRegisteredOperatorSpecs().toArray()[0];
assertEquals(reparOpSpec.getOpId(), String.format("%s-%s-partition_by-%s", testJobName, testJobId, testRepartitionedStreamName));
assertEquals(reparOpSpec.getKeyFunction(), keyFn);
assertEquals(reparOpSpec.getValueFunction(), valueFn);
assertEquals(reparOpSpec.getOutputStream().getStreamId(), reparOpSpec.getOpId());
assertNull(reparOpSpec.getScheduledFn());
assertNull(reparOpSpec.getWatermarkFn());
}
use of org.apache.samza.serializers.KVSerde in project samza by apache.
the class ApplicationDescriptorImpl method getOrCreateTableSerdes.
KV<Serde, Serde> getOrCreateTableSerdes(String tableId, KVSerde kvSerde) {
Serde keySerde, valueSerde;
keySerde = kvSerde.getKeySerde();
valueSerde = kvSerde.getValueSerde();
if (!tableSerdes.containsKey(tableId)) {
tableSerdes.put(tableId, KV.of(keySerde, valueSerde));
return tableSerdes.get(tableId);
}
KV<Serde, Serde> currentSerdePair = tableSerdes.get(tableId);
if (!currentSerdePair.getKey().equals(keySerde) || !currentSerdePair.getValue().equals(valueSerde)) {
throw new IllegalArgumentException(String.format("Serde for table %s is already defined. Cannot change it to " + "different serdes.", tableId));
}
return streamSerdes.get(tableId);
}
use of org.apache.samza.serializers.KVSerde in project samza by apache.
the class StreamApplicationDescriptorImpl method getInputStream.
@Override
public <M> MessageStream<M> getInputStream(InputDescriptor<M, ?> inputDescriptor) {
SystemDescriptor systemDescriptor = inputDescriptor.getSystemDescriptor();
Optional<StreamExpander> expander = systemDescriptor.getExpander();
if (expander.isPresent()) {
return expander.get().apply(this, inputDescriptor);
}
// TODO: SAMZA-1841: need to add to the broadcast streams if inputDescriptor is for a broadcast stream
addInputDescriptor(inputDescriptor);
String streamId = inputDescriptor.getStreamId();
Serde serde = inputDescriptor.getSerde();
KV<Serde, Serde> kvSerdes = getOrCreateStreamSerdes(streamId, serde);
boolean isKeyed = serde instanceof KVSerde;
InputTransformer transformer = inputDescriptor.getTransformer().orElse(null);
InputOperatorSpec inputOperatorSpec = OperatorSpecs.createInputOperatorSpec(streamId, kvSerdes.getKey(), kvSerdes.getValue(), transformer, isKeyed, this.getNextOpId(OpCode.INPUT, null));
inputOperators.put(streamId, inputOperatorSpec);
return new MessageStreamImpl(this, inputOperators.get(streamId));
}
use of org.apache.samza.serializers.KVSerde in project samza by apache.
the class StreamApplicationDescriptorImpl method getOutputStream.
@Override
public <M> OutputStream<M> getOutputStream(OutputDescriptor<M, ?> outputDescriptor) {
addOutputDescriptor(outputDescriptor);
String streamId = outputDescriptor.getStreamId();
Serde serde = outputDescriptor.getSerde();
KV<Serde, Serde> kvSerdes = getOrCreateStreamSerdes(streamId, serde);
boolean isKeyed = serde instanceof KVSerde;
outputStreams.put(streamId, new OutputStreamImpl<>(streamId, kvSerdes.getKey(), kvSerdes.getValue(), isKeyed));
return outputStreams.get(streamId);
}
use of org.apache.samza.serializers.KVSerde in project samza by apache.
the class StreamApplicationDescriptorImpl method getIntermediateStream.
/**
* Internal helper for {@link MessageStreamImpl} to add an intermediate {@link MessageStream} to the graph.
* An intermediate {@link MessageStream} is both an output and an input stream.
*
* @param streamId the id of the stream to be created.
* @param serde the {@link Serde} to use for the message in the intermediate stream. If null, the default serde
* is used.
* @param isBroadcast whether the stream is a broadcast stream.
* @param <M> the type of messages in the intermediate {@link MessageStream}
* @return the intermediate {@link MessageStreamImpl}
*/
@VisibleForTesting
public <M> IntermediateMessageStreamImpl<M> getIntermediateStream(String streamId, Serde<M> serde, boolean isBroadcast) {
Preconditions.checkNotNull(serde, "serde must not be null for intermediate stream: " + streamId);
Preconditions.checkState(!inputOperators.containsKey(streamId) && !outputStreams.containsKey(streamId), "getIntermediateStream must not be called multiple times with the same streamId: " + streamId);
if (isBroadcast) {
intermediateBroadcastStreamIds.add(streamId);
}
boolean isKeyed = serde instanceof KVSerde;
KV<Serde, Serde> kvSerdes = getOrCreateStreamSerdes(streamId, serde);
InputTransformer transformer = (InputTransformer) getDefaultSystemDescriptor().flatMap(SystemDescriptor::getTransformer).orElse(null);
InputOperatorSpec inputOperatorSpec = OperatorSpecs.createInputOperatorSpec(streamId, kvSerdes.getKey(), kvSerdes.getValue(), transformer, isKeyed, this.getNextOpId(OpCode.INPUT, null));
inputOperators.put(streamId, inputOperatorSpec);
outputStreams.put(streamId, new OutputStreamImpl(streamId, kvSerdes.getKey(), kvSerdes.getValue(), isKeyed));
return new IntermediateMessageStreamImpl<>(this, inputOperators.get(streamId), outputStreams.get(streamId));
}
Aggregations