Java Code Examples for org.apache.flink.core.io.SimpleVersionedSerializer#serialize()

The following examples show how to use org.apache.flink.core.io.SimpleVersionedSerializer#serialize() . 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: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testResumeAfterMultiplePersist(
		final String persistName,
		final String expectedFinalContents,
		final String firstItemToWrite,
		final String secondItemToWrite,
		final String thirdItemToWrite) throws Exception {

	final Path path = new Path(basePathForTest, "part-0");
	final RecoverableWriter initWriter = getRecoverableWriter();

	final Map<String, RecoverableWriter.ResumeRecoverable> recoverables = new HashMap<>(4);
	try (final RecoverableFsDataOutputStream stream = initWriter.open(path)) {
		recoverables.put(INIT_EMPTY_PERSIST, stream.persist());

		stream.write(bytesOf(firstItemToWrite));

		recoverables.put(INTERM_WITH_STATE_PERSIST, stream.persist());
		recoverables.put(INTERM_WITH_NO_ADDITIONAL_STATE_PERSIST, stream.persist());

		// and write some more data
		stream.write(bytesOf(secondItemToWrite));

		recoverables.put(FINAL_WITH_EXTRA_STATE, stream.persist());
	}

	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> serializer = initWriter.getResumeRecoverableSerializer();
	final byte[] serializedRecoverable = serializer.serialize(recoverables.get(persistName));

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();
	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> deserializer = newWriter.getResumeRecoverableSerializer();
	final RecoverableWriter.ResumeRecoverable recoveredRecoverable =
			deserializer.deserialize(serializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream recoveredStream = newWriter.recover(recoveredRecoverable);
	recoveredStream.write(bytesOf(thirdItemToWrite));
	recoveredStream.closeForCommit().commit();

	Assert.assertEquals(expectedFinalContents, getContentsOfFile(path));
}
 
Example 2
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testResumeAfterMultiplePersist(
		final String persistName,
		final String expectedFinalContents,
		final String firstItemToWrite,
		final String secondItemToWrite,
		final String thirdItemToWrite) throws Exception {

	final Path path = new Path(basePathForTest, "part-0");
	final RecoverableWriter initWriter = getRecoverableWriter();

	final Map<String, RecoverableWriter.ResumeRecoverable> recoverables = new HashMap<>(4);
	try (final RecoverableFsDataOutputStream stream = initWriter.open(path)) {
		recoverables.put(INIT_EMPTY_PERSIST, stream.persist());

		stream.write(bytesOf(firstItemToWrite));

		recoverables.put(INTERM_WITH_STATE_PERSIST, stream.persist());
		recoverables.put(INTERM_WITH_NO_ADDITIONAL_STATE_PERSIST, stream.persist());

		// and write some more data
		stream.write(bytesOf(secondItemToWrite));

		recoverables.put(FINAL_WITH_EXTRA_STATE, stream.persist());
	}

	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> serializer = initWriter.getResumeRecoverableSerializer();
	final byte[] serializedRecoverable = serializer.serialize(recoverables.get(persistName));

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();
	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> deserializer = newWriter.getResumeRecoverableSerializer();
	final RecoverableWriter.ResumeRecoverable recoveredRecoverable =
			deserializer.deserialize(serializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream recoveredStream = newWriter.recover(recoveredRecoverable);
	recoveredStream.write(bytesOf(thirdItemToWrite));
	recoveredStream.closeForCommit().commit();

	Assert.assertEquals(expectedFinalContents, getContentsOfFile(path));
}
 
Example 3
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testResumeAfterMultiplePersist(
		final String persistName,
		final String expectedFinalContents,
		final String firstItemToWrite,
		final String secondItemToWrite,
		final String thirdItemToWrite) throws Exception {

	final Path path = new Path(basePathForTest, "part-0");
	final RecoverableWriter initWriter = getRecoverableWriter();

	final Map<String, RecoverableWriter.ResumeRecoverable> recoverables = new HashMap<>(4);
	try (final RecoverableFsDataOutputStream stream = initWriter.open(path)) {
		recoverables.put(INIT_EMPTY_PERSIST, stream.persist());

		stream.write(bytesOf(firstItemToWrite));

		recoverables.put(INTERM_WITH_STATE_PERSIST, stream.persist());
		recoverables.put(INTERM_WITH_NO_ADDITIONAL_STATE_PERSIST, stream.persist());

		// and write some more data
		stream.write(bytesOf(secondItemToWrite));

		recoverables.put(FINAL_WITH_EXTRA_STATE, stream.persist());
	}

	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> serializer = initWriter.getResumeRecoverableSerializer();
	final byte[] serializedRecoverable = serializer.serialize(recoverables.get(persistName));

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();
	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> deserializer = newWriter.getResumeRecoverableSerializer();
	final RecoverableWriter.ResumeRecoverable recoveredRecoverable =
			deserializer.deserialize(serializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream recoveredStream = newWriter.recover(recoveredRecoverable);
	recoveredStream.write(bytesOf(thirdItemToWrite));
	recoveredStream.closeForCommit().commit();

	Assert.assertEquals(expectedFinalContents, getContentsOfFile(path));
}
 
Example 4
Source File: SourceCoordinatorSerdeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize the assignment by checkpoint ids.
 */
static <SplitT> void writeAssignmentsByCheckpointId(
		Map<Long, Map<Integer, LinkedHashSet<SplitT>>> assignmentByCheckpointIds,
		SimpleVersionedSerializer<SplitT> splitSerializer,
		DataOutputStream out) throws IOException {
	// SplitSerializer version.
	out.writeInt(splitSerializer.getVersion());
	// Num checkpoints.
	out.writeInt(assignmentByCheckpointIds.size());
	for (Map.Entry<Long, Map<Integer, LinkedHashSet<SplitT>>> assignments : assignmentByCheckpointIds.entrySet()) {
		long checkpointId = assignments.getKey();
		out.writeLong(checkpointId);

		int numSubtasks = assignments.getValue().size();
		out.writeInt(numSubtasks);
		for (Map.Entry<Integer, LinkedHashSet<SplitT>> assignment : assignments.getValue().entrySet()) {
			int subtaskId = assignment.getKey();
			out.writeInt(subtaskId);

			int numAssignedSplits = assignment.getValue().size();
			out.writeInt(numAssignedSplits);
			for (SplitT split : assignment.getValue()) {
				byte[] serializedSplit = splitSerializer.serialize(split);
				out.writeInt(serializedSplit.length);
				out.write(serializedSplit);
			}
		}
	}
}