org.apache.kafka.streams.kstream.Materialized Java Examples
The following examples show how to use
org.apache.kafka.streams.kstream.Materialized.
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: 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 #3
Source File: WordCountStream.java From micronaut-kafka with Apache License 2.0 | 6 votes |
@Singleton @Named(STREAM_WORD_COUNT) KStream<String, String> wordCountStream(ConfiguredStreamBuilder builder) { // <3> // set default serdes Properties props = builder.getConfiguration(); props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName()); props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName()); props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); KStream<String, String> source = builder .stream(INPUT); KTable<String, Long> groupedByWord = source .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .groupBy((key, word) -> word, Grouped.with(Serdes.String(), Serdes.String())) //Store the result in a store for lookup later .count(Materialized.as(WORD_COUNT_STORE)); // <4> groupedByWord //convert to stream .toStream() //send to output using specific serdes .to(OUTPUT, Produced.with(Serdes.String(), Serdes.Long())); return source; }
Example #4
Source File: RunningAverage.java From kafka-tutorials with Apache License 2.0 | 6 votes |
protected static KTable<Long, Double> getRatingAverageTable(KStream<Long, Rating> ratings, String avgRatingsTopicName, SpecificAvroSerde<CountAndSum> countAndSumSerde) { // Grouping Ratings KGroupedStream<Long, Double> ratingsById = ratings .map((key, rating) -> new KeyValue<>(rating.getMovieId(), rating.getRating())) .groupByKey(with(Long(), Double())); final KTable<Long, CountAndSum> ratingCountAndSum = ratingsById.aggregate(() -> new CountAndSum(0L, 0.0), (key, value, aggregate) -> { aggregate.setCount(aggregate.getCount() + 1); aggregate.setSum(aggregate.getSum() + value); return aggregate; }, Materialized.with(Long(), countAndSumSerde)); final KTable<Long, Double> ratingAverage = ratingCountAndSum.mapValues(value -> value.getSum() / value.getCount(), Materialized.as("average-ratings")); // persist the result in topic ratingAverage.toStream().to(avgRatingsTopicName); return ratingAverage; }
Example #5
Source File: TumblingWindowExpressionTest.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 6 votes |
@Test public void shouldCreateTumblingWindowAggregate() { final KGroupedStream stream = EasyMock.createNiceMock(KGroupedStream.class); final TimeWindowedKStream windowedKStream = EasyMock.createNiceMock(TimeWindowedKStream.class); final UdafAggregator aggregator = EasyMock.createNiceMock(UdafAggregator.class); final TumblingWindowExpression windowExpression = new TumblingWindowExpression(10, TimeUnit.SECONDS); final Initializer initializer = () -> 0; final Materialized<String, GenericRow, WindowStore<Bytes, byte[]>> store = Materialized.as("store"); EasyMock.expect(stream.windowedBy(TimeWindows.of(10000L))).andReturn(windowedKStream); EasyMock.expect(windowedKStream.aggregate(same(initializer), same(aggregator), same(store))).andReturn(null); EasyMock.replay(stream, windowedKStream); windowExpression.applyAggregate(stream, initializer, aggregator, store); EasyMock.verify(stream, windowedKStream); }
Example #6
Source File: HoppingWindowExpressionTest.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 6 votes |
@Test public void shouldCreateHoppingWindowAggregate() { final KGroupedStream stream = EasyMock.createNiceMock(KGroupedStream.class); final TimeWindowedKStream windowedKStream = EasyMock.createNiceMock(TimeWindowedKStream.class); final UdafAggregator aggregator = EasyMock.createNiceMock(UdafAggregator.class); final HoppingWindowExpression windowExpression = new HoppingWindowExpression(10, TimeUnit.SECONDS, 4, TimeUnit.MILLISECONDS); final Initializer initializer = () -> 0; final Materialized<String, GenericRow, WindowStore<Bytes, byte[]>> store = Materialized.as("store"); EasyMock.expect(stream.windowedBy(TimeWindows.of(10000L).advanceBy(4L))).andReturn(windowedKStream); EasyMock.expect(windowedKStream.aggregate(same(initializer), same(aggregator), same(store))).andReturn(null); EasyMock.replay(stream, windowedKStream); windowExpression.applyAggregate(stream, initializer, aggregator, store); EasyMock.verify(stream, windowedKStream); }
Example #7
Source File: KGraph.java From kafka-graphs with Apache License 2.0 | 6 votes |
public <T> KGraph<K, VV, EV> joinWithEdgesOnSource(KTable<K, T> inputDataSet, final EdgeJoinFunction<EV, T> edgeJoinFunction) { KTable<Edge<K>, EV> resultedEdges = edgesGroupedBySource() .leftJoin(inputDataSet, new ApplyLeftJoinToEdgeValuesOnEitherSourceOrTarget<>(edgeJoinFunction), Materialized.with(keySerde(), new KryoSerde<>())) .toStream() .flatMap((k, edgeWithValues) -> { List<KeyValue<Edge<K>, EV>> edges = new ArrayList<>(); for (EdgeWithValue<K, EV> edge : edgeWithValues) { edges.add(new KeyValue<>(new Edge<>(edge.source(), edge.target()), edge.value())); } return edges; }) .groupByKey(Grouped.with(new KryoSerde<>(), edgeValueSerde())) .<EV>reduce((v1, v2) -> v2, Materialized.<Edge<K>, EV, KeyValueStore<Bytes, byte[]>>as( generateStoreName()).withKeySerde(new KryoSerde<>()).withValueSerde(edgeValueSerde())); return new KGraph<>(this.vertices, resultedEdges, serialized); }
Example #8
Source File: KGraph.java From kafka-graphs with Apache License 2.0 | 6 votes |
public <T> KGraph<K, VV, EV> joinWithEdgesOnTarget(KTable<K, T> inputDataSet, final EdgeJoinFunction<EV, T> edgeJoinFunction) { KTable<Edge<K>, EV> resultedEdges = edgesGroupedByTarget() .leftJoin(inputDataSet, new ApplyLeftJoinToEdgeValuesOnEitherSourceOrTarget<>(edgeJoinFunction), Materialized.with(keySerde(), new KryoSerde<>())) .toStream() .flatMap((k, edgeWithValues) -> { List<KeyValue<Edge<K>, EV>> edges = new ArrayList<>(); for (EdgeWithValue<K, EV> edge : edgeWithValues) { edges.add(new KeyValue<>(new Edge<>(edge.source(), edge.target()), edge.value())); } return edges; }) .groupByKey(Grouped.with(new KryoSerde<>(), edgeValueSerde())) .<EV>reduce((v1, v2) -> v2, Materialized.<Edge<K>, EV, KeyValueStore<Bytes, byte[]>>as( generateStoreName()).withKeySerde(new KryoSerde<>()).withValueSerde(edgeValueSerde())); return new KGraph<>(vertices, resultedEdges, serialized); }
Example #9
Source File: KGraph.java From kafka-graphs with Apache License 2.0 | 6 votes |
public KGraph<K, VV, EV> subgraph(Predicate<K, VV> vertexFilter, Predicate<Edge<K>, EV> edgeFilter) { KTable<K, VV> filteredVertices = vertices.filter(vertexFilter); KTable<Edge<K>, EV> remainingEdges = edgesBySource() .join(filteredVertices, (e, v) -> e, Joined.with(keySerde(), new KryoSerde<>(), vertexValueSerde())) .map((k, edge) -> new KeyValue<>(edge.target(), edge)) .join(filteredVertices, (e, v) -> e, Joined.with(keySerde(), new KryoSerde<>(), vertexValueSerde())) .map((k, edge) -> new KeyValue<>(new Edge<>(edge.source(), edge.target()), edge.value())) .groupByKey(Grouped.with(new KryoSerde<>(), edgeValueSerde())) .reduce((v1, v2) -> v2, Materialized.with(new KryoSerde<>(), edgeValueSerde())); KTable<Edge<K>, EV> filteredEdges = remainingEdges .filter(edgeFilter, Materialized.<Edge<K>, EV, KeyValueStore<Bytes, byte[]>>as(generateStoreName()).withKeySerde(new KryoSerde<>()).withValueSerde(edgeValueSerde())); return new KGraph<>(filteredVertices, filteredEdges, serialized); }
Example #10
Source File: KGraph.java From kafka-graphs with Apache License 2.0 | 6 votes |
public <T> KTable<K, T> groupReduceOnEdges(EdgesFunctionWithVertexValue<K, VV, EV, T> edgesFunction, EdgeDirection direction) throws IllegalArgumentException { switch (direction) { case IN: return vertices() .leftJoin(edgesGroupedByTarget(), new ApplyEdgeLeftJoinFunction<>(edgesFunction), Materialized.with(keySerde(), new KryoSerde<>())); case OUT: return vertices() .leftJoin(edgesGroupedBySource(), new ApplyEdgeLeftJoinFunction<>(edgesFunction), Materialized.with(keySerde(), new KryoSerde<>())); case BOTH: throw new UnsupportedOperationException(); default: throw new IllegalArgumentException("Illegal edge direction"); } }
Example #11
Source File: StreamDemo.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void main(String[] args) { // 1. 指定流的配置 Properties config = new Properties(); config.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-application"); config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, HOST); config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass()); config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); // 设置流构造器 StreamsBuilder builder = new StreamsBuilder(); KStream<String, String> textLines = builder.stream("TextLinesTopic"); KTable<String, Long> wordCounts = textLines .flatMapValues(textLine -> Arrays.asList(textLine.toLowerCase().split("\\W+"))) .groupBy((key, word) -> word) .count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("counts-store")); wordCounts.toStream().to("WordsWithCountsTopic", Produced.with(Serdes.String(), Serdes.Long())); // 根据流构造器和流配置初始化 Kafka 流 KafkaStreams streams = new KafkaStreams(builder.build(), config); streams.start(); }
Example #12
Source File: KafkaStreamsBinderWordCountIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@StreamListener @SendTo("output") public KStream<?, WordCount> process( @Input("input") KStream<Object, String> input) { return input .flatMapValues( value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Grouped.with(Serdes.String(), Serdes.String())) .windowedBy(TimeWindows.of(Duration.ofSeconds(5))).count(Materialized.as("foo-WordCounts")) .toStream() .map((key, value) -> new KeyValue<>(null, new WordCount(key.key(), value, new Date(key.window().start()), new Date(key.window().end())))); }
Example #13
Source File: KafkaStreamsBinderMultipleInputTopicsTest.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@StreamListener @SendTo("output") public KStream<?, WordCount> process( @Input("input") KStream<Object, String> input) { input.map((k, v) -> { System.out.println(k); System.out.println(v); return new KeyValue<>(k, v); }); return input .flatMapValues( value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Serialized.with(Serdes.String(), Serdes.String())) .count(Materialized.as("WordCounts")).toStream() .map((key, value) -> new KeyValue<>(null, new WordCount(key, value))); }
Example #14
Source File: WordCountMultipleBranchesIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@StreamListener("input") @SendTo({ "output1", "output2", "output3" }) @SuppressWarnings("unchecked") public KStream<?, WordCount>[] process(KStream<Object, String> input) { Predicate<Object, WordCount> isEnglish = (k, v) -> v.word.equals("english"); Predicate<Object, WordCount> isFrench = (k, v) -> v.word.equals("french"); Predicate<Object, WordCount> isSpanish = (k, v) -> v.word.equals("spanish"); return input .flatMapValues( value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .groupBy((key, value) -> value).windowedBy(TimeWindows.of(Duration.ofSeconds(5))) .count(Materialized.as("WordCounts-multi")).toStream() .map((key, value) -> new KeyValue<>(null, new WordCount(key.key(), value, new Date(key.window().start()), new Date(key.window().end())))) .branch(isEnglish, isFrench, isSpanish); }
Example #15
Source File: KafkaStreamsBinderWordCountBranchesFunctionTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@Bean @SuppressWarnings("unchecked") public Function<KStream<Object, String>, KStream<?, WordCount>[]> process() { Predicate<Object, WordCount> isEnglish = (k, v) -> v.word.equals("english"); Predicate<Object, WordCount> isFrench = (k, v) -> v.word.equals("french"); Predicate<Object, WordCount> isSpanish = (k, v) -> v.word.equals("spanish"); return input -> input .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .groupBy((key, value) -> value) .windowedBy(TimeWindows.of(5000)) .count(Materialized.as("WordCounts-branch")) .toStream() .map((key, value) -> new KeyValue<>(null, new WordCount(key.key(), value, new Date(key.window().start()), new Date(key.window().end())))) .branch(isEnglish, isFrench, isSpanish); }
Example #16
Source File: CountVersionApplication.java From spring-cloud-stream-samples with Apache License 2.0 | 6 votes |
@Bean public Function<KStream<Object, Sensor>, KStream<String, Long>> process() { Map<String, Object> configs = new HashMap<>(); configs.put("valueClass", Sensor.class); configs.put("contentType", "application/*+avro"); customSerde.configure(configs, false); return input -> input .map((key, value) -> { String newKey = "v1"; if (value.getId().toString().endsWith("v2")) { newKey = "v2"; } return new KeyValue<>(newKey, value); }) .groupByKey(Grouped.with(Serdes.String(), customSerde)) .count(Materialized.as(STORE_NAME)) .toStream(); }
Example #17
Source File: KafkaStreamsBranchingSample.java From spring-cloud-stream-samples with Apache License 2.0 | 6 votes |
@Bean @SuppressWarnings("unchecked") public Function<KStream<Object, String>, KStream<?, WordCount>[]> process() { Predicate<Object, WordCount> isEnglish = (k, v) -> v.word.equals("english"); Predicate<Object, WordCount> isFrench = (k, v) -> v.word.equals("french"); Predicate<Object, WordCount> isSpanish = (k, v) -> v.word.equals("spanish"); return input -> input .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .groupBy((key, value) -> value) .windowedBy(TimeWindows.of(Duration.ofSeconds(6))) .count(Materialized.as("WordCounts-1")) .toStream() .map((key, value) -> new KeyValue<>(null, new WordCount(key.key(), value, new Date(key.window().start()), new Date(key.window().end())))) .branch(isEnglish, isFrench, isSpanish); }
Example #18
Source File: WordCount.java From fluent-kafka-streams-tests with MIT License | 6 votes |
public Topology getTopology() { final Serde<String> stringSerde = Serdes.String(); final Serde<Long> longSerde = Serdes.Long(); final StreamsBuilder builder = new StreamsBuilder(); final KStream<String, String> textLines = builder.stream(this.inputTopic); final Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS); final KTable<String, Long> wordCounts = textLines .flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase()))) .groupBy((key, word) -> word) .count(Materialized.as("count")); wordCounts.toStream().to(this.outputTopic, Produced.with(stringSerde, longSerde)); return builder.build(); }
Example #19
Source File: StreamUtils.java From kafka-graphs with Apache License 2.0 | 6 votes |
public static <K, V> KTable<K, V> tableFromCollection( StreamsBuilder builder, Properties props, String topic, int numPartitions, short replicationFactor, Serde<K> keySerde, Serde<V> valueSerde, Collection<KeyValue<K, V>> values) { ClientUtils.createTopic(topic, numPartitions, replicationFactor, props); try (Producer<K, V> producer = new KafkaProducer<>(props, keySerde.serializer(), valueSerde.serializer())) { for (KeyValue<K, V> value : values) { ProducerRecord<K, V> producerRecord = new ProducerRecord<>(topic, value.key, value.value); producer.send(producerRecord); } producer.flush(); } return builder.table(topic, Consumed.with(keySerde, valueSerde), Materialized.with(keySerde, valueSerde)); }
Example #20
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 #21
Source File: KafkaStreamsPipeline.java From quarkus with Apache License 2.0 | 5 votes |
@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(); }
Example #22
Source File: CountVersionApplication.java From spring-cloud-stream-samples with Apache License 2.0 | 5 votes |
@Bean public Function<KStream<Object, Sensor>, KStream<String, Long>> process() { //The following Serde definitions are not needed in the topoloyy below //as we are not using it. However, if your topoloyg explicitly uses this //Serde, you need to configure this with the schema registry url as below. final Map<String, String> serdeConfig = Collections.singletonMap( AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081"); final SpecificAvroSerde<Sensor> sensorSerde = new SpecificAvroSerde<>(); sensorSerde.configure(serdeConfig, false); return input -> input .map((k, value) -> { String newKey = "v1"; if (value.getId().toString().endsWith("v2")) { newKey = "v2"; } return new KeyValue<>(newKey, value); }) .groupByKey() .count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as(STORE_NAME) .withKeySerde(Serdes.String()) .withValueSerde(Serdes.Long())) .toStream(); }
Example #23
Source File: TumblingWindowExpression.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public KTable applyAggregate(final KGroupedStream groupedStream, final Initializer initializer, final UdafAggregator aggregator, final Materialized<String, GenericRow, ?> materialized) { return groupedStream.windowedBy(TimeWindows.of(sizeUnit.toMillis(size))) .aggregate(initializer, aggregator, materialized); }
Example #24
Source File: CogroupingStreams.java From kafka-tutorials with Apache License 2.0 | 5 votes |
public Topology buildTopology(Properties envProps) { final StreamsBuilder builder = new StreamsBuilder(); final String appOneInputTopic = envProps.getProperty("app-one.topic.name"); final String appTwoInputTopic = envProps.getProperty("app-two.topic.name"); final String appThreeInputTopic = envProps.getProperty("app-three.topic.name"); final String totalResultOutputTopic = envProps.getProperty("output.topic.name"); final Serde<String> stringSerde = getPrimitiveAvroSerde(envProps, true); final Serde<LoginEvent> loginEventSerde = getSpecificAvroSerde(envProps); final Serde<LoginRollup> loginRollupSerde = getSpecificAvroSerde(envProps); final KStream<String, LoginEvent> appOneStream = builder.stream(appOneInputTopic, Consumed.with(stringSerde, loginEventSerde)); final KStream<String, LoginEvent> appTwoStream = builder.stream(appTwoInputTopic, Consumed.with(stringSerde, loginEventSerde)); final KStream<String, LoginEvent> appThreeStream = builder.stream(appThreeInputTopic, Consumed.with(stringSerde, loginEventSerde)); final Aggregator<String, LoginEvent, LoginRollup> loginAggregator = new LoginAggregator(); final KGroupedStream<String, LoginEvent> appOneGrouped = appOneStream.groupByKey(); final KGroupedStream<String, LoginEvent> appTwoGrouped = appTwoStream.groupByKey(); final KGroupedStream<String, LoginEvent> appThreeGrouped = appThreeStream.groupByKey(); appOneGrouped.cogroup(loginAggregator) .cogroup(appTwoGrouped, loginAggregator) .cogroup(appThreeGrouped, loginAggregator) .aggregate(() -> new LoginRollup(new HashMap<>()), Materialized.with(Serdes.String(), loginRollupSerde)) .toStream().to(totalResultOutputTopic, Produced.with(stringSerde, loginRollupSerde)); return builder.build(); }
Example #25
Source File: ExactTriangleCount.java From kafka-graphs with Apache License 2.0 | 5 votes |
public static KTable<Long, Long> countTriangles(KGraphStream<Long, Void, Void> graph) { return graph.buildNeighborhood(false) .map(new ExactTriangleCount.ProjectCanonicalEdges()) .flatMap(new ExactTriangleCount.IntersectNeighborhoods()) .mapValues(new ExactTriangleCount.SumAndEmitCounters()) .groupByKey() .reduce(Math::max, Materialized.as(KGraph.generateStoreName())); }
Example #26
Source File: KafkaStreamsBinderWordCountFunctionTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Bean public Function<KStream<Object, String>, KStream<String, WordCount>> process() { return input -> input .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Grouped.with(Serdes.String(), Serdes.String())) .windowedBy(TimeWindows.of(5000)) .count(Materialized.as("foo-WordCounts")) .toStream() .map((key, value) -> new KeyValue<>(key.key(), new WordCount(key.key(), value, new Date(key.window().start()), new Date(key.window().end())))); }
Example #27
Source File: HoppingWindowExpression.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public KTable applyAggregate( KGroupedStream groupedStream, Initializer initializer, UdafAggregator aggregator, Materialized<String, GenericRow, ?> materialized ) { return groupedStream.windowedBy( TimeWindows.of(sizeUnit.toMillis(size)) .advanceBy(advanceByUnit.toMillis(advanceBy)) ).aggregate(initializer, aggregator, materialized); }
Example #28
Source File: KafkaStreamsNativeEncodingDecodingTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@StreamListener("input") @SendTo("output") public KStream<?, String> process(KStream<Object, String> input) { return input .flatMapValues( value -> Arrays.asList(value.toLowerCase().split("\\W+"))) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey(Serialized.with(Serdes.String(), Serdes.String())) .windowedBy(TimeWindows.of(Duration.ofSeconds(5))).count(Materialized.as("foo-WordCounts-x")) .toStream().map((key, value) -> new KeyValue<>(null, "Count for " + key.key() + " : " + value)); }
Example #29
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 #30
Source File: SummaryBulkAggregation.java From kafka-graphs with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public KTable<Windowed<Short>, T> run(final KStream<Edge<K>, EV> edgeStream) { //For parallel window support we key the edge stream by partition and apply a parallel fold per partition. //Finally, we merge all locally combined results into our final graph aggregation property. KTable<Windowed<Short>, S> partialAgg = edgeStream .groupByKey(Grouped.with(new KryoSerde<>(), new KryoSerde<>())) .windowedBy(TimeWindows.of(Duration.ofMillis(timeMillis))) .aggregate(this::initialValue, new PartialAgg<>(updateFun())) .toStream() .groupBy((k, v) -> GLOBAL_KEY) .windowedBy(TimeWindows.of(Duration.ofMillis(timeMillis))) .reduce(combineFun()) .mapValues(aggregator(edgeStream), Materialized.<Windowed<Short>, S, KeyValueStore<Bytes, byte[]>> as(KGraph.generateStoreName()).withKeySerde(new KryoSerde<>()).withValueSerde(new KryoSerde<>())); if (transform() != null) { return partialAgg.mapValues( transform(), Materialized.<Windowed<Short>, T, KeyValueStore<Bytes, byte[]>> as(KGraph.generateStoreName()).withKeySerde(new KryoSerde<>()).withValueSerde(new KryoSerde<>()) ); } return (KTable<Windowed<Short>, T>) partialAgg; }