Search in sources :

Example 11 with ObjectMapperSerde

use of io.quarkus.kafka.client.serialization.ObjectMapperSerde in project AD482-apps by RedHatTraining.

the class VehicleMovementTracker method buildTopology.

@Produces
public Topology buildTopology() {
    StreamsBuilder builder = new StreamsBuilder();
    // Event Key SerDe (all events use the vehicle id as the Kafka record key)
    Serde<Integer> intSerde = Serdes.Integer();
    // Event Value SerDes
    ObjectMapperSerde<Vehicle> vehicleSerde = new ObjectMapperSerde<>(Vehicle.class);
    ObjectMapperSerde<VehicleMetrics> vehicleMetricsSerde = new ObjectMapperSerde<>(VehicleMetrics.class);
    ObjectMapperSerde<VehicleMoved> vehicleMovedSerde = new ObjectMapperSerde<>(VehicleMoved.class);
    ObjectMapperSerde<VehicleStatus> vehicleStatusSerde = new ObjectMapperSerde<>(VehicleStatus.class);
    GlobalKTable<Integer, Vehicle> vehiclesTable = builder.globalTable("vehicle-registered", Materialized.<Integer, Vehicle, KeyValueStore<Bytes, byte[]>>as("vehicles-store").withKeySerde(intSerde).withValueSerde(vehicleSerde));
    KStream<Integer, VehicleMoved> movementsStream = builder.stream("vehicle-moved", Consumed.with(intSerde, vehicleMovedSerde));
    KStream<Integer, VehicleStatus> vehicleStatusStream = movementsStream.join(vehiclesTable, (vehicleId, vehicleMoved) -> vehicleId, (vehicleMoved, vehicle) -> new VehicleStatus(vehicle, vehicleMoved.latitude, vehicleMoved.longitude, vehicleMoved.elevation));
    // TODO: materialize vehicleStatusStream to the "vehicle-status" topic
    vehicleStatusStream.to("vehicle-status", Produced.with(intSerde, vehicleStatusSerde));
    vehicleStatusStream.groupByKey().aggregate(VehicleMetrics::new, (vehicleId, vehicleStatus, vehicleMetrics) -> vehicleMetrics.update(vehicleStatus), Materialized.<Integer, VehicleMetrics, KeyValueStore<Bytes, byte[]>>as("vehicle-metrics-store").withKeySerde(intSerde).withValueSerde(vehicleMetricsSerde));
    return builder.build();
}
Also used : VehicleMoved(com.redhat.vehicles.events.VehicleMoved) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Vehicle(com.redhat.vehicles.inventory.Vehicle) Bytes(org.apache.kafka.common.utils.Bytes) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) Produces(javax.enterprise.inject.Produces)

Example 12 with ObjectMapperSerde

use of io.quarkus.kafka.client.serialization.ObjectMapperSerde in project AD482-apps by RedHatTraining.

the class AmountWasWithdrawnPipeline method onStart.

void onStart(@Observes StartupEvent startupEvent) {
    StreamsBuilder builder = new StreamsBuilder();
    ObjectMapperSerde<AmountWasWithdrawn> withdrawalEventSerde = new ObjectMapperSerde<>(AmountWasWithdrawn.class);
    lowRiskEventSerde = new ObjectMapperSerde<>(LowRiskWithdrawnWasDetected.class);
    moderateRiskEventSerde = new ObjectMapperSerde<>(ModerateRiskWithdrawnWasDetected.class);
    highRiskEventSerde = new ObjectMapperSerde<>(HighRiskWithdrawnWasDetected.class);
    // TODO: Add inverse filter
    KStream<Long, AmountWasWithdrawn> mainStream = builder.stream(AMOUNT_WAS_WITHDRAWN_TOPIC, Consumed.with(Serdes.Long(), withdrawalEventSerde)).filterNot((key, withdrawal) -> withdrawal.amount <= 50);
    // TODO: Split the stream
    mainStream.split().branch((key, withdrawal) -> withdrawal.amount > 50 && withdrawal.amount <= 1000, Branched.withConsumer(this::processLowAmountEvents)).branch((key, withdrawal) -> withdrawal.amount > 1000 && withdrawal.amount <= 3000, Branched.withConsumer(this::processModerateAmountEvents)).branch((key, withdrawal) -> true, Branched.withConsumer(this::processHighAmountEvents));
    // TODO: Create a Kafka streams and start it
    streams = new KafkaStreams(builder.build(), generateStreamConfig());
    streams.start();
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) AmountWasWithdrawn(com.redhat.training.bank.event.AmountWasWithdrawn) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) AmountWasWithdrawn(com.redhat.training.bank.event.AmountWasWithdrawn) HighRiskWithdrawnWasDetected(com.redhat.training.bank.event.HighRiskWithdrawnWasDetected) org.apache.kafka.streams.kstream(org.apache.kafka.streams.kstream) Logger(org.jboss.logging.Logger) KeyValue(org.apache.kafka.streams.KeyValue) ShutdownEvent(io.quarkus.runtime.ShutdownEvent) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) Observes(javax.enterprise.event.Observes) Serdes(org.apache.kafka.common.serialization.Serdes) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ModerateRiskWithdrawnWasDetected(com.redhat.training.bank.event.ModerateRiskWithdrawnWasDetected) StartupEvent(io.quarkus.runtime.StartupEvent) KafkaStreams(org.apache.kafka.streams.KafkaStreams) LowRiskWithdrawnWasDetected(com.redhat.training.bank.event.LowRiskWithdrawnWasDetected) KafkaStreams(org.apache.kafka.streams.KafkaStreams) ModerateRiskWithdrawnWasDetected(com.redhat.training.bank.event.ModerateRiskWithdrawnWasDetected) LowRiskWithdrawnWasDetected(com.redhat.training.bank.event.LowRiskWithdrawnWasDetected) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) HighRiskWithdrawnWasDetected(com.redhat.training.bank.event.HighRiskWithdrawnWasDetected)

Example 13 with ObjectMapperSerde

use of io.quarkus.kafka.client.serialization.ObjectMapperSerde in project AD482-apps by RedHatTraining.

the class TransformTemperature method onStart.

void onStart(@Observes StartupEvent startupEvent) {
    StreamsBuilder builder = new StreamsBuilder();
    ObjectMapperSerde<TemperatureWasMeasuredInCelsius> temperaturesEventSerde = new ObjectMapperSerde<>(TemperatureWasMeasuredInCelsius.class);
    ObjectMapperSerde<TemperatureWasTransformed> temperaturesTransformedEventSerde = new ObjectMapperSerde<>(TemperatureWasTransformed.class);
    KStream<String, TemperatureWasMeasuredInCelsius> stream = builder.stream(TEMPERATURES_TOPIC, Consumed.with(Serdes.String(), temperaturesEventSerde));
    stream.map((key, measure) -> {
        LOGGER.infov("Transforming {0}ºC to ºF...", measure.measure);
        // Simulate a slow calculation
        try {
            Thread.sleep(CALCULATION_DELAY);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Double fahrenheit = ((double) measure.measure * 9 / 5) + 32;
        LOGGER.infov("Temp. transformed {0}ºC -> {1}ºF (ID: {2})", measure.measure, fahrenheit, measure.locationId);
        return new KeyValue<>(measure.locationId, new TemperatureWasTransformed(measure.locationId, measure.measure, fahrenheit));
    }).to(MEASURED_TEMPERATURES_TOPIC, Produced.with(Serdes.Integer(), temperaturesTransformedEventSerde));
    streams = new KafkaStreams(builder.build(), generateStreamConfig());
    streams.start();
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Produces(javax.enterprise.inject.Produces) Produced(org.apache.kafka.streams.kstream.Produced) TemperatureWasTransformed(com.redhat.monitor.event.TemperatureWasTransformed) Consumed(org.apache.kafka.streams.kstream.Consumed) Logger(org.jboss.logging.Logger) KeyValue(org.apache.kafka.streams.KeyValue) ShutdownEvent(io.quarkus.runtime.ShutdownEvent) KStream(org.apache.kafka.streams.kstream.KStream) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) TemperatureWasMeasuredInCelsius(com.redhat.monitor.event.TemperatureWasMeasuredInCelsius) Observes(javax.enterprise.event.Observes) Serdes(org.apache.kafka.common.serialization.Serdes) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StartupEvent(io.quarkus.runtime.StartupEvent) KafkaStreams(org.apache.kafka.streams.KafkaStreams) TemperatureWasMeasuredInCelsius(com.redhat.monitor.event.TemperatureWasMeasuredInCelsius) KafkaStreams(org.apache.kafka.streams.KafkaStreams) TemperatureWasTransformed(com.redhat.monitor.event.TemperatureWasTransformed) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde)

Example 14 with ObjectMapperSerde

use of io.quarkus.kafka.client.serialization.ObjectMapperSerde in project quarkus by quarkusio.

the class ObjectMapperSerdeTest method shouldSerializeAndDeserializeEntityWithGivenObjectMapper.

@Test
public void shouldSerializeAndDeserializeEntityWithGivenObjectMapper() throws Exception {
    MyEntity entity = new MyEntity();
    entity.id = 42L;
    entity.name = "Bob";
    ObjectMapper objectMapper = new ObjectMapper();
    try (ObjectMapperSerde<MyEntity> serde = new ObjectMapperSerde<>(MyEntity.class, objectMapper)) {
        byte[] serialized = serde.serializer().serialize("my-topic", entity);
        MyEntity deserialized = serde.deserializer().deserialize("my-topic", serialized);
        assertThat(deserialized.id).isEqualTo(42L);
        assertThat(deserialized.name).isEqualTo("Bob");
    }
}
Also used : ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 15 with ObjectMapperSerde

use of io.quarkus.kafka.client.serialization.ObjectMapperSerde in project quarkus by quarkusio.

the class KafkaStreamsPipeline method buildTopology.

@Produces
public Topology buildTopology() {
    StreamsBuilder builder = new StreamsBuilder();
    ObjectMapperSerde<Category> categorySerde = new ObjectMapperSerde<>(Category.class);
    ObjectMapperSerde<Customer> customerSerde = new ObjectMapperSerde<>(Customer.class);
    ObjectMapperSerde<EnrichedCustomer> enrichedCustomerSerde = new ObjectMapperSerde<>(EnrichedCustomer.class);
    KTable<Integer, Category> categories = builder.table("streams-test-categories", Consumed.with(Serdes.Integer(), categorySerde));
    KStream<Integer, EnrichedCustomer> customers = builder.stream("streams-test-customers", Consumed.with(Serdes.Integer(), customerSerde)).selectKey((id, customer) -> customer.category).join(categories, (customer, category) -> {
        return new EnrichedCustomer(customer.id, customer.name, category);
    }, Joined.with(Serdes.Integer(), customerSerde, categorySerde));
    KeyValueBytesStoreSupplier storeSupplier = Stores.inMemoryKeyValueStore("countstore");
    customers.groupByKey().count(Materialized.<Integer, Long>as(storeSupplier));
    customers.selectKey((categoryId, customer) -> customer.id).to("streams-test-customers-processed", Produced.with(Serdes.Integer(), enrichedCustomerSerde));
    return builder.build();
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KTable(org.apache.kafka.streams.kstream.KTable) KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) Produces(javax.enterprise.inject.Produces) Produced(org.apache.kafka.streams.kstream.Produced) Consumed(org.apache.kafka.streams.kstream.Consumed) Stores(org.apache.kafka.streams.state.Stores) KStream(org.apache.kafka.streams.kstream.KStream) Singleton(javax.inject.Singleton) State(org.apache.kafka.streams.KafkaStreams.State) Joined(org.apache.kafka.streams.kstream.Joined) StateListener(org.apache.kafka.streams.KafkaStreams.StateListener) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) Materialized(org.apache.kafka.streams.kstream.Materialized) Serdes(org.apache.kafka.common.serialization.Serdes) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Topology(org.apache.kafka.streams.Topology) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValueBytesStoreSupplier(org.apache.kafka.streams.state.KeyValueBytesStoreSupplier) ObjectMapperSerde(io.quarkus.kafka.client.serialization.ObjectMapperSerde) Produces(javax.enterprise.inject.Produces)

Aggregations

ObjectMapperSerde (io.quarkus.kafka.client.serialization.ObjectMapperSerde)15 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)13 ApplicationScoped (javax.enterprise.context.ApplicationScoped)8 Serdes (org.apache.kafka.common.serialization.Serdes)8 KafkaStreams (org.apache.kafka.streams.KafkaStreams)8 Produces (javax.enterprise.inject.Produces)7 KeyValue (org.apache.kafka.streams.KeyValue)7 Consumed (org.apache.kafka.streams.kstream.Consumed)7 Produced (org.apache.kafka.streams.kstream.Produced)7 ShutdownEvent (io.quarkus.runtime.ShutdownEvent)5 StartupEvent (io.quarkus.runtime.StartupEvent)5 Observes (javax.enterprise.event.Observes)5 KStream (org.apache.kafka.streams.kstream.KStream)5 Logger (org.jboss.logging.Logger)5 Bytes (org.apache.kafka.common.utils.Bytes)3 Topology (org.apache.kafka.streams.Topology)3 WindTurbineProfitMarginWasCalculated (com.redhat.energy.profit.event.WindTurbineProfitMarginWasCalculated)2 MWattsMeasurement (com.redhat.energy.records.MWattsMeasurement)2 TemperatureWasMeasuredInCelsius (com.redhat.monitor.event.TemperatureWasMeasuredInCelsius)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1