Search in sources :

Example 36 with StringSerde

use of org.apache.samza.serializers.StringSerde in project samza by apache.

the class RepartitionExample method describe.

@Override
public void describe(StreamApplicationDescriptor appDescriptor) {
    KafkaSystemDescriptor trackingSystem = new KafkaSystemDescriptor("tracking");
    KafkaInputDescriptor<PageViewEvent> inputStreamDescriptor = trackingSystem.getInputDescriptor("pageViewEvent", new JsonSerdeV2<>(PageViewEvent.class));
    KafkaOutputDescriptor<KV<String, MyStreamOutput>> outputStreamDescriptor = trackingSystem.getOutputDescriptor("pageViewEventPerMember", KVSerde.of(new StringSerde(), new JsonSerdeV2<>(MyStreamOutput.class)));
    appDescriptor.withDefaultSystem(trackingSystem);
    MessageStream<PageViewEvent> pageViewEvents = appDescriptor.getInputStream(inputStreamDescriptor);
    OutputStream<KV<String, MyStreamOutput>> pageViewEventPerMember = appDescriptor.getOutputStream(outputStreamDescriptor);
    pageViewEvents.partitionBy(pve -> pve.getMemberId(), pve -> pve, KVSerde.of(new StringSerde(), new JsonSerdeV2<>(PageViewEvent.class)), "partitionBy").window(Windows.keyedTumblingWindow(KV::getKey, Duration.ofMinutes(5), () -> 0, (m, c) -> c + 1, null, null), "window").map(windowPane -> KV.of(windowPane.getKey().getKey(), new MyStreamOutput(windowPane))).sendTo(pageViewEventPerMember);
}
Also used : ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) Windows(org.apache.samza.operators.windows.Windows) KafkaInputDescriptor(org.apache.samza.system.kafka.descriptors.KafkaInputDescriptor) CommandLine(org.apache.samza.util.CommandLine) KafkaSystemDescriptor(org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor) PageViewEvent(org.apache.samza.example.models.PageViewEvent) WindowPane(org.apache.samza.operators.windows.WindowPane) StringSerde(org.apache.samza.serializers.StringSerde) KafkaOutputDescriptor(org.apache.samza.system.kafka.descriptors.KafkaOutputDescriptor) StreamApplicationDescriptor(org.apache.samza.application.descriptors.StreamApplicationDescriptor) Duration(java.time.Duration) Config(org.apache.samza.config.Config) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) JsonSerdeV2(org.apache.samza.serializers.JsonSerdeV2) KVSerde(org.apache.samza.serializers.KVSerde) StreamApplication(org.apache.samza.application.StreamApplication) KV(org.apache.samza.operators.KV) OutputStream(org.apache.samza.operators.OutputStream) MessageStream(org.apache.samza.operators.MessageStream) StringSerde(org.apache.samza.serializers.StringSerde) PageViewEvent(org.apache.samza.example.models.PageViewEvent) KV(org.apache.samza.operators.KV) KafkaSystemDescriptor(org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor) JsonSerdeV2(org.apache.samza.serializers.JsonSerdeV2)

Example 37 with StringSerde

use of org.apache.samza.serializers.StringSerde in project samza by apache.

the class AsyncApplicationExample method describe.

@Override
public void describe(StreamApplicationDescriptor appDescriptor) {
    KafkaSystemDescriptor trackingSystem = new KafkaSystemDescriptor("tracking");
    KafkaInputDescriptor<AdClickEvent> inputStreamDescriptor = trackingSystem.getInputDescriptor("adClickEvent", new JsonSerdeV2<>(AdClickEvent.class));
    KafkaOutputDescriptor<KV<String, EnrichedAdClickEvent>> outputStreamDescriptor = trackingSystem.getOutputDescriptor("enrichedAdClickEvent", KVSerde.of(new StringSerde(), new JsonSerdeV2<>(EnrichedAdClickEvent.class)));
    MessageStream<AdClickEvent> adClickEventStream = appDescriptor.getInputStream(inputStreamDescriptor);
    OutputStream<KV<String, EnrichedAdClickEvent>> enrichedAdClickStream = appDescriptor.getOutputStream(outputStreamDescriptor);
    adClickEventStream.flatMapAsync(AsyncApplicationExample::enrichAdClickEvent).map(enrichedAdClickEvent -> KV.of(enrichedAdClickEvent.getCountry(), enrichedAdClickEvent)).sendTo(enrichedAdClickStream);
}
Also used : CommandLine(org.apache.samza.util.CommandLine) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) StringSerde(org.apache.samza.serializers.StringSerde) AdClickEvent(org.apache.samza.example.models.AdClickEvent) KafkaOutputDescriptor(org.apache.samza.system.kafka.descriptors.KafkaOutputDescriptor) ImmutableList(com.google.common.collect.ImmutableList) EnrichedAdClickEvent(org.apache.samza.example.models.EnrichedAdClickEvent) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) KV(org.apache.samza.operators.KV) MessageStream(org.apache.samza.operators.MessageStream) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) KafkaInputDescriptor(org.apache.samza.system.kafka.descriptors.KafkaInputDescriptor) Collection(java.util.Collection) Member(org.apache.samza.example.models.Member) KafkaSystemDescriptor(org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) StreamApplicationDescriptor(org.apache.samza.application.descriptors.StreamApplicationDescriptor) Config(org.apache.samza.config.Config) JsonSerdeV2(org.apache.samza.serializers.JsonSerdeV2) KVSerde(org.apache.samza.serializers.KVSerde) StreamApplication(org.apache.samza.application.StreamApplication) Collections(java.util.Collections) OutputStream(org.apache.samza.operators.OutputStream) AdClickEvent(org.apache.samza.example.models.AdClickEvent) EnrichedAdClickEvent(org.apache.samza.example.models.EnrichedAdClickEvent) StringSerde(org.apache.samza.serializers.StringSerde) KV(org.apache.samza.operators.KV) KafkaSystemDescriptor(org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor) JsonSerdeV2(org.apache.samza.serializers.JsonSerdeV2)

Example 38 with StringSerde

use of org.apache.samza.serializers.StringSerde in project samza by apache.

the class TestTimeSeriesKeySerde method testStringTimeSeriesKey.

@Test
public void testStringTimeSeriesKey() {
    TimeSeriesKey<String> storeKey = new TimeSeriesKey<>("test", 1, 23);
    TimeSeriesKeySerde<String> serde = new TimeSeriesKeySerde<>(new StringSerde("UTF-8"));
    byte[] serializedBytes = serde.toBytes(storeKey);
    TimeSeriesKey<String> deserializedTimeSeriesKey = serde.fromBytes(serializedBytes);
    assertEquals(storeKey.getKey(), deserializedTimeSeriesKey.getKey());
    assertEquals(storeKey.getSeqNum(), deserializedTimeSeriesKey.getSeqNum());
    assertEquals(storeKey.getTimestamp(), deserializedTimeSeriesKey.getTimestamp());
    assertEquals(storeKey, deserializedTimeSeriesKey);
}
Also used : StringSerde(org.apache.samza.serializers.StringSerde) Test(org.junit.Test)

Example 39 with StringSerde

use of org.apache.samza.serializers.StringSerde in project samza by apache.

the class TestOperatorSpec method testOutputOperatorSpec.

@Test
public void testOutputOperatorSpec() {
    Serde<Object> objSerde = new Serde<Object>() {

        @Override
        public Object fromBytes(byte[] bytes) {
            return null;
        }

        @Override
        public byte[] toBytes(Object object) {
            return new byte[0];
        }
    };
    OutputStreamImpl<KV<String, Object>> outputStrmImpl = new OutputStreamImpl<>("mockStreamId", new StringSerde("UTF-8"), objSerde, true);
    OutputOperatorSpec<KV<String, Object>> outputOperatorSpec = new OutputOperatorSpec<>(outputStrmImpl, "op0");
    OutputOperatorSpec<KV<String, Object>> outputOpCopy = (OutputOperatorSpec<KV<String, Object>>) OperatorSpecTestUtils.copyOpSpec(outputOperatorSpec);
    assertNotEquals("Expected deserialized copy of operator spec should not be the same as the original operator spec", outputOperatorSpec, outputOpCopy);
    assertTrue(outputOperatorSpec.isClone(outputOpCopy));
}
Also used : Serde(org.apache.samza.serializers.Serde) StringSerde(org.apache.samza.serializers.StringSerde) NoOpSerde(org.apache.samza.serializers.NoOpSerde) StringSerde(org.apache.samza.serializers.StringSerde) KV(org.apache.samza.operators.KV) Test(org.junit.Test)

Example 40 with StringSerde

use of org.apache.samza.serializers.StringSerde in project samza by apache.

the class TestOperatorSpec method testBroadcastOperatorSpec.

@Test
public void testBroadcastOperatorSpec() {
    OutputStreamImpl<TestOutputMessageEnvelope> outputStream = new OutputStreamImpl<>("output-0", new StringSerde("UTF-8"), new JsonSerdeV2<TestOutputMessageEnvelope>(), true);
    BroadcastOperatorSpec<TestOutputMessageEnvelope> broadcastOpSpec = new BroadcastOperatorSpec<>(outputStream, "broadcast-1");
    BroadcastOperatorSpec<TestOutputMessageEnvelope> broadcastOpCopy = (BroadcastOperatorSpec<TestOutputMessageEnvelope>) OperatorSpecTestUtils.copyOpSpec(broadcastOpSpec);
    assertNotEquals(broadcastOpCopy, broadcastOpSpec);
    assertEquals(broadcastOpCopy.getOpId(), broadcastOpSpec.getOpId());
    assertTrue(broadcastOpCopy.getOutputStream() != broadcastOpSpec.getOutputStream());
    assertEquals(broadcastOpCopy.getOutputStream().getStreamId(), broadcastOpSpec.getOutputStream().getStreamId());
    assertEquals(broadcastOpCopy.getOutputStream().isKeyed(), broadcastOpSpec.getOutputStream().isKeyed());
}
Also used : StringSerde(org.apache.samza.serializers.StringSerde) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) Test(org.junit.Test)

Aggregations

StringSerde (org.apache.samza.serializers.StringSerde)52 Test (org.junit.Test)32 KV (org.apache.samza.operators.KV)25 KVSerde (org.apache.samza.serializers.KVSerde)19 KafkaSystemDescriptor (org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor)14 Config (org.apache.samza.config.Config)13 JsonSerdeV2 (org.apache.samza.serializers.JsonSerdeV2)13 StreamApplication (org.apache.samza.application.StreamApplication)11 Duration (java.time.Duration)10 StreamApplicationDescriptor (org.apache.samza.application.descriptors.StreamApplicationDescriptor)10 MessageStream (org.apache.samza.operators.MessageStream)10 KafkaInputDescriptor (org.apache.samza.system.kafka.descriptors.KafkaInputDescriptor)10 ApplicationRunner (org.apache.samza.runtime.ApplicationRunner)9 ApplicationRunners (org.apache.samza.runtime.ApplicationRunners)9 NoOpSerde (org.apache.samza.serializers.NoOpSerde)9 KafkaOutputDescriptor (org.apache.samza.system.kafka.descriptors.KafkaOutputDescriptor)9 TableDescriptor (org.apache.samza.table.descriptors.TableDescriptor)9 CommandLine (org.apache.samza.util.CommandLine)9 OutputStream (org.apache.samza.operators.OutputStream)8 Windows (org.apache.samza.operators.windows.Windows)8