org.apache.kafka.streams.state.KeyValueStore Java Examples

The following examples show how to use org.apache.kafka.streams.state.KeyValueStore. 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 vote down vote up
@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: AggregationProcessorSupplier.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void init(final ProcessorContext context) {
    this.context = context;

    // Sort the group by vars so that they will be written to the state store in the same order every time.
    final List<String> groupByVars = Lists.newArrayList(aggNode.getGroupBindingNames());
    groupByVars.sort(Comparator.naturalOrder());

    // Get a reference to the state store that keeps track of aggregation state.
    final KeyValueStore<String, AggregationState> stateStore =
            (KeyValueStore<String, AggregationState>) context.getStateStore( stateStoreName );
    aggStateStore = new KeyValueAggregationStateStore(stateStore, groupByVars);

    // Create the aggregation evaluator.
    evaluator = AggregationsEvaluator.make(aggStateStore, aggNode, groupByVars);
}
 
Example #3
Source File: StockPerformanceStreamsProcessorTopologyTest.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Checking State Store for Value")
public void shouldStorePerformanceObjectInStore() {

    Serde<String> stringSerde = Serdes.String();
    Serde<StockTransaction> stockTransactionSerde = StreamsSerdes.StockTransactionSerde();

    StockTransaction stockTransaction = DataGenerator.generateStockTransaction();

    topologyTestDriver.process("stock-transactions",
            stockTransaction.getSymbol(),
            stockTransaction,
            stringSerde.serializer(),
            stockTransactionSerde.serializer());

    KeyValueStore<String, StockPerformance> store = topologyTestDriver.getKeyValueStore("stock-performance-store");
    
    assertThat(store.get(stockTransaction.getSymbol()), notNullValue());

    StockPerformance stockPerformance = store.get(stockTransaction.getSymbol());
    
    assertThat(stockPerformance.getCurrentShareVolume(), equalTo(stockTransaction.getShares()));
    assertThat(stockPerformance.getCurrentPrice(), equalTo(stockTransaction.getSharePrice()));
}
 
Example #4
Source File: KGraph.java    From kafka-graphs with Apache License 2.0 6 votes vote down vote up
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 #5
Source File: KafkaStreamsFunctionStateStoreTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Bean
public java.util.function.Consumer<KTable<Object, String>> hello() {
	return input -> {
		input.toStream().process(() -> new Processor<Object, String>() {
			@Override
			@SuppressWarnings("unchecked")
			public void init(ProcessorContext context) {
				state3 = (KeyValueStore<Long, Long>) context.getStateStore("my-store");
				state4 = (WindowStore<Long, Long>) context.getStateStore("other-store");
			}

			@Override
			public void process(Object key, String value) {
				processed2 = true;
			}

			@Override
			public void close() {

			}
		}, "my-store", "other-store");
	};
}
 
Example #6
Source File: KGraph.java    From kafka-graphs with Apache License 2.0 6 votes vote down vote up
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 #7
Source File: StockPerformanceMetricsTransformer.java    From kafka-streams-in-action with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void init(final ProcessorContext processorContext) {
    keyValueStore = (KeyValueStore) processorContext.getStateStore(stocksStateStore);
    this.processorContext = processorContext;
    
    this.processorContext.schedule(5000, PunctuationType.WALL_CLOCK_TIME, this::doPunctuate);


    final String tagKey = "task-id";
    final String tagValue = processorContext.taskId().toString();
    final String nodeName = "StockPerformanceProcessor_"+count.getAndIncrement();
   metricsSensor = processorContext.metrics().addLatencyAndThroughputSensor("transformer-node",
            nodeName, "stock-performance-calculation",
            Sensor.RecordingLevel.DEBUG,
            tagKey,
            tagValue);
}
 
Example #8
Source File: KafkaStreamsFunctionStateStoreTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Bean
public java.util.function.BiConsumer<KStream<Object, String>, KStream<Object, String>> process() {
	return (input0, input1) ->
			input0.process((ProcessorSupplier<Object, String>) () -> new Processor<Object, String>() {
				@Override
				@SuppressWarnings("unchecked")
				public void init(ProcessorContext context) {
					state1 = (KeyValueStore<Long, Long>) context.getStateStore("my-store");
					state2 = (WindowStore<Long, Long>) context.getStateStore("other-store");
				}

				@Override
				public void process(Object key, String value) {
					processed1 = true;
				}

				@Override
				public void close() {

				}
			}, "my-store", "other-store");
}
 
Example #9
Source File: KafkaStreamsAggregateSample.java    From spring-cloud-stream-samples with Apache License 2.0 6 votes vote down vote up
@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 #10
Source File: AppTopologyTest.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 6 votes vote down vote up
@Test
@Order(4)
@DisplayName("Check current state store values and validate end state")
void testStateStore(){
    KeyValueStore<String,CampaignPerformance> store =
        topologyTestDriver.getKeyValueStore(
        AppConfigs.stateStoreNameCP);

    CampaignPerformance cpValue =  store.get("ABC Ltd");
    logger.info(cpValue);

    assertAll(() -> assertEquals("ABC Ltd", cpValue.getCampaigner()),
        () -> assertEquals("2", cpValue.getAdImpressions().toString()),
        () -> assertEquals("1", cpValue.getAdClicks().toString())
    );
}
 
Example #11
Source File: KeyValueJoinStateStore.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance of {@link KeyValueJoinStateStore}.
 *
 * @param store - The state store that will be used. (not null)
 * @param id - The ID used for the state store. (not null)
 * @param joinVars - The variables that are used to build grouping keys. (not null)
 * @param allVars - The variables that are used to build full value keys. (not null)
 * @throws IllegalArgumentException Thrown if {@code allVars} does not start with {@code joinVars}.
 */
public KeyValueJoinStateStore(
        final KeyValueStore<String, VisibilityBindingSet> store,
        final String id,
        final List<String> joinVars,
        final List<String> allVars) throws IllegalArgumentException {
    this.store = requireNonNull(store);
    this.id = requireNonNull(id);
    this.joinVars = requireNonNull(joinVars);
    this.allVars = requireNonNull(allVars);

    for(int i = 0; i < joinVars.size(); i++) {
        if(!joinVars.get(i).equals(allVars.get(i))) {
            throw new IllegalArgumentException("All vars must be lead by the join vars, but it did not. " +
                    "Join Vars: " + joinVars + ", All Vars: " + allVars);
        }
    }
}
 
Example #12
Source File: StreamDemo.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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 #13
Source File: CPUMetricStreamHandler.java    From kafka-streams-example with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ProcessorContext pc) {

    this.pc = pc;
    this.pc.schedule(12000); //invoke punctuate every 12 seconds
    this.machineToAvgCPUUsageStore = (KeyValueStore<String, Double>) pc.getStateStore(AVG_STORE_NAME);
    this.machineToNumOfRecordsReadStore = (KeyValueStore<String, Integer>) pc.getStateStore(NUM_RECORDS_STORE_NAME);

    PROC_LOGGER.info("Processor initialized");
}
 
Example #14
Source File: KafkaAnomalyDetectorMapper.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Override
protected Topology buildTopology() {
    val config = getConfig();
    val inputTopic = config.getInputTopic();
    val defaultOutputTopic = config.getOutputTopic();
    log.info("Initializing: inputTopic={}, defaultOutputTopic={}", inputTopic, defaultOutputTopic);

    val builder = new StreamsBuilder();

    // create store
    StoreBuilder<KeyValueStore<String, MetricData>> keyValueStoreBuilder =
            Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore(STATE_STORE_NAME),
                    Serdes.String(),
                    new MetricDataJsonSerde())
                    .withLoggingDisabled();
    // register store
    builder.addStateStore(keyValueStoreBuilder);

    //Dynamically choose kafka topic depending on the consumer id.
    final TopicNameExtractor<String, MappedMetricData> kafkaTopicNameExtractor = (key, mappedMetricData, recordContext) -> {
        final String consumerId = mappedMetricData.getConsumerId();
        if (DEFAULT_CONSUMER_ID.equals(consumerId)) {
            return defaultOutputTopic;
        }
        return defaultOutputTopic + "-" + consumerId;
    };

    final KStream<String, MetricData> stream = builder.stream(inputTopic);
    stream
            .filter((key, md) -> md != null)
            .transform(new MetricDataTransformerSupplier(mapper, STATE_STORE_NAME), STATE_STORE_NAME)
            .flatMap(this::metricsByDetector)
            .to(kafkaTopicNameExtractor, Produced.with(outputKeySerde, outputValueSerde));
    return builder.build();
}
 
Example #15
Source File: StockProcessor.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
@Override
public void init(ProcessorContext processorContext) {
    this.stateStore = (KeyValueStore<String, TickerStack>)
        processorContext.getStateStore(STATE_STORE_NAME);

    processorContext.schedule(
        Duration.ofSeconds(AppConfigs.secondsDelay),
        PunctuationType.WALL_CLOCK_TIME,
        new StockPunctuator(processorContext, stateStore)
    );
}
 
Example #16
Source File: KafkaStreamsFunctionStateStoreTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private void receiveAndValidate(ConfigurableApplicationContext context) throws Exception {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
	try {
		KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
		template.setDefaultTopic("words");
		template.sendDefault(1, "foobar");
		Thread.sleep(2000L);
		StateStoreTestApplication processorApplication = context
				.getBean(StateStoreTestApplication.class);

		KeyValueStore<Long, Long> state1 = processorApplication.state1;
		assertThat(processorApplication.processed1).isTrue();
		assertThat(state1 != null).isTrue();
		assertThat(state1.name()).isEqualTo("my-store");
		WindowStore<Long, Long> state2 = processorApplication.state2;
		assertThat(state2 != null).isTrue();
		assertThat(state2.name()).isEqualTo("other-store");
		assertThat(state2.persistent()).isTrue();

		KeyValueStore<Long, Long> state3 = processorApplication.state1;
		assertThat(processorApplication.processed2).isTrue();
		assertThat(state3 != null).isTrue();
		assertThat(state3.name()).isEqualTo("my-store");
		WindowStore<Long, Long> state4 = processorApplication.state2;
		assertThat(state4 != null).isTrue();
		assertThat(state4.name()).isEqualTo("other-store");
		assertThat(state4.persistent()).isTrue();
	}
	finally {
		pf.destroy();
	}
}
 
Example #17
Source File: StockPerformanceStreamsAndProcessorMultipleValuesApplication.java    From kafka-streams-in-action with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {


        StreamsConfig streamsConfig = new StreamsConfig(getProperties());
        Serde<String> stringSerde = Serdes.String();
        Serde<StockPerformance> stockPerformanceSerde = StreamsSerdes.StockPerformanceSerde();
        Serde<StockTransaction> stockTransactionSerde = StreamsSerdes.StockTransactionSerde();


        StreamsBuilder builder = new StreamsBuilder();

        String stocksStateStore = "stock-performance-store";
        double differentialThreshold = 0.05;

        TransformerSupplier<String, StockTransaction, KeyValue<String, List<KeyValue<String, StockPerformance>>>> transformerSupplier =
                () -> new StockPerformanceMultipleValuesTransformer(stocksStateStore, differentialThreshold);

        KeyValueBytesStoreSupplier storeSupplier = Stores.lruMap(stocksStateStore, 100);
        StoreBuilder<KeyValueStore<String, StockPerformance>> storeBuilder = Stores.keyValueStoreBuilder(storeSupplier, Serdes.String(), stockPerformanceSerde);

        builder.addStateStore(storeBuilder);

        builder.stream("stock-transactions", Consumed.with(stringSerde, stockTransactionSerde))
                .transform(transformerSupplier, stocksStateStore).flatMap((dummyKey,valueList) -> valueList)
                .print(Printed.<String, StockPerformance>toSysOut().withLabel("StockPerformance"));
                //.to(stringSerde, stockPerformanceSerde, "stock-performance");


        KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), streamsConfig);
        MockDataProducer.produceStockTransactionsWithKeyFunction(50, 50, 25, StockTransaction::getSymbol);
        System.out.println("Stock Analysis KStream/Process API App Started");
        kafkaStreams.cleanUp();
        kafkaStreams.start();
        Thread.sleep(70000);
        System.out.println("Shutting down the Stock KStream/Process API Analysis App now");
        kafkaStreams.close();
        MockDataProducer.shutdown();
    }
 
Example #18
Source File: KafkaStreamsInteractiveQueryApplication.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@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 #19
Source File: PregelComputation.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void init(final ProcessorContext context) {
    this.context = context;
    this.localSolutionSetStore = (KeyValueStore<K, Tuple4<Integer, VV, Integer, VV>>) context.getStateStore(localSolutionSetStoreName);
    this.verticesStore = (TimestampedKeyValueStore<K, VV>) context.getStateStore(vertices.queryableStoreName());
    this.edgesStore = (TimestampedKeyValueStore<K, Map<K, EV>>) context.getStateStore(edgesGroupedBySource.queryableStoreName());
}
 
Example #20
Source File: KStreamAggDemo.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, AppConfigs.applicationID);
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfigs.bootstrapServers);
    props.put(StreamsConfig.STATE_DIR_CONFIG, AppConfigs.stateStoreName);

    StreamsBuilder streamsBuilder = new StreamsBuilder();
    KStream<String, Employee> KS0 = streamsBuilder.stream(AppConfigs.topicName,
        Consumed.with(AppSerdes.String(), AppSerdes.Employee()));

    KGroupedStream<String, Employee> KGS1 = KS0.groupBy(
        (k, v) -> v.getDepartment(),
        Serialized.with(AppSerdes.String(),
            AppSerdes.Employee()));

    KTable<String, DepartmentAggregate> KT2 = KGS1.aggregate(
        //Initializer
        () -> new DepartmentAggregate()
            .withEmployeeCount(0)
            .withTotalSalary(0)
            .withAvgSalary(0D),
        //Aggregator
        (k, v, aggV) -> new DepartmentAggregate()
            .withEmployeeCount(aggV.getEmployeeCount() + 1)
            .withTotalSalary(aggV.getTotalSalary() + v.getSalary())
            .withAvgSalary((aggV.getTotalSalary() + v.getSalary()) / (aggV.getEmployeeCount() + 1D)),
        //Serializer
        Materialized.<String, DepartmentAggregate, KeyValueStore<Bytes, byte[]>>as("agg-store")
            .withKeySerde(AppSerdes.String())
            .withValueSerde(AppSerdes.DepartmentAggregate())
    );

    KT2.toStream().foreach(
        (k, v) -> System.out.println("Key = " + k + " Value = " + v.toString()));

    KafkaStreams streams = new KafkaStreams(streamsBuilder.build(), props);
    streams.start();
    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));

}
 
Example #21
Source File: KeyValueAggregationStateStore.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance of {@link KeyValueAggregationStateStore}.
 *
 * @param store - The state store that will be used. (not null)
 * @param groupByVars - An ordered list of group by variable names. (not null)
 */
public KeyValueAggregationStateStore(
        final KeyValueStore<String, AggregationState> store,
        final List<String> groupByVars) {
    this.store = requireNonNull(store);
    this.groupByVars = requireNonNull(groupByVars);
}
 
Example #22
Source File: MonitorProcessorSupplier.java    From DBus with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ProcessorContext context) {
    super.init(context);
    // 两分钟执行一次punctuate方法
    context().schedule(2 * 60 * 1000);
    this.store = (KeyValueStore<String, String>) context.getStateStore("zkInfo");
}
 
Example #23
Source File: StreamsTester.java    From football-events with MIT License 5 votes vote down vote up
public <K, V> KeyValueStore<K, V> getStore(String name) {
    KeyValueStore<K, V> store = testDriver.getKeyValueStore(name);

    if (store == null) {
        throw new IllegalArgumentException("Store not found: " + name);
    }
    return store;
}
 
Example #24
Source File: DomainEventSinkImpl.java    From event-store-demo with GNU General Public License v3.0 5 votes vote down vote up
@StreamListener( "input" )
public void process( KStream<Object, byte[]> input ) {
    log.debug( "process : enter" );

    input
            .map( (key, value) -> {

                try {

                    DomainEvent domainEvent = mapper.readValue( value, DomainEvent.class );
                    log.debug( "process : domainEvent=" + domainEvent );

                    return new KeyValue<>( domainEvent.getBoardUuid().toString(), domainEvent );

                } catch( IOException e ) {
                    log.error( "process : error converting json to DomainEvent", e );
                }

                return null;
            })
            .groupBy( (s, domainEvent) -> s, Serialized.with( Serdes.String(), domainEventSerde ) )
            .aggregate(
                    Board::new,
                    (key, domainEvent, board) -> board.handleEvent( domainEvent ),
                    Materialized.<String, Board, KeyValueStore<Bytes, byte[]>>as( BOARD_EVENTS_SNAPSHOTS )
                        .withKeySerde( Serdes.String() )
                        .withValueSerde( boardSerde )
            );

    log.debug( "process : exit" );
}
 
Example #25
Source File: DomainEventSinkImpl.java    From event-store-demo with GNU General Public License v3.0 5 votes vote down vote up
@StreamListener( "input" )
public void process( KStream<Object, byte[]> input ) {
    log.debug( "process : enter" );

    input
            .map( (key, value) -> {

                try {

                    DomainEvent domainEvent = mapper.readValue( value, DomainEvent.class );
                    log.debug( "process : domainEvent=" + domainEvent );

                    return new KeyValue<>( domainEvent.getBoardUuid().toString(), domainEvent );

                } catch( IOException e ) {
                    log.error( "process : error converting json to DomainEvent", e );
                }

                return null;
            })
            .groupBy( (s, domainEvent) -> s, Serialized.with( Serdes.String(), domainEventSerde ) )
            .aggregate(
                    Board::new,
                    (key, domainEvent, board) -> board.handleEvent( domainEvent ),
                    Materialized.<String, Board, KeyValueStore<Bytes, byte[]>>as( BOARD_EVENTS_SNAPSHOTS )
                        .withKeySerde( Serdes.String() )
                        .withValueSerde( boardSerde )
            );

    log.debug( "process : exit" );
}
 
Example #26
Source File: StreamsUtils.java    From football-events with MIT License 5 votes vote down vote up
public static <D, E extends Event> void addStore(Topology topology, Class<D> domainType, String store,
        Class<E>... eventTypes) {
    StoreBuilder<KeyValueStore<String, D>> matchStoreBuilder = Stores.keyValueStoreBuilder(
            Stores.persistentKeyValueStore(store), Serdes.String(), new JsonPojoSerde<D>(domainType))
            .withLoggingDisabled();

    String[] processorNames = Stream.of(eventTypes)
        .map(event -> event.getSimpleName() + "Process")
        .collect(Collectors.toList()).toArray(new String[eventTypes.length]);

    topology.addStateStore(matchStoreBuilder, processorNames);
}
 
Example #27
Source File: JoinProcessorSupplier.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final ProcessorContext context) {
    // Hold onto the context so that we can forward results.
    this.context = context;

    final String appId = context.applicationId();
    final UUID queryId = UuidUtils.extractUuidFromStringEnd(appId);

    // Get a reference to the state store that keeps track of what can be joined with.
    final KeyValueStore<String, VisibilityBindingSet> stateStore =
            (KeyValueStore<String, VisibilityBindingSet>) context.getStateStore( stateStoreName );
    joinStateStore = new KeyValueJoinStateStore( stateStore, queryId.toString(), joinVars, allVars );
}
 
Example #28
Source File: KGraph.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public KGraph<K, VV, EV> filterOnVertices(Predicate<K, VV> vertexFilter) {
    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.<Edge<K>, EV, KeyValueStore<Bytes, byte[]>>as(generateStoreName()).withKeySerde(new KryoSerde<>()).withValueSerde(edgeValueSerde()));

    return new KGraph<>(filteredVertices, remainingEdges, serialized);
}
 
Example #29
Source File: SummaryBulkAggregation.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source File: KGraph.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
public KGraph<K, VV, EV> undirected() {

        KTable<Edge<K>, EV> undirectedEdges = edges
            .toStream()
            .flatMap(new UndirectEdges<>())
            .groupByKey(Grouped.with(new KryoSerde<>(), serialized.edgeValueSerde()))
            .reduce((v1, v2) -> v2, Materialized.<Edge<K>, EV, KeyValueStore<Bytes, byte[]>>as(generateStoreName())
                .withKeySerde(new KryoSerde<>()).withValueSerde(serialized.edgeValueSerde()));

        return new KGraph<>(vertices, undirectedEdges, serialized);
    }