Exemplo n.º 1
0
  @Override
  protected List<OUT> executeOnCollections(
      List<IN> inputData, RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
    MapFunction<IN, OUT> function = this.userFunction.getUserCodeObject();

    FunctionUtils.setFunctionRuntimeContext(function, ctx);
    FunctionUtils.openFunction(function, this.parameters);

    ArrayList<OUT> result = new ArrayList<OUT>(inputData.size());

    TypeSerializer<IN> inSerializer =
        getOperatorInfo().getInputType().createSerializer(executionConfig);
    TypeSerializer<OUT> outSerializer =
        getOperatorInfo().getOutputType().createSerializer(executionConfig);

    for (IN element : inputData) {
      IN inCopy = inSerializer.copy(element);
      OUT out = function.map(inCopy);
      result.add(outSerializer.copy(out));
    }

    FunctionUtils.closeFunction(function);

    return result;
  }
 /** Close method to be used if the user defined function extends the RichFunction class */
 public void close() {
   isRunning = false;
   collector.close();
   try {
     FunctionUtils.closeFunction(userFunction);
   } catch (Exception e) {
     throw new RuntimeException("Error when closing the function: " + e.getMessage());
   }
 }
 public void setRuntimeContext(RuntimeContext t) {
   FunctionUtils.setFunctionRuntimeContext(userFunction, t);
 }
 /**
  * Open method to be used if the user defined function extends the RichFunction class
  *
  * @param parameters The configuration parameters for the operator
  */
 public void open(Configuration parameters) throws Exception {
   isRunning = true;
   FunctionUtils.openFunction(userFunction, parameters);
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void testDriverInternal(PactDriver driver, Class stubClass) throws Exception {

    this.driver = driver;
    driver.setup(this);

    this.stub = (S) stubClass.newInstance();

    // regular running logic
    this.running = true;
    boolean stubOpen = false;

    try {
      // run the data preparation
      try {
        driver.prepare();
      } catch (Throwable t) {
        throw new Exception("The data preparation caused an error: " + t.getMessage(), t);
      }

      // open stub implementation
      try {
        FunctionUtils.openFunction(this.stub, getTaskConfig().getStubParameters());
        stubOpen = true;
      } catch (Throwable t) {
        throw new Exception(
            "The user defined 'open()' method caused an exception: " + t.getMessage(), t);
      }

      // run the user code
      driver.run();

      // close. We close here such that a regular close throwing an exception marks a task as
      // failed.
      if (this.running) {
        FunctionUtils.closeFunction(this.stub);
        stubOpen = false;
      }

      this.output.close();
    } catch (Exception ex) {
      // close the input, but do not report any exceptions, since we already have another root cause
      if (stubOpen) {
        try {
          FunctionUtils.closeFunction(this.stub);
        } catch (Throwable t) {
        }
      }

      // if resettable driver invoke treardown
      if (this.driver instanceof ResettablePactDriver) {
        final ResettablePactDriver<?, ?> resDriver = (ResettablePactDriver<?, ?>) this.driver;
        try {
          resDriver.teardown();
        } catch (Throwable t) {
          throw new Exception(
              "Error while shutting down an iterative operator: " + t.getMessage(), t);
        }
      }

      // drop exception, if the task was canceled
      if (this.running) {
        throw ex;
      }

    } finally {
      driver.cleanup();
    }
  }