use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap in project beam by apache.
the class IsmSideInputReaderTest method testSingletonMultimapInWindow.
@Test
public void testSingletonMultimapInWindow() throws Exception {
IntervalWindow firstWindow = new IntervalWindow(new Instant(0L), new Instant(100L));
IntervalWindow secondWindow = new IntervalWindow(new Instant(50L), new Instant(150L));
IntervalWindow emptyWindow = new IntervalWindow(new Instant(75L), new Instant(175L));
// Collection is iterable, and this is immutable
@SuppressWarnings({ "unchecked", "rawtypes" }) final Map<IntervalWindow, WindowedValue<Map<String, Iterable<Long>>>> elements = ImmutableMap.<IntervalWindow, WindowedValue<Map<String, Iterable<Long>>>>builder().put(firstWindow, WindowedValue.of((Map) ImmutableListMultimap.<String, Long>builder().put("foo", 0L).put("foo", 2L).put("bar", -1L).build().asMap(), new Instant(7), firstWindow, PaneInfo.NO_FIRING)).put(secondWindow, WindowedValue.of((Map) ImmutableListMultimap.<String, Long>builder().put("bar", -1L).put("baz", 1L).put("baz", 3L).build().asMap(), new Instant(53L), secondWindow, PaneInfo.NO_FIRING)).build();
StringUtf8Coder strCoder = StringUtf8Coder.of();
Coder<Map<String, Iterable<Long>>> mapCoder = MapCoder.of(strCoder, IterableCoder.of(VarLongCoder.of()));
final PCollectionView<Map<String, Iterable<Long>>> view = Pipeline.create().apply(Create.empty(KvCoder.of(StringUtf8Coder.of(), VarLongCoder.of()))).apply(Window.into(FixedWindows.of(Duration.millis(100L)))).apply(View.asMultimap());
IsmRecordCoder<WindowedValue<Map<String, Iterable<Long>>>> recordCoder = IsmRecordCoder.of(1, 0, ImmutableList.<Coder<?>>of(INTERVAL_WINDOW_CODER), WindowedValue.getFullCoder(mapCoder, INTERVAL_WINDOW_CODER));
final Source source = initInputFile(fromValues(elements.values()), recordCoder);
final IsmSideInputReader reader = sideInputReader(view.getTagInternal().getId(), source);
List<Callable<Map<BoundedWindow, Map<String, Iterable<Long>>>>> tasks = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
tasks.add(() -> {
// Store a strong reference to the returned value so that the logical reference
// cache is not cleared for this test.
Map<String, Iterable<Long>> value = reader.get(view, firstWindow);
assertEquals(elements.get(firstWindow).getValue(), value);
// Assert that the same value reference was returned showing that it was cached.
assertSame(value, reader.get(view, firstWindow));
Map<String, Iterable<Long>> secondValue = reader.get(view, secondWindow);
assertEquals(elements.get(secondWindow).getValue(), secondValue);
// Assert that the same value reference was returned showing that it was cached.
assertSame(secondValue, reader.get(view, secondWindow));
Map<String, Iterable<Long>> emptyValue = reader.get(view, emptyWindow);
assertThat(emptyValue.keySet(), empty());
Map<BoundedWindow, Map<String, Iterable<Long>>> result = ImmutableMap.<BoundedWindow, Map<String, Iterable<Long>>>builder().put(firstWindow, value).put(secondWindow, secondValue).put(emptyWindow, emptyValue).build();
return result;
});
}
List<Future<Map<BoundedWindow, Map<String, Iterable<Long>>>>> results = pipelineOptions.getExecutorService().invokeAll(tasks);
Map<BoundedWindow, Map<String, Iterable<Long>>> value = results.get(0).get();
for (Future<Map<BoundedWindow, Map<String, Iterable<Long>>>> result : results) {
assertEquals(value, result.get());
for (Map.Entry<BoundedWindow, Map<String, Iterable<Long>>> entry : result.get().entrySet()) {
assertSame(value.get(entry.getKey()), entry.getValue());
}
}
}
use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap in project beam by apache.
the class IsmSideInputReaderTest method testSingletonMapInWindow.
@Test
public void testSingletonMapInWindow() throws Exception {
IntervalWindow firstWindow = new IntervalWindow(new Instant(0L), new Instant(100L));
IntervalWindow secondWindow = new IntervalWindow(new Instant(50L), new Instant(150L));
IntervalWindow emptyWindow = new IntervalWindow(new Instant(75L), new Instant(175L));
final Map<IntervalWindow, WindowedValue<Map<String, Long>>> elements = ImmutableMap.<IntervalWindow, WindowedValue<Map<String, Long>>>builder().put(firstWindow, WindowedValue.of(ImmutableMap.<String, Long>builder().put("foo", 0L).put("bar", -1L).build(), new Instant(7), firstWindow, PaneInfo.NO_FIRING)).put(secondWindow, WindowedValue.of(ImmutableMap.<String, Long>builder().put("bar", -1L).put("baz", 1L).build(), new Instant(53L), secondWindow, PaneInfo.NO_FIRING)).build();
Coder<Map<String, Long>> mapCoder = MapCoder.of(StringUtf8Coder.of(), VarLongCoder.of());
final PCollectionView<Map<String, Long>> view = Pipeline.create().apply(Create.empty(KvCoder.of(StringUtf8Coder.of(), VarLongCoder.of()))).apply(Window.into(SlidingWindows.of(Duration.millis(100L)).every(Duration.millis(50L)))).apply(View.asMap());
IsmRecordCoder<WindowedValue<Map<String, Long>>> recordCoder = IsmRecordCoder.of(1, 0, ImmutableList.<Coder<?>>of(INTERVAL_WINDOW_CODER), WindowedValue.getFullCoder(mapCoder, INTERVAL_WINDOW_CODER));
final Source source = initInputFile(fromValues(elements.values()), recordCoder);
final IsmSideInputReader reader = sideInputReader(view.getTagInternal().getId(), source);
List<Callable<Map<BoundedWindow, Map<String, Long>>>> tasks = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
tasks.add(() -> {
// Store a strong reference to the returned value so that the logical reference
// cache is not cleared for this test.
Map<String, Long> value = reader.get(view, firstWindow);
assertEquals(elements.get(firstWindow).getValue(), value);
// Assert that the same value reference was returned showing that it was cached.
assertSame(value, reader.get(view, firstWindow));
Map<String, Long> secondValue = reader.get(view, secondWindow);
assertEquals(elements.get(secondWindow).getValue(), secondValue);
// Assert that the same value reference was returned showing that it was cached.
assertSame(secondValue, reader.get(view, secondWindow));
Map<String, Long> emptyValue = reader.get(view, emptyWindow);
assertThat(emptyValue.keySet(), empty());
Map<BoundedWindow, Map<String, Long>> result = ImmutableMap.<BoundedWindow, Map<String, Long>>builder().put(firstWindow, value).put(secondWindow, secondValue).put(emptyWindow, emptyValue).build();
return result;
});
}
List<Future<Map<BoundedWindow, Map<String, Long>>>> results = pipelineOptions.getExecutorService().invokeAll(tasks);
// Assert that all threads got back the same reference
Map<BoundedWindow, Map<String, Long>> value = results.get(0).get();
for (Future<Map<BoundedWindow, Map<String, Long>>> result : results) {
assertEquals(value, result.get());
for (Map.Entry<BoundedWindow, Map<String, Long>> entry : result.get().entrySet()) {
assertSame(value.get(entry.getKey()), entry.getValue());
}
}
}
use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap in project beam by apache.
the class IsmSideInputReaderTest method testMultimapInWindow.
@Test
public void testMultimapInWindow() throws Exception {
// Note that we purposely use byte[]s as keys to force structural equality testing
// versus using java equality testing.
Coder<WindowedValue<Long>> valueCoder = WindowedValue.getFullCoder(VarLongCoder.of(), INTERVAL_WINDOW_CODER);
final ListMultimap<byte[], WindowedValue<Long>> firstWindow = ImmutableListMultimap.<byte[], WindowedValue<Long>>builder().put(new byte[] { 0x00 }, valueInIntervalWindow(12L, 10)).put(new byte[] { 0x01 }, valueInIntervalWindow(22L, 10)).put(new byte[] { 0x02 }, valueInIntervalWindow(32L, 10)).build();
final ListMultimap<byte[], WindowedValue<Long>> secondWindow = ImmutableListMultimap.<byte[], WindowedValue<Long>>builder().put(new byte[] { 0x00 }, valueInIntervalWindow(42L, 20)).put(new byte[] { 0x03 }, valueInIntervalWindow(52L, 20)).put(new byte[] { 0x02 }, valueInIntervalWindow(62L, 20)).build();
final ListMultimap<byte[], WindowedValue<Long>> thirdWindow = ImmutableListMultimap.<byte[], WindowedValue<Long>>builder().put(new byte[] { 0x02 }, valueInIntervalWindow(72L, 30)).put(new byte[] { 0x04 }, valueInIntervalWindow(82L, 30)).put(new byte[] { 0x05 }, valueInIntervalWindow(92L, 30)).build();
final PCollectionView<Map<byte[], Iterable<Long>>> view = Pipeline.create().apply(Create.empty(KvCoder.of(ByteArrayCoder.of(), VarLongCoder.of()))).apply(Window.into(FixedWindows.of(Duration.millis(10)))).apply(View.asMultimap());
IsmRecordCoder<WindowedValue<Long>> ismCoder = IsmRecordCoder.of(1, 2, ImmutableList.of(MetadataKeyCoder.of(ByteArrayCoder.of()), INTERVAL_WINDOW_CODER, BigEndianLongCoder.of()), valueCoder);
Multimap<Integer, IsmRecord<WindowedValue<Long>>> elementsPerShard = forMap(ismCoder, firstWindow);
elementsPerShard.putAll(forMap(ismCoder, secondWindow));
elementsPerShard.putAll(forMap(ismCoder, thirdWindow));
List<IsmRecord<WindowedValue<Long>>> firstElements = new ArrayList<>();
List<IsmRecord<WindowedValue<Long>>> secondElements = new ArrayList<>();
for (Map.Entry<Integer, Collection<IsmRecord<WindowedValue<Long>>>> entry : elementsPerShard.asMap().entrySet()) {
if (entry.getKey() % 2 == 0) {
firstElements.addAll(entry.getValue());
} else {
secondElements.addAll(entry.getValue());
}
}
// Ensure that each file will have some records.
checkState(!firstElements.isEmpty());
checkState(!secondElements.isEmpty());
Source sourceA = initInputFile(firstElements, ismCoder);
Source sourceB = initInputFile(secondElements, ismCoder);
List<IsmRecord<WindowedValue<Long>>> firstWindowMapMetadata = forMapMetadata(ByteArrayCoder.of(), firstWindow.keySet(), intervalWindow(10));
List<IsmRecord<WindowedValue<Long>>> secondWindowMapMetadata = forMapMetadata(ByteArrayCoder.of(), secondWindow.keySet(), intervalWindow(20));
List<IsmRecord<WindowedValue<Long>>> thirdWindowMapMetadata = forMapMetadata(ByteArrayCoder.of(), thirdWindow.keySet(), intervalWindow(30));
Source sourceMetaA = initInputFile(firstWindowMapMetadata, ismCoder);
Source sourceMetaB = initInputFile(concat(secondWindowMapMetadata, thirdWindowMapMetadata), ismCoder);
final IsmSideInputReader reader = sideInputReader(view.getTagInternal().getId(), sourceA, sourceB, sourceMetaA, sourceMetaB);
List<Callable<Map<BoundedWindow, Map<byte[], Iterable<Long>>>>> tasks = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
tasks.add(() -> {
// Store a strong reference to the returned value so that the logical reference
// cache is not cleared for this test.
Map<byte[], Iterable<Long>> firstValues = reader.get(view, intervalWindow(10));
Map<byte[], Iterable<Long>> secondValues = reader.get(view, intervalWindow(20));
Map<byte[], Iterable<Long>> thirdValues = reader.get(view, intervalWindow(30));
verifyMap(Maps.transformValues(firstWindow.asMap(), new TransformForMultimap<Long>()), firstValues, new ComparatorForMultimap<Long>());
verifyMap(Maps.transformValues(secondWindow.asMap(), new TransformForMultimap<Long>()), secondValues, new ComparatorForMultimap<Long>());
verifyMap(Maps.transformValues(thirdWindow.asMap(), new TransformForMultimap<Long>()), thirdValues, new ComparatorForMultimap<Long>());
// Assert that the same value reference was returned showing that it was cached.
assertSame(firstValues, reader.get(view, intervalWindow(10)));
assertSame(secondValues, reader.get(view, intervalWindow(20)));
assertSame(thirdValues, reader.get(view, intervalWindow(30)));
// Also verify when requesting a window that is not part of the side input
assertEquals(Collections.EMPTY_MAP, reader.get(view, intervalWindow(40)));
return ImmutableMap.<BoundedWindow, Map<byte[], Iterable<Long>>>of(intervalWindow(10), firstValues, intervalWindow(20), secondValues, intervalWindow(30), thirdValues);
});
}
List<Future<Map<BoundedWindow, Map<byte[], Iterable<Long>>>>> results = pipelineOptions.getExecutorService().invokeAll(tasks);
Map<BoundedWindow, Map<byte[], Iterable<Long>>> value = results.get(0).get();
// Assert that all threads got back the same reference
for (Future<Map<BoundedWindow, Map<byte[], Iterable<Long>>>> result : results) {
assertEquals(value, result.get());
for (Map.Entry<BoundedWindow, Map<byte[], Iterable<Long>>> entry : result.get().entrySet()) {
assertSame(value.get(entry.getKey()), entry.getValue());
}
}
}
use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap in project beam by apache.
the class IsmSideInputReaderTest method testSingletonMap.
@Test
public void testSingletonMap() throws Exception {
final WindowedValue<Map<String, Long>> element = valueInGlobalWindow(ImmutableMap.<String, Long>builder().put("foo", 0L).put("bar", -1L).build());
Coder<Map<String, Long>> mapCoder = MapCoder.of(StringUtf8Coder.of(), VarLongCoder.of());
final PCollectionView<Map<String, Long>> view = Pipeline.create().apply(Create.empty(KvCoder.of(StringUtf8Coder.of(), VarLongCoder.of()))).apply(View.asMap());
IsmRecordCoder<WindowedValue<Map<String, Long>>> recordCoder = IsmRecordCoder.of(1, 0, ImmutableList.<Coder<?>>of(GLOBAL_WINDOW_CODER), WindowedValue.getFullCoder(mapCoder, GLOBAL_WINDOW_CODER));
final Source source = initInputFile(fromValues(Arrays.asList(element)), recordCoder);
final IsmSideInputReader reader = sideInputReader(view.getTagInternal().getId(), source);
List<Callable<Map<String, Long>>> tasks = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
tasks.add(() -> {
// Store a strong reference to the returned value so that the logical reference
// cache is not cleared for this test.
Map<String, Long> value = reader.get(view, GlobalWindow.INSTANCE);
assertEquals(element.getValue(), value);
// Assert that the same value reference was returned showing that it was cached.
assertSame(value, reader.get(view, GlobalWindow.INSTANCE));
return value;
});
}
List<Future<Map<String, Long>>> results = pipelineOptions.getExecutorService().invokeAll(tasks);
// Assert that all threads got back the same reference
Map<String, Long> value = results.get(0).get();
for (Future<Map<String, Long>> result : results) {
assertSame(value, result.get());
}
}
Aggregations