org.springframework.kafka.support.serializer.JsonSerde Java Examples
The following examples show how to use
org.springframework.kafka.support.serializer.JsonSerde.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScsApplication.java From spring_io_2019 with Apache License 2.0 | 7 votes |
@StreamListener @SendTo(Bindings.RATED_MOVIES) KStream<Long, RatedMovie> rateMoviesFor(@Input(Bindings.AVG_TABLE) KTable<Long, Double> ratings, @Input(Bindings.MOVIES) KTable<Long, Movie> movies) { ValueJoiner<Movie, Double, RatedMovie> joiner = (movie, rating) -> new RatedMovie(movie.getMovieId(), movie.getReleaseYear(), movie.getTitle(), rating); movies .join(ratings, joiner, Materialized .<Long, RatedMovie, KeyValueStore<Bytes, byte[]>>as(Bindings.RATED_MOVIES_STORE) .withKeySerde(Serdes.Long()) .withValueSerde(new JsonSerde<>(RatedMovie.class))); return movies.join(ratings, joiner).toStream(); }
Example #2
Source File: Producers.java From spring-cloud-stream-samples with Apache License 2.0 | 6 votes |
public static void main(String... args) { ObjectMapper mapper = new ObjectMapper(); Serde<DomainEvent> domainEventSerde = new JsonSerde<>(DomainEvent.class, mapper); Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ProducerConfig.RETRIES_CONFIG, 0); props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384); props.put(ProducerConfig.LINGER_MS_CONFIG, 1); props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, domainEventSerde.serializer().getClass()); DomainEvent ddEvent = new DomainEvent(); ddEvent.setBoardUuid("12345"); ddEvent.setEventType("thisisanevent"); DefaultKafkaProducerFactory<String, DomainEvent> pf = new DefaultKafkaProducerFactory<>(props); KafkaTemplate<String, DomainEvent> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("foobar"); template.sendDefault("", ddEvent); }
Example #3
Source File: KafkaStreamsAggregateSample.java From spring-cloud-stream-samples with Apache License 2.0 | 6 votes |
@Bean public Consumer<KStream<String, DomainEvent>> aggregate() { ObjectMapper mapper = new ObjectMapper(); Serde<DomainEvent> domainEventSerde = new JsonSerde<>( DomainEvent.class, mapper ); return input -> input .groupBy( (s, domainEvent) -> domainEvent.boardUuid, Grouped.with(null, domainEventSerde)) .aggregate( String::new, (s, domainEvent, board) -> board.concat(domainEvent.eventType), Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("test-events-snapshots") .withKeySerde(Serdes.String()). withValueSerde(Serdes.String()) ); }
Example #4
Source File: VehicleStatusCountProcessor.java From microservice-patterns with Apache License 2.0 | 5 votes |
@Bean //configure key-value serdes public Map<String, Object> consumerConfigs() { Map<String, Object> props = new HashMap<>(kafkaProperties.buildProducerProperties()); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class); props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class); return props; }
Example #5
Source File: VehicleStatusCountProcessor.java From microservice-patterns with Apache License 2.0 | 5 votes |
@Bean public KStream<String, Long> statusCountStreamProcessor(StreamsBuilder streamsBuilder) { KStream<Integer, VehicleLocation> stream = streamsBuilder.stream("gpslocation", //Read from topic Consumed.with(Serdes.Integer(), new JsonSerde<>(VehicleLocation.class))); //using Integer and JSON serde return stream.map((k,v)-> { // transform they key as Online/Offline based on status String online = v.isOnline() == true ? "Online" : "Offline"; return new KeyValue<>(online, v); }) .groupByKey(Serialized.with( //Group by the newly mapped key in previous step Serdes.String(), new JsonSerde<>(VehicleLocation.class)) ) .count(Materialized.as("statusCount")) // materialize this value to state store .toStream(); }
Example #6
Source File: CollectionSerde.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
/** * Constructor to delegate serialization operations for the inner objects * to {@link JsonSerde}. * * @param targetTypeForJsonSerde target type used by the JsonSerde * @param collectionsClass type of the Collection class */ public CollectionSerde(Class<?> targetTypeForJsonSerde, Class<?> collectionsClass) { this.collectionClass = collectionsClass; try (JsonSerde<E> jsonSerde = new JsonSerde(targetTypeForJsonSerde)) { this.inner = Serdes.serdeFrom( new CollectionSerializer<>(jsonSerde.serializer()), new CollectionDeserializer<>(jsonSerde.deserializer(), collectionsClass)); } }
Example #7
Source File: KafkaStreamsBinderPojoInputAndPrimitiveTypeOutputTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@StreamListener("input") @SendTo("output") public KStream<Integer, Long> process(KStream<Object, Product> input) { return input.filter((key, product) -> product.getId() == 123) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Serialized.with(new JsonSerde<>(Product.class), new JsonSerde<>(Product.class))) .windowedBy(TimeWindows.of(5000)) .count(Materialized.as("id-count-store-x")).toStream() .map((key, value) -> { return new KeyValue<>(key.key().id, value); }); }
Example #8
Source File: KafkastreamsBinderPojoInputStringOutputIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@StreamListener("input") @SendTo("output") public KStream<Integer, String> process(KStream<Object, Product> input) { return input.filter((key, product) -> product.getId() == 123) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Serialized.with(new JsonSerde<>(Product.class), new JsonSerde<>(Product.class))) .windowedBy(TimeWindows.of(5000)) .count(Materialized.as("id-count-store")).toStream() .map((key, value) -> new KeyValue<>(key.key().id, "Count for product with ID 123: " + value)); }
Example #9
Source File: DeserializtionErrorHandlerByBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@StreamListener("input") @SendTo("output") public KStream<Integer, Long> process(KStream<Object, Product> input) { return input.filter((key, product) -> product.getId() == 123) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Grouped.with(new JsonSerde<>(Product.class), new JsonSerde<>(Product.class))) .windowedBy(TimeWindows.of(5000)) .count(Materialized.as("id-count-store-x")).toStream() .map((key, value) -> new KeyValue<>(key.key().id, value)); }
Example #10
Source File: KafkaStreamsInteractiveQueryIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@StreamListener("input") @SendTo("output") public KStream<?, String> process(KStream<Object, Product> input) { return input.filter((key, product) -> product.getId() == 123) .map((key, value) -> new KeyValue<>(value.id, value)) .groupByKey(Serialized.with(new Serdes.IntegerSerde(), new JsonSerde<>(Product.class))) .count(Materialized.as("prod-id-count-store")).toStream() .map((key, value) -> new KeyValue<>(null, "Count for product with ID 123: " + value)); }
Example #11
Source File: KafkaStreamsProductTrackerApplication.java From spring-cloud-stream-samples with Apache License 2.0 | 5 votes |
@Bean public Function<KStream<Object, Product>, KStream<Integer, ProductStatus>> process() { return input -> input .filter((key, product) -> productIds().contains(product.getId())) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Grouped.with(new JsonSerde<>(Product.class), new JsonSerde<>(Product.class))) .windowedBy(TimeWindows.of(Duration.ofSeconds(60))) .count(Materialized.as("product-counts")) .toStream() .map((key, value) -> new KeyValue<>(key.key().id, new ProductStatus(key.key().id, value, Instant.ofEpochMilli(key.window().start()).atZone(ZoneId.systemDefault()).toLocalTime(), Instant.ofEpochMilli(key.window().end()).atZone(ZoneId.systemDefault()).toLocalTime()))); }
Example #12
Source File: KafkaStreamsAggregateSampleTests.java From spring-cloud-stream-samples with Apache License 2.0 | 5 votes |
@Test public void testKafkaStreamsWordCountProcessor() throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ObjectMapper mapper = new ObjectMapper(); Serde<DomainEvent> domainEventSerde = new JsonSerde<>(DomainEvent.class, mapper); senderProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); senderProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, domainEventSerde.serializer().getClass()); DefaultKafkaProducerFactory<String, DomainEvent> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<String, DomainEvent> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("foobar"); DomainEvent ddEvent = new DomainEvent(); ddEvent.setBoardUuid("12345"); ddEvent.setEventType("create-domain-event"); template.sendDefault("", ddEvent); Thread.sleep(1000); RestTemplate restTemplate = new RestTemplate(); String fooResourceUrl = "http://localhost:" + randomServerPort + "/events"; ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl, String.class); assertThat(response.getBody()).contains("create-domain-event"); } finally { pf.destroy(); } }
Example #13
Source File: KafkaStreamsInteractiveQueryApplication.java From spring-cloud-stream-samples with Apache License 2.0 | 5 votes |
@Bean public Function<KStream<Object, Product>, KStream<Integer, Long>> process() { return input -> input .filter((key, product) -> productIds().contains(product.getId())) .map((key, value) -> new KeyValue<>(value.id, value)) .groupByKey(Grouped.with(Serdes.Integer(), new JsonSerde<>(Product.class))) .count(Materialized.<Integer, Long, KeyValueStore<Bytes, byte[]>>as(STORE_NAME) .withKeySerde(Serdes.Integer()) .withValueSerde(Serdes.Long())) .toStream(); }
Example #14
Source File: KafkaStreamsInventoryCountApplication.java From spring-cloud-stream-samples with Apache License 2.0 | 4 votes |
public KafkaStreamsInventoryAggregator(KeyValueBytesStoreSupplier storeSupplier) { this.storeSupplier = storeSupplier; this.keySerde = new JsonSerde<>(ProductKey.class); this.countEventSerde = new JsonSerde<>(InventoryCountEvent.class); this.updateEventSerde = new JsonSerde<>(InventoryUpdateEvent.class); }
Example #15
Source File: DomainEventSinkImpl.java From event-store-demo with GNU General Public License v3.0 | 3 votes |
public DomainEventSinkImpl( final ObjectMapper mapper ) { this.mapper = mapper; this.domainEventSerde = new JsonSerde<>( DomainEvent.class, mapper ); this.boardSerde = new JsonSerde<>( Board.class, mapper ); }
Example #16
Source File: DomainEventSinkImpl.java From event-store-demo with GNU General Public License v3.0 | 3 votes |
public DomainEventSinkImpl( final ObjectMapper mapper ) { this.mapper = mapper; this.domainEventSerde = new JsonSerde<>( DomainEvent.class, mapper ); this.boardSerde = new JsonSerde<>( Board.class, mapper ); }