Search in sources :

Example 71 with SessionWindow

use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.

the class RocksDBSessionStoreTest method shouldFindValuesWithinMergingSessionWindowRange.

@Test
public void shouldFindValuesWithinMergingSessionWindowRange() throws Exception {
    final String key = "a";
    sessionStore.put(new Windowed<>(key, new SessionWindow(0L, 0L)), 1L);
    sessionStore.put(new Windowed<>(key, new SessionWindow(1000L, 1000L)), 2L);
    final KeyValueIterator<Windowed<String>, Long> results = sessionStore.findSessions(key, -1, 1000L);
    final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(0L, 0L)), 1L), KeyValue.pair(new Windowed<>(key, new SessionWindow(1000L, 1000L)), 2L));
    assertEquals(expected, toList(results));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 72 with SessionWindow

use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.

the class CachingSessionStoreTest method shouldPutFetchFromCache.

@Test
public void shouldPutFetchFromCache() throws Exception {
    cachingStore.put(new Windowed<>("a", new SessionWindow(0, 0)), 1L);
    cachingStore.put(new Windowed<>("aa", new SessionWindow(0, 0)), 1L);
    cachingStore.put(new Windowed<>("b", new SessionWindow(0, 0)), 1L);
    final KeyValueIterator<Windowed<String>, Long> a = cachingStore.findSessions("a", 0, 0);
    final KeyValueIterator<Windowed<String>, Long> b = cachingStore.findSessions("b", 0, 0);
    assertEquals(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L), a.next());
    assertEquals(KeyValue.pair(new Windowed<>("b", new SessionWindow(0, 0)), 1L), b.next());
    assertFalse(a.hasNext());
    assertFalse(b.hasNext());
    assertEquals(3, cache.size());
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 73 with SessionWindow

use of org.apache.kafka.streams.kstream.internals.SessionWindow in project apache-kafka-on-k8s by banzaicloud.

the class SessionKeySchemaTest method before.

@Before
public void before() {
    sessionKeySchema.init("topic");
    final List<KeyValue<Bytes, Integer>> keys = Arrays.asList(KeyValue.pair(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0, 0 }), new SessionWindow(0, 0)))), 1), KeyValue.pair(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0 }), new SessionWindow(0, 0)))), 2), KeyValue.pair(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0, 0, 0 }), new SessionWindow(0, 0)))), 3), KeyValue.pair(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0 }), new SessionWindow(10, 20)))), 4), KeyValue.pair(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0, 0 }), new SessionWindow(10, 20)))), 5), KeyValue.pair(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0, 0, 0 }), new SessionWindow(10, 20)))), 6));
    iterator = new DelegatingPeekingKeyValueIterator<>("foo", new KeyValueIteratorStub<>(keys.iterator()));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) KeyValueIteratorStub(org.apache.kafka.test.KeyValueIteratorStub) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Before(org.junit.Before)

Example 74 with SessionWindow

use of org.apache.kafka.streams.kstream.internals.SessionWindow in project apache-kafka-on-k8s by banzaicloud.

the class SessionKeySchemaTest method testLowerBoundMatchesTrailingZeros.

@Test
public void testLowerBoundMatchesTrailingZeros() {
    Bytes lower = sessionKeySchema.lowerRange(Bytes.wrap(new byte[] { 0xA, 0xB, 0xC }), Long.MAX_VALUE);
    assertThat("appending zeros to key should still be in range", lower.compareTo(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0xA, 0xB, 0xC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }), new SessionWindow(Long.MAX_VALUE, Long.MAX_VALUE))))) < 0);
    assertThat(lower, equalTo(Bytes.wrap(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0xA, 0xB, 0xC }), new SessionWindow(0, 0))))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 75 with SessionWindow

use of org.apache.kafka.streams.kstream.internals.SessionWindow in project apache-kafka-on-k8s by banzaicloud.

the class RocksDBSegmentedBytesStoreTest method shouldLoadSegementsWithOldStyleDateFormattedName.

@Test
public void shouldLoadSegementsWithOldStyleDateFormattedName() {
    final Segments segments = new Segments(storeName, retention, numSegments);
    final String key = "a";
    bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(0L, 0L))), serializeValue(50L));
    bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(30000L, 60000L))), serializeValue(100L));
    bytesStore.close();
    final String firstSegmentName = segments.segmentName(0);
    final String[] nameParts = firstSegmentName.split("\\.");
    final Long segmentId = Long.parseLong(nameParts[1]);
    final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
    formatter.setTimeZone(new SimpleTimeZone(0, "UTC"));
    final String formatted = formatter.format(new Date(segmentId * segmentInterval(retention, numSegments)));
    final File parent = new File(stateDir, storeName);
    final File oldStyleName = new File(parent, nameParts[0] + "-" + formatted);
    assertTrue(new File(parent, firstSegmentName).renameTo(oldStyleName));
    bytesStore = new RocksDBSegmentedBytesStore(storeName, retention, numSegments, schema);
    bytesStore.init(context, bytesStore);
    final List<KeyValue<Windowed<String>, Long>> results = toList(bytesStore.fetch(Bytes.wrap(key.getBytes()), 0L, 60000L));
    assertThat(results, equalTo(Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(0L, 0L)), 50L), KeyValue.pair(new Windowed<>(key, new SessionWindow(30000L, 60000L)), 100L))));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Date(java.util.Date) Windowed(org.apache.kafka.streams.kstream.Windowed) SimpleTimeZone(java.util.SimpleTimeZone) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Test(org.junit.Test)

Aggregations

SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)130 Test (org.junit.Test)113 Windowed (org.apache.kafka.streams.kstream.Windowed)112 Bytes (org.apache.kafka.common.utils.Bytes)50 KeyValue (org.apache.kafka.streams.KeyValue)50 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)9 HashMap (java.util.HashMap)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)8 StreamsTestUtils.verifyWindowedKeyValue (org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue)8 LinkedList (java.util.LinkedList)6 ReadOnlySessionStoreStub (org.apache.kafka.test.ReadOnlySessionStoreStub)6 GenericRow (io.confluent.ksql.GenericRow)5 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)4 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)4 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)4 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)4 IntegrationTest (org.apache.kafka.test.IntegrationTest)4