Java Code Examples for org.apache.samza.storage.kv.KeyValueIterator#next()

The following examples show how to use org.apache.samza.storage.kv.KeyValueIterator#next() . 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: SimpleStatefulTask.java    From samza with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void init(Context context) {
  this.store = (KeyValueStore<String, String>) context.getTaskContext().getStore("mystore");
  System.out.println("Contents of store: ");
  KeyValueIterator<String, String> iter = store.all();
  while (iter.hasNext()) {
    Entry<String, String> entry = iter.next();
    System.out.println(entry.getKey() + " => " + entry.getValue());
  }
  iter.close();
}
 
Example 2
Source File: AbandonedCartStreamTask.java    From Unified-Log-Processing with Apache License 2.0 6 votes vote down vote up
@Override
public void window(MessageCollector collector,
  TaskCoordinator coordinator) {

  KeyValueIterator<String, String> entries = store.all();
  while (entries.hasNext()) {                                        // c
    Entry<String, String> entry = entries.next();
    String key = entry.getKey();
    String value = entry.getValue();
    if (isTimestampKey(key) && Cart.isAbandoned(value)) {            // d
      String shopper = extractShopper(key);
      String cart = store.get(asCartKey(shopper));
      
      AbandonedCartEvent event =
        new AbandonedCartEvent(shopper, cart);
      collector.send(new OutgoingMessageEnvelope(
        new SystemStream("kafka", "derived-events-ch04"), event));    // e
      
      resetShopper(shopper);
    }
  }
}
 
Example 3
Source File: Checker.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public void window(MessageCollector collector, TaskCoordinator coordinator) {
  String currentEpoch = this.store.get(CURRENT_EPOCH);
  logger.info("Checking if epoch " + currentEpoch + " is complete.");
  int count = 0;
  KeyValueIterator<String, String> iter = this.store.all();

  while (iter.hasNext()) {
    Entry<String, String> entry = iter.next();
    String foundEpoch = entry.getValue();
    if (foundEpoch.equals(currentEpoch)) {
      count += 1;
    } else {
      logger.info("####### Found a different epoch! - " + foundEpoch + " Current epoch is " + currentEpoch);
    }
  }
  iter.close();
  if (count == expectedKeys + 1) {
    logger.info("Epoch " + currentEpoch + " is complete.");
    int nextEpoch = Integer.parseInt(currentEpoch) + 1;
    for (int i = 0; i < numPartitions; i++) {
      logger.info("Emitting next epoch - " + Integer.toString(i) + " -> " + Integer.toString(nextEpoch));
      collector.send(new OutgoingMessageEnvelope(new SystemStream("kafka", "epoch"), Integer.toString(i), Integer.toString(nextEpoch)));
    }
    this.store.put(CURRENT_EPOCH, Integer.toString(nextEpoch));
  } else if (count > expectedKeys + 1) {
    throw new IllegalStateException("Got " + count + " keys, which is more than the expected " + (expectedKeys + 1));
  } else {
    logger.info("Only found " + count + " valid keys, try again later.");
  }
}
 
Example 4
Source File: TestInMemoryKeyValueStore.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testSnapshot() throws Exception {
  InMemoryKeyValueStore store = new InMemoryKeyValueStore(
      new KeyValueStoreMetrics("testInMemory", new MetricsRegistryMap()));
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  String prefix = "prefix";
  for (int i = 0; i < 100; i++) {
    store.put(genKey(outputStream, prefix, i), genValue());
  }

  byte[] firstKey = genKey(outputStream, prefix, 0);
  byte[] lastKey = genKey(outputStream, prefix, 100);
  KeyValueSnapshot<byte[], byte[]> snapshot = store.snapshot(firstKey, lastKey);
  // Make sure the cached Iterable won't change when new elements are added
  store.put(genKey(outputStream, prefix, 200), genValue());
  assertTrue(Iterators.size(snapshot.iterator()) == 100);

  List<Integer> keys = new ArrayList<>();
  KeyValueIterator<byte[], byte[]> iter = snapshot.iterator();
  while (iter.hasNext()) {
    Entry<byte[], byte[]> entry = iter.next();
    int key = Ints.fromByteArray(Arrays.copyOfRange(entry.getKey(), prefix.getBytes().length, entry.getKey().length));
    keys.add(key);
  }
  assertEquals(keys, IntStream.rangeClosed(0, 99).boxed().collect(Collectors.toList()));

  outputStream.close();
  store.close();
}