use of org.apache.flink.types.StringValue in project flink by apache.
the class OutputEmitterTest method testMultiKeys.
@Test
public void testMultiKeys() {
final int numberOfChannels = 100;
final int numRecords = 5000;
final TypeComparator<Record> multiComp = new RecordComparatorFactory(new int[] { 0, 1, 3 }, new Class[] { IntValue.class, StringValue.class, DoubleValue.class }).createComparator();
final ChannelSelector<SerializationDelegate<Record>> selector = createChannelSelector(ShipStrategyType.PARTITION_HASH, multiComp, numberOfChannels);
final SerializationDelegate<Record> delegate = new SerializationDelegate<>(new RecordSerializerFactory().getSerializer());
int[] hits = new int[numberOfChannels];
for (int i = 0; i < numRecords; i++) {
Record record = new Record(4);
record.setField(0, new IntValue(i));
record.setField(1, new StringValue("AB" + i + "CD" + i));
record.setField(3, new DoubleValue(i * 3.141d));
delegate.setInstance(record);
int channel = selector.selectChannel(delegate);
hits[channel]++;
}
int totalHitCount = 0;
for (int hit : hits) {
assertTrue(hit > 0);
totalHitCount += hit;
}
assertTrue(totalHitCount == numRecords);
}
use of org.apache.flink.types.StringValue in project flink by apache.
the class OutputEmitterTest method getSelectedChannelsHitCount.
private int[] getSelectedChannelsHitCount(ChannelSelector<SerializationDelegate<Record>> selector, SerializationDelegate<Record> delegate, Enum recordType, int numRecords, int numberOfChannels) {
int[] hits = new int[numberOfChannels];
Value value;
for (int i = 0; i < numRecords; i++) {
if (recordType == RecordType.INTEGER) {
value = new IntValue(i);
} else {
value = new StringValue(i + "");
}
Record record = new Record(value);
delegate.setInstance(record);
int channel = selector.selectChannel(delegate);
hits[channel]++;
}
return hits;
}
use of org.apache.flink.types.StringValue in project flink by apache.
the class NonReusingKeyGroupedIteratorTest method testHasNextDoesNotOverweiteCurrentRecord.
@Test
public void testHasNextDoesNotOverweiteCurrentRecord() throws Exception {
try {
Iterator<Record> valsIter = null;
Record rec = null;
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
valsIter = this.psi.getValues();
Assert.assertNotNull("Returned Iterator must not be null", valsIter);
Assert.assertTrue("KeyGroupedIterator's value iterator must have another value.", valsIter.hasNext());
rec = valsIter.next();
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 1, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("A"), rec.getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator must have another value.", valsIter.hasNext());
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 1, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("A"), rec.getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator's value iterator must not have another value.", valsIter.hasNext());
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
valsIter = this.psi.getValues();
Assert.assertNotNull("Returned Iterator must not be null", valsIter);
Assert.assertTrue("KeyGroupedIterator's value iterator must have another value.", valsIter.hasNext());
rec = valsIter.next();
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 2, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("B"), rec.getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator must have another value.", valsIter.hasNext());
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 2, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("B"), rec.getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator's value iterator must not have another value.", valsIter.hasNext());
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
valsIter = this.psi.getValues();
Assert.assertNotNull("Returned Iterator must not be null", valsIter);
Assert.assertTrue("KeyGroupedIterator's value iterator must have another value.", valsIter.hasNext());
rec = valsIter.next();
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("C"), rec.getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator's value iterator must have another value.", valsIter.hasNext());
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("C"), rec.getField(1, StringValue.class));
rec = valsIter.next();
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("D"), rec.getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator's value iterator must have another value.", valsIter.hasNext());
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("D"), rec.getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator's value iterator must have another value.", valsIter.hasNext());
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, rec.getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("D"), rec.getField(1, StringValue.class));
} catch (Exception e) {
e.printStackTrace();
Assert.fail("The test encountered an unexpected exception.");
}
}
use of org.apache.flink.types.StringValue in project flink by apache.
the class FileChannelStreamsTest method testCloseAndDeleteOutputView.
@Test
public void testCloseAndDeleteOutputView() {
try (IOManager ioManager = new IOManagerAsync()) {
MemoryManager memMan = MemoryManagerBuilder.newBuilder().build();
List<MemorySegment> memory = new ArrayList<MemorySegment>();
memMan.allocatePages(new DummyInvokable(), memory, 4);
FileIOChannel.ID channel = ioManager.createChannel();
BlockChannelWriter<MemorySegment> writer = ioManager.createBlockChannelWriter(channel);
FileChannelOutputView out = new FileChannelOutputView(writer, memMan, memory, memMan.getPageSize());
new StringValue("Some test text").write(out);
// close for the first time, make sure all memory returns
out.close();
assertTrue(memMan.verifyEmpty());
// close again, should not cause an exception
out.close();
// delete, make sure file is removed
out.closeAndDelete();
assertFalse(new File(channel.getPath()).exists());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.apache.flink.types.StringValue in project flink by apache.
the class ReusingKeyGroupedIteratorTest method testFullIterationThroughAllValues.
@Test
public void testFullIterationThroughAllValues() throws IOException {
try {
// Key 1, Value A
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
Assert.assertTrue(hasIterator(this.psi.getValues()));
Assert.assertFalse(hasIterator(this.psi.getValues()));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(1))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 1, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("A"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator must not have another value.", this.psi.getValues().hasNext());
// Key 2, Value B
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
Assert.assertTrue(hasIterator(this.psi.getValues()));
Assert.assertFalse(hasIterator(this.psi.getValues()));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(2))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 2, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("B"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertFalse("KeyGroupedIterator must not have another value.", this.psi.getValues().hasNext());
// Key 3, Values C, D
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
Assert.assertTrue(hasIterator(this.psi.getValues()));
Assert.assertFalse(hasIterator(this.psi.getValues()));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(3))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("C"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(3))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("D"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(3))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, this.psi.getCurrent().getField(0, IntValue.class).getValue());
try {
this.psi.getValues().next();
Assert.fail("A new KeyGroupedIterator must not have any value available and hence throw an exception on next().");
} catch (NoSuchElementException nseex) {
}
Assert.assertFalse("KeyGroupedIterator must not have another value.", this.psi.getValues().hasNext());
try {
this.psi.getValues().next();
Assert.fail("A new KeyGroupedIterator must not have any value available and hence throw an exception on next().");
} catch (NoSuchElementException nseex) {
}
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(3))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 3, this.psi.getCurrent().getField(0, IntValue.class).getValue());
// Key 4, Values E, F, G
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
Assert.assertTrue(hasIterator(this.psi.getValues()));
Assert.assertFalse(hasIterator(this.psi.getValues()));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(4))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 4, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("E"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(4))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 4, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("F"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(4))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 4, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("G"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(4))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 4, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertFalse("KeyGroupedIterator must not have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(4))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 4, this.psi.getCurrent().getField(0, IntValue.class).getValue());
// Key 5, Values H, I, J, K, L
Assert.assertTrue("KeyGroupedIterator must have another key.", this.psi.nextKey());
Assert.assertTrue(hasIterator(this.psi.getValues()));
Assert.assertFalse(hasIterator(this.psi.getValues()));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("H"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("I"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("J"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("K"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator must have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
Assert.assertEquals("KeyGroupedIterator returned a wrong value.", new StringValue("L"), this.psi.getValues().next().getField(1, StringValue.class));
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
try {
this.psi.getValues().next();
Assert.fail("A new KeyGroupedIterator must not have any value available and hence throw an exception on next().");
} catch (NoSuchElementException nseex) {
}
Assert.assertFalse("KeyGroupedIterator must not have another value.", this.psi.getValues().hasNext());
Assert.assertTrue("KeyGroupedIterator returned a wrong key.", this.psi.getComparatorWithCurrentReference().equalToReference(new Record(new IntValue(5))));
Assert.assertEquals("KeyGroupedIterator returned a wrong key.", 5, this.psi.getCurrent().getField(0, IntValue.class).getValue());
try {
this.psi.getValues().next();
Assert.fail("A new KeyGroupedIterator must not have any value available and hence throw an exception on next().");
} catch (NoSuchElementException nseex) {
}
Assert.assertFalse("KeyGroupedIterator must not have another key.", this.psi.nextKey());
Assert.assertFalse("KeyGroupedIterator must not have another key.", this.psi.nextKey());
Assert.assertNull(this.psi.getValues());
} catch (Exception e) {
e.printStackTrace();
Assert.fail("The test encountered an unexpected exception.");
}
}
Aggregations