Flink源码-8-JobVertex生成JobGraph

JobGraph

通常是由DataStream生成的,然后转换生成的JobGraph
我们可以直接JobVertex--------->JobGraph 来深入理解这个过程

JobVertex

       //生成一个顶点
		final JobVertex sender = new JobVertex("Sender");
		sender.setParallelism(parallelism);
		//设置反射类
		sender.setInvokableClass(TestingAbstractInvokables.Sender.class);


		//生成一个顶点
		final JobVertex receiver = new JobVertex("Receiver");
		receiver.setParallelism(parallelism);
		receiver.setInvokableClass(TestingAbstractInvokables.Receiver.class);

		//生成一个边
		receiver.connectNewDataSetAsInput(sender, DistributionPattern.POINTWISE, ResultPartitionType.PIPELINED);

		//生成一个jobgraph图
		final JobGraph jobGraph = new JobGraph(getClass().getSimpleName(), sender, receiver);

		return jobGraph;

构造具体的Job

Sender类
Receiver类

public class TestingInvokables {

	private TestingInvokables() {
		throw new UnsupportedOperationException(getClass().getSimpleName() + " should not be instantiated.");
	}



	public static class Sender extends AbstractInvokable {

		public Sender(Environment environment) {
			super(environment);
		}

		@Override
		public void invoke() throws Exception {
			//写数据
			final RecordWriter<IntValue> writer = new RecordWriterBuilder<IntValue>().build(getEnvironment().getWriter(0));
			try {
				writer.emit(new IntValue(42));
				writer.emit(new IntValue(1337));
				writer.flushAll();
			} finally {
				writer.clearBuffers();
			}
		}
	}


	public static class Receiver extends AbstractInvokable {

		public Receiver(Environment environment) {
			super(environment);
		}

		@Override
		public void invoke() throws Exception {
			//读数据
			final RecordReader<IntValue> reader = new RecordReader<>(
				getEnvironment().getInputGate(0),
				IntValue.class,
				getEnvironment().getTaskManagerInfo().getTmpDirectories());
			final IntValue i1 = reader.next();
			System.out.println("输出" + i1);
			final IntValue i2 = reader.next();
			System.out.println("输出" + i2);
			final IntValue i3 = reader.next();
			System.out.println("输出" + i3);
			if (i1.getValue() != 42 || i2.getValue() != 1337 || i3 != null) {
				throw new Exception("Wrong data received.");
			}
		}
	}
}

测试类

@Test
	public void testCoLocationConstraintJobExecution() throws Exception {
		final int numSlotsPerTaskExecutor = 1;
		final int numTaskExecutors = 1;
		final int parallelism = numTaskExecutors * numSlotsPerTaskExecutor;
		final JobGraph jobGraph = createNewJobGraph(parallelism);

		final TestingMiniClusterConfiguration miniClusterConfiguration = new TestingMiniClusterConfiguration.Builder()
			.setNumSlotsPerTaskManager(numSlotsPerTaskExecutor)
			.setNumTaskManagers(numTaskExecutors)
			.setLocalCommunication(true)
			.build();


       // 测试任务提交
		try (MiniCluster miniCluster = new MiniCluster(miniClusterConfiguration)) {
			miniCluster.start();
			miniCluster.submitJob(jobGraph).get();
			final CompletableFuture<JobResult> jobResultFuture = miniCluster.requestJobResult(jobGraph.getJobID());
			assertThat(jobResultFuture.get().isSuccess(), is(true));
		}
	}

你可能感兴趣的:(Flink,入门到实践)