Java Code Examples for org.apache.kafka.streams.state.ReadOnlyKeyValueStore#get()

The following examples show how to use org.apache.kafka.streams.state.ReadOnlyKeyValueStore#get() . 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: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 6 votes vote down vote up
@Get("/serviceNames/:service_name/spanNames")
@ProducesJson
public JsonNode getSpanNames(@Param("service_name") String serviceName) {
  try {
    if (!storage.traceSearchEnabled) return MAPPER.createArrayNode();
    ReadOnlyKeyValueStore<String, Set<String>> store = storage.getTraceStorageStream()
        .store(SPAN_NAMES_STORE_NAME, QueryableStoreTypes.keyValueStore());
    Set<String> names = store.get(serviceName);
    ArrayNode array = MAPPER.createArrayNode();
    if (names != null) names.forEach(array::add);
    return array;
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    throw e;
  }
}
 
Example 2
Source File: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 6 votes vote down vote up
@Get("/serviceNames/:service_name/remoteServiceNames")
@ProducesJson
public JsonNode getRemoteServiceNames(@Param("service_name") String serviceName) {
  try {
    if (!storage.traceSearchEnabled) return MAPPER.createArrayNode();
    ReadOnlyKeyValueStore<String, Set<String>> store = storage.getTraceStorageStream()
        .store(REMOTE_SERVICE_NAMES_STORE_NAME, QueryableStoreTypes.keyValueStore());
    Set<String> names = store.get(serviceName);
    ArrayNode array = MAPPER.createArrayNode();
    if (names != null) names.forEach(array::add);
    return (array);
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    throw e;
  }
}
 
Example 3
Source File: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 6 votes vote down vote up
@Get("/traces/:trace_id")
public AggregatedHttpResponse getTrace(@Param("trace_id") String traceId) {
  try {
    if (!storage.traceByIdQueryEnabled) return AggregatedHttpResponse.of(HttpStatus.NOT_FOUND);
    ReadOnlyKeyValueStore<String, List<Span>> store =
        storage.getTraceStorageStream()
            .store(TRACES_STORE_NAME, QueryableStoreTypes.keyValueStore());
    List<Span> spans = store.get(traceId);
    return AggregatedHttpResponse.of(
        HttpStatus.OK,
        MediaType.JSON,
        SpanBytesEncoder.JSON_V2.encodeList(spans));
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    return AggregatedHttpResponse.of(HttpStatus.SERVICE_UNAVAILABLE);
  }
}
 
Example 4
Source File: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 6 votes vote down vote up
@Get("/autocompleteTags/:key")
@ProducesJson
public JsonNode getAutocompleteValues(@Param("key") String key) {
  try {
    if (!storage.traceSearchEnabled) return MAPPER.createArrayNode();
    ReadOnlyKeyValueStore<String, Set<String>> autocompleteTagsStore =
        storage.getTraceStorageStream().store(AUTOCOMPLETE_TAGS_STORE_NAME,
            QueryableStoreTypes.keyValueStore());
    Set<String> values = autocompleteTagsStore.get(key);
    ArrayNode array = MAPPER.createArrayNode();
    if (values != null) values.forEach(array::add);
    return array;
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    throw e;
  }
}
 
Example 5
Source File: AppRestService.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
private String readKeyFromLocal(String searchKey)
    throws JsonProcessingException {
    ReadOnlyKeyValueStore<String, DepartmentAggregate> departmentStore =
        streams.store(
            AppConfigs.aggStateStoreName,
            QueryableStoreTypes.keyValueStore()
        );
    DepartmentAggregate result = departmentStore.get(searchKey);
    return (result == null) ? NO_RESULTS
        : objectMapper.writeValueAsString(result);
}
 
Example 6
Source File: StatisticsBuilderTest.java    From football-events with MIT License 5 votes vote down vote up
@Test
public void testMatchStatistics() {
    tester.sendEvents(getClass().getResource("player-started-career.json"), PlayerStartedCareer.class);
    tester.sendEvents(getClass().getResource("match-started.json"), MatchStarted.class);
    tester.sendEvents(getClass().getResource("goal-scored.json"), GoalScored.class);
    tester.sendEvents(getClass().getResource("match-finished.json"), MatchFinished.class);

    ReadOnlyKeyValueStore<String, MatchScore> matchStore = tester.getStore(MATCH_SCORES_STORE);
    ReadOnlyKeyValueStore<String, TeamRanking> rankingStore = tester.getStore(TEAM_RANKING_STORE);

    assertThat(tester.count(rankingStore)).isEqualTo(24);
    assertThat(tester.count(matchStore)).isEqualTo(22);

    MatchScore brentfordVsNottinghamForest = matchStore.get("15");
    assertThat(brentfordVsNottinghamForest.getHomeGoals()).isEqualTo(3);
    assertThat(brentfordVsNottinghamForest.getAwayGoals()).isEqualTo(4);

    MatchScore leedsUnitedVsPrestonNorthEnd = matchStore.get("19");
    assertThat(leedsUnitedVsPrestonNorthEnd.getHomeGoals()).isEqualTo(0);
    assertThat(leedsUnitedVsPrestonNorthEnd.getAwayGoals()).isEqualTo(0);

    TeamRanking nottinghamForest = rankingStore.get("Nottingham Forest");
    assertThat(nottinghamForest.getMatchesPlayed()).isEqualTo(2);
    assertThat(nottinghamForest.getGoalsFor()).isEqualTo(5);
    assertThat(nottinghamForest.getGoalsAgainst()).isEqualTo(3);
    assertThat(nottinghamForest.getPoints()).isEqualTo(6);

    TeamRanking burtonAlbion = rankingStore.get("Burton Albion");
    assertThat(burtonAlbion.getMatchesPlayed()).isEqualTo(2);
    assertThat(burtonAlbion.getGoalsFor()).isEqualTo(1);
    assertThat(burtonAlbion.getGoalsAgainst()).isEqualTo(5);
    assertThat(burtonAlbion.getPoints()).isEqualTo(0);
}
 
Example 7
Source File: StatisticsBuilderTest.java    From football-events with MIT License 5 votes vote down vote up
@Test
public void testPlayerStatistics() {
    tester.sendEvents(getClass().getResource("player-started-career.json"), PlayerStartedCareer.class);
    tester.sendEvents(getClass().getResource("goal-scored.json"), GoalScored.class);
    tester.sendEvents(getClass().getResource("card-received.json"), CardReceived.class);

    ReadOnlyKeyValueStore<String, PlayerGoals> goalsStore = tester.getStore(PLAYER_GOALS_STORE);
    ReadOnlyKeyValueStore<String, PlayerCards> cardsStore = tester.getStore(PLAYER_CARDS_STORE);

    PlayerGoals bBradleyJohnsonGoals = goalsStore.get("B. Bradley Johnson");
    assertThat(bBradleyJohnsonGoals.getGoals()).isEqualTo(1);
    assertThat(cardsStore.get("B. Bradley Johnson")).isNull();

    PlayerGoals aAndreasBouchalakisGoals = goalsStore.get("A. Andreas Bouchalakis");
    PlayerCards aAndreasBouchalakisCards = cardsStore.get("A. Andreas Bouchalakis");
    assertThat(aAndreasBouchalakisGoals.getGoals()).isEqualTo(2);
    assertThat(aAndreasBouchalakisCards.getYellowCards()).isEqualTo(3);
    assertThat(aAndreasBouchalakisCards.getRedCards()).isEqualTo(0);

    PlayerCards jJamesVaughanCards = cardsStore.get("J. James Vaughan");
    assertThat(jJamesVaughanCards.getYellowCards()).isEqualTo(1);
    assertThat(jJamesVaughanCards.getRedCards()).isEqualTo(0);
    assertThat(goalsStore.get("J. James Vaughan")).isNull();

    PlayerCards dDarylMurphyCards = cardsStore.get("D. Daryl Murphy");
    PlayerGoals dDarylMurphyGoals = goalsStore.get("D. Daryl Murphy");
    assertThat(dDarylMurphyGoals.getGoals()).isEqualTo(1);
    assertThat(dDarylMurphyCards.getYellowCards()).isEqualTo(0);
    assertThat(dDarylMurphyCards.getRedCards()).isEqualTo(1);
}
 
Example 8
Source File: DomainUpdaterTest.java    From football-events with MIT License 5 votes vote down vote up
@Test
public void test() throws Exception {
    tester.sendEvents(getClass().getResource("player-started-career.json"), PlayerStartedCareer.class);
    tester.sendEvents(getClass().getResource("match-scheduled.json"), MatchScheduled.class);
    tester.sendEvents(getClass().getResource("match-started.json"), MatchStarted.class);
    tester.sendEvents(getClass().getResource("goal-scored.json"), GoalScored.class);
    tester.sendEvents(getClass().getResource("card-received.json"), CardReceived.class);
    tester.sendEvents(getClass().getResource("match-finished.json"), MatchFinished.class);

    ReadOnlyKeyValueStore<String, Match> matchStore = tester.getStore(DomainUpdater.MATCH_STORE);
    ReadOnlyKeyValueStore<String, Player> playerStore = tester.getStore(DomainUpdater.PLAYER_STORE);

    assertThat(tester.count(playerStore)).isEqualTo(4);
    assertThat(playerStore.get("3").getName()).isEqualTo("Lewis McGugan");

    assertThat(tester.count(matchStore)).isEqualTo(4);
    Match match1 = matchStore.get("1");
    Match match2 = matchStore.get("2");
    Match match3 = matchStore.get("3");
    Match match4 = matchStore.get("4");

    assertThat(match1.getState()).isEqualTo(Match.State.FINISHED);
    assertThat(match2.getState()).isEqualTo(Match.State.STARTED);
    assertThat(match3.getState()).isEqualTo(Match.State.STARTED);
    assertThat(match4.getState()).isEqualTo(Match.State.SCHEDULED);

    assertThat(match1.getHomeGoals().size()).isEqualTo(1);
    assertThat(match1.getAwayGoals().size()).isEqualTo(1);
    assertThat(match2.getHomeGoals().size()).isEqualTo(1);
    assertThat(match2.getAwayGoals().size()).isEqualTo(0);
    assertThat(match3.getHomeGoals().size()).isEqualTo(3);
    assertThat(match3.getAwayGoals().size()).isEqualTo(1);
    assertThat(match4.getHomeGoals().size()).isEqualTo(0);
    assertThat(match4.getAwayGoals().size()).isEqualTo(0);

    assertThat(match1.getCards().size()).isEqualTo(1);
    assertThat(match2.getCards().size()).isEqualTo(1);
}
 
Example 9
Source File: VehicleQueryService.java    From microservice-patterns with Apache License 2.0 5 votes vote down vote up
@GetMapping("/count/{status}")
public long getVehicleCoutnForStatus(@PathVariable("status") String status) {
	// Get the state-store
	ReadOnlyKeyValueStore<String, Long> keyValueStore= kStreamBuilderFactoryBean.getKafkaStreams()
													.store("statusCount", QueryableStoreTypes.keyValueStore());
	return keyValueStore.get(status);	//get the count for the key viz. Offline/Online
}
 
Example 10
Source File: KafkaStreamsInteractiveQueryIntegrationTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
public Long getProductStock(Integer id) {
	ReadOnlyKeyValueStore<Object, Object> keyValueStore = interactiveQueryService
			.getQueryableStore("prod-id-count-store",
					QueryableStoreTypes.keyValueStore());

	return (Long) keyValueStore.get(id);
}
 
Example 11
Source File: KafkaStreamsAggregateSample.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/events")
public String events() {

	final ReadOnlyKeyValueStore<String, String> topFiveStore =
			interactiveQueryService.getQueryableStore("test-events-snapshots", QueryableStoreTypes.<String, String>keyValueStore());
	return topFiveStore.get("12345");
}
 
Example 12
Source File: KafkaBoardClient.java    From event-store-demo with GNU General Public License v3.0 3 votes vote down vote up
@Override
public Board find( final UUID boardUuid ) {
    log.debug( "find : enter" );

    try {

        ReadOnlyKeyValueStore<String, Board> store = queryableStoreRegistry.getQueryableStoreType( BOARD_EVENTS_SNAPSHOTS, QueryableStoreTypes.<String, Board>keyValueStore() );

        Board board = store.get( boardUuid.toString() );
        if( null != board ) {

            log.debug( "find : board=" + board.toString() );

            log.debug( "find : exit" );
            return board;

        } else {

            throw new IllegalArgumentException( "board[" + boardUuid.toString() + "] not found!" );
        }

    } catch( InvalidStateStoreException e ) {
        log.error( "find : error", e );

    }

    throw new IllegalArgumentException( "board[" + boardUuid.toString() + "] not found!" );
}
 
Example 13
Source File: KafkaBoardClient.java    From event-store-demo with GNU General Public License v3.0 3 votes vote down vote up
@Override
    public Board find( final UUID boardUuid ) {
        log.debug( "find : enter" );

//        while( true ) {

            try {

                ReadOnlyKeyValueStore<String, Board> store = queryableStoreRegistry.getQueryableStoreType( BOARD_EVENTS_SNAPSHOTS, QueryableStoreTypes.<String, Board>keyValueStore() );

                Board board = store.get( boardUuid.toString() );
                if( null != board ) {

                    board.flushChanges();
                    log.debug( "find : board=" + board.toString() );

                    log.debug( "find : exit" );
                    return board;

                } else {

                    throw new IllegalArgumentException( "board[" + boardUuid.toString() + "] not found!" );
                }

            } catch( InvalidStateStoreException e ) {
                log.error( "find : error", e );

//                try {
//                    Thread.sleep( 100 );
//                } catch( InterruptedException e1 ) {
//                    log.error( "find : thread interrupted", e1 );
//                }

            }

//        }

        throw new IllegalArgumentException( "board[" + boardUuid.toString() + "] not found!" );
    }