private void verifyAllCustomersInStore(final List<Customer> customers, final ReadOnlyKeyValueStore<Long, Customer> store) { for (long id = 0; id < customers.size(); id++) { assertThat(store.get(id), equalTo(customers.get((int)id))); } }
private void verifyAllProductsInStore(final List<Product> products, final ReadOnlyKeyValueStore<Long, Product> store) { for (long id = 0; id < products.size(); id++) { assertThat(store.get(id), equalTo(products.get((int)id))); } }
@GET() @Path("/song/{id}") @Produces(MediaType.APPLICATION_JSON) public SongBean song(@PathParam("id") Long songId) { final ReadOnlyKeyValueStore<Long, Song> songStore = streams.store(KafkaMusicExample.ALL_SONGS, QueryableStoreTypes.<Long, Song>keyValueStore()); final Song song = songStore.get(songId); if (song == null) { throw new NotFoundException(String.format("Song with id [%d] was not found", songId)); } return new SongBean(song.getArtist(), song.getAlbum(), song.getName()); }
/** * Asserts that the key-value store contains exactly the expected content and nothing more. * * @param store the store to be validated * @param expected the expected contents of the store * @param <K> the store's key type * @param <V> the store's value type */ public static <K, V> void assertThatKeyValueStoreContains(ReadOnlyKeyValueStore<K, V> store, Map<K, V> expected) throws InterruptedException { TestUtils.waitForCondition(() -> expected.keySet() .stream() .allMatch(k -> expected.get(k).equals(store.get(k))), 30000, "Expected values not found in KV store"); }
/** * Fetch the order from the local materialized view * * @param id ID to fetch * @param asyncResponse the response to call once completed * @param predicate a filter that for this fetch, so for example we might fetch only VALIDATED * orders. */ private void fetchLocal(String id, AsyncResponse asyncResponse, Predicate<String, Order> predicate) { log.info("running GET on this node"); try { Order order = ordersStore().get(id); if (order == null || !predicate.test(id, order)) { log.info("Delaying get as order not present for id " + id); outstandingRequests.put(id, new FilteredResponse<>(asyncResponse, predicate)); } else { asyncResponse.resume(toBean(order)); } } catch (InvalidStateStoreException e) { //Store not ready so delay outstandingRequests.put(id, new FilteredResponse<>(asyncResponse, predicate)); } }
/** * Get a key-value pair from a KeyValue Store * @param storeName the store to look in * @param key the key to get * @return {@link KeyValueBean} representing the key-value pair */ @GET @Path("/keyvalue/{storeName}/{key}") @Produces(MediaType.APPLICATION_JSON) public KeyValueBean byKey(@PathParam("storeName") final String storeName, @PathParam("key") final String key) { HostStoreInfo hostStoreInfo = streamsMetadataForStoreAndKey(storeName, key); if (!thisHost(hostStoreInfo)){ return fetchByKey(hostStoreInfo, "state/keyvalue/"+storeName+"/"+key); } // Lookup the KeyValueStore with the provided storeName final ReadOnlyKeyValueStore<String, Long> store = streams.store(storeName, QueryableStoreTypes.<String, Long>keyValueStore()); if (store == null) { throw new NotFoundException(); } // Get the value from the store final Long value = store.get(key); if (value == null) { throw new NotFoundException(); } return new KeyValueBean(key, value); }
streams.store(storeName, QueryableStoreTypes.<String, KafkaMusicExample.TopFiveSongs>keyValueStore()); final KafkaMusicExample.TopFiveSongs value = topFiveStore.get(key); if (value == null) { throw new NotFoundException(String.format("Unable to find value in %s for key %s", storeName, key)); final Song song = songStore.get(songPlayCount.getSongId()); results.add(new SongPlayCountBean(song.getArtist(),song.getAlbum(), song.getName(), songPlayCount.getPlays()));
public Optional<AvroStream> getAvroStreamForKey(AvroStreamKey key){ return Optional.ofNullable(view.get(key)); }
@Scheduled(fixedRate = 30000, initialDelay = 5000) public void printVersionCounts() { if (keyValueStore == null) { keyValueStore = queryService.getQueryableStore(STORE_NAME, QueryableStoreTypes.keyValueStore()); } logger.info("Count for v1 is=" + keyValueStore.get("v1")); logger.info("Count for v2 is=" + keyValueStore.get("v2")); }
@Scheduled(fixedRate = 30000, initialDelay = 5000) public void printVersionCounts() { if (keyValueStore == null) { keyValueStore = queryService.getQueryableStore(STORE_NAME, QueryableStoreTypes.keyValueStore()); } logger.info("Count for v1 is=" + keyValueStore.get("v1")); logger.info("Count for v2 is=" + keyValueStore.get("v2")); }
@Override public OpennmsModelProtos.Alarm getAlarm(String reductionKey) { final byte[] alarmBytes = getAlarmTableNow().get(reductionKey); try { return OpennmsModelProtos.Alarm.parseFrom(alarmBytes); } catch (InvalidProtocolBufferException e) { throw new RuntimeException("Failed to parse alarm for bytes at reduction key " + reductionKey, e); } }
@Override Supplier<List<String>> query() { return () -> { try { ReadOnlyKeyValueStore<String, Set<String>> store = kafkaStreams.store(storeName, QueryableStoreTypes.keyValueStore()); if (serviceName == null || serviceName.equals("all")) return new ArrayList<>(); Set<String> spanNames = store.get(serviceName); if (spanNames == null) return new ArrayList<>(); return new ArrayList<>(spanNames); } catch (Exception e) { LOG.error("Error looking up for span names for service {}", serviceName, e); return new ArrayList<>(); } }; }
@RequestMapping("/events") public String events() { final ReadOnlyKeyValueStore<String, String> topFiveStore = queryableStoreRegistry.getQueryableStoreType("test-events-snapshots", QueryableStoreTypes.<String, String>keyValueStore()); return topFiveStore.get("12345"); } }
@RequestMapping("/events") public String events() { final ReadOnlyKeyValueStore<String, String> topFiveStore = interactiveQueryService.getQueryableStore("test-events-snapshots", QueryableStoreTypes.<String, String>keyValueStore()); return topFiveStore.get("12345"); } }
@Override public OpennmsModelProtos.Alarm getAlarm(String reductionKey) { final byte[] alarmBytes = getAlarmTableNow().get(reductionKey); try { return OpennmsModelProtos.Alarm.parseFrom(alarmBytes); } catch (InvalidProtocolBufferException e) { throw new RuntimeException("Failed to parse alarm for bytes at reduction key " + reductionKey, e); } }
@Override Supplier<List<Span>> query() { return () -> { try { ReadOnlyKeyValueStore<String, List<Span>> traceStore = kafkaStreams.store(storeName, QueryableStoreTypes.keyValueStore()); return traceStore.get(traceId); } catch (Exception e) { LOG.error("Error getting trace with ID {}", traceId, e); return null; } }; }
@Scheduled(fixedRate = 30000, initialDelay = 5000) public void printProductCounts() { if (keyValueStore == null) { keyValueStore = queryService.getQueryableStore(STORE_NAME, QueryableStoreTypes.keyValueStore()); } for (Integer id : productIds()) { System.out.println("Product ID: " + id + " Count: " + keyValueStore.get(id)); } }
@Override public V get(final K key) { Objects.requireNonNull(key); final List<ReadOnlyKeyValueStore<K, V>> stores = storeProvider.stores(storeName, storeType); for (final ReadOnlyKeyValueStore<K, V> store : stores) { try { final V result = store.get(key); if (result != null) { return result; } } catch (final InvalidStateStoreException e) { throw new InvalidStateStoreException("State store is not available anymore and may have been migrated to another instance; please re-discover its location from the state metadata."); } } return null; }
@Override public Optional<ClusterValue> getClusterByKey(ClusterKey clusterKey) { ClusterValue clusterValue; Validate.validState(store != null, "Infra Manager should be configured"); try { log.debug("Approximate Num. of Entries in Infra Table-{}", store.approximateNumEntries()); clusterValue = store.get(clusterKey); } catch (Exception e) { throw new IllegalStateException("Error while retrieving the cluster Value using cluster key:" + clusterKey, e); } if (clusterValue == null) { log.error("Cluster Not Found, key: {}", clusterKey); } else { log.info("Cluster Name - {}", clusterValue.getClusterProperties()); } return Optional.ofNullable(clusterValue); } }
@RequestMapping("/song/idx") public SongBean song(@RequestParam(value="id") Long id) { final ReadOnlyKeyValueStore<Long, Song> songStore = interactiveQueryService.getQueryableStore(KafkaStreamsInteractiveQuerySample.ALL_SONGS, QueryableStoreTypes.<Long, Song>keyValueStore()); final Song song = songStore.get(id); if (song == null) { throw new IllegalArgumentException("hi"); } return new SongBean(song.getArtist(), song.getAlbum(), song.getName()); }