예제 #1
0
  public static <IN, OUT> List<OUT> createAndExecute(
      OneInputStreamOperator<IN, OUT> operator, List<IN> inputs) {
    MockContext<IN, OUT> mockContext = new MockContext<IN, OUT>(inputs);
    StreamingRuntimeContext runtimeContext =
        new StreamingRuntimeContext(
            new MockEnvironment("MockTask", 3 * 1024 * 1024, new MockInputSplitProvider(), 1024),
            new ExecutionConfig(),
            null,
            null,
            new HashMap<String, Accumulator<?, ?>>());

    operator.setup(mockContext.output, runtimeContext);
    try {
      operator.open(null);

      StreamRecord<IN> nextRecord;
      for (IN in : inputs) {
        operator.processElement(new StreamRecord<IN>(in));
      }

      operator.close();
    } catch (Exception e) {
      throw new RuntimeException("Cannot invoke operator.", e);
    }

    return mockContext.getOutputs();
  }
예제 #2
0
  public static <IN, OUT, KEY> List<OUT> createAndExecuteForKeyedStream(
      OneInputStreamOperator<IN, OUT> operator,
      List<IN> inputs,
      KeySelector<IN, KEY> keySelector,
      TypeInformation<KEY> keyType) {

    MockContext<IN, OUT> mockContext = new MockContext<IN, OUT>(inputs);

    StreamConfig config = new StreamConfig(new Configuration());
    if (keySelector != null && keyType != null) {
      config.setStateKeySerializer(keyType.createSerializer(new ExecutionConfig()));
      config.setStatePartitioner(keySelector);
    }

    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    final Object lock = new Object();
    final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

    operator.setup(mockTask, config, mockContext.output);
    try {
      operator.open();

      StreamRecord<IN> record = new StreamRecord<IN>(null);
      for (IN in : inputs) {
        record = record.replace(in);
        synchronized (lock) {
          operator.setKeyContextElement(record);
          operator.processElement(record);
        }
      }

      operator.close();
    } catch (Exception e) {
      throw new RuntimeException("Cannot invoke operator.", e);
    } finally {
      timerService.shutdownNow();
    }

    return mockContext.getOutputs();
  }