Search in sources :

Example 6 with TimestampedValue

use of org.apache.samza.util.TimestampedValue in project samza by apache.

the class TestTimestampedValueSerde method testEmptyValueSerialization.

@Test
public void testEmptyValueSerialization() {
    byte[] expectedBytes = new byte[8];
    ByteBuffer.wrap(expectedBytes).putLong(1234L);
    TimestampedValueSerde<Integer> timestampedValueSerde = new TimestampedValueSerde<>(new IntegerSerde());
    TimestampedValue<Integer> timestampedValue = new TimestampedValue<>(null, 1234L);
    assertTrue(Arrays.equals(expectedBytes, timestampedValueSerde.toBytes(timestampedValue)));
}
Also used : TimestampedValue(org.apache.samza.util.TimestampedValue) IntegerSerde(org.apache.samza.serializers.IntegerSerde) Test(org.junit.Test)

Example 7 with TimestampedValue

use of org.apache.samza.util.TimestampedValue in project samza by apache.

the class TestTimeSeriesStoreImpl method testGetWithNonExistentKeys.

@Test
public void testGetWithNonExistentKeys() {
    TimeSeriesStore<String, byte[]> timeSeriesStore = newTimeSeriesStore(new StringSerde("UTF-8"), true);
    timeSeriesStore.put("hello", "world-1".getBytes(), 1L);
    // read from a non-existent key
    List<TimestampedValue<byte[]>> values = readStore(timeSeriesStore, "non-existent-key", 0, Integer.MAX_VALUE);
    Assert.assertEquals(0, values.size());
    // read from an existing key but out of range timestamp
    values = readStore(timeSeriesStore, "hello", 2, Integer.MAX_VALUE);
    Assert.assertEquals(0, values.size());
}
Also used : StringSerde(org.apache.samza.serializers.StringSerde) TimestampedValue(org.apache.samza.util.TimestampedValue) Test(org.junit.Test)

Example 8 with TimestampedValue

use of org.apache.samza.util.TimestampedValue in project samza by apache.

the class PartialJoinOperatorImpl method handleMessageAsync.

@Override
protected CompletionStage<Collection<JM>> handleMessageAsync(M message, MessageCollector collector, TaskCoordinator coordinator) {
    Collection<JM> output = Collections.emptyList();
    try {
        KeyValueStore<K, TimestampedValue<M>> thisState = thisPartialJoinFn.getState();
        KeyValueStore<K, TimestampedValue<OM>> otherState = otherPartialJoinFn.getState();
        K key = thisPartialJoinFn.getKey(message);
        thisState.put(key, new TimestampedValue<>(message, clock.currentTimeMillis()));
        TimestampedValue<OM> otherMessage = otherState.get(key);
        long now = clock.currentTimeMillis();
        if (otherMessage != null && otherMessage.getTimestamp() > now - ttlMs) {
            JM joinResult = thisPartialJoinFn.apply(message, otherMessage.getValue());
            output = Collections.singletonList(joinResult);
        }
    } catch (Exception e) {
        throw new SamzaException("Error handling message in PartialJoinOperatorImpl " + getOpImplId(), e);
    }
    return CompletableFuture.completedFuture(output);
}
Also used : TimestampedValue(org.apache.samza.util.TimestampedValue) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException)

Example 9 with TimestampedValue

use of org.apache.samza.util.TimestampedValue in project samza by apache.

the class TimestampedValueSerde method fromBytes.

@Override
public TimestampedValue<V> fromBytes(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    byte[] vBytes = new byte[bytes.length - TIMESTAMP_BYTES];
    bb.get(vBytes, 0, vBytes.length);
    V v = vSerde.fromBytes(vBytes);
    long ts = bb.getLong();
    return new TimestampedValue<>(v, ts);
}
Also used : TimestampedValue(org.apache.samza.util.TimestampedValue) ByteBuffer(java.nio.ByteBuffer)

Aggregations

TimestampedValue (org.apache.samza.util.TimestampedValue)9 Test (org.junit.Test)7 StringSerde (org.apache.samza.serializers.StringSerde)6 IntegerSerde (org.apache.samza.serializers.IntegerSerde)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Serializable (java.io.Serializable)1 ByteBuffer (java.nio.ByteBuffer)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Partition (org.apache.samza.Partition)1