Java Code Examples for org.apache.beam.model.pipeline.v1.RunnerApi#Environment

The following examples show how to use org.apache.beam.model.pipeline.v1.RunnerApi#Environment . 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: InMemoryJobService.java    From beam with Apache License 2.0 6 votes vote down vote up
private RunnerApi.Pipeline resolveDependencies(RunnerApi.Pipeline pipeline, String stagingToken) {
  Map<String, List<RunnerApi.ArtifactInformation>> resolvedDependencies =
      stagingService.getService().getStagedArtifacts(stagingToken);
  Map<String, RunnerApi.Environment> newEnvironments = new HashMap<>();
  for (Map.Entry<String, RunnerApi.Environment> entry :
      pipeline.getComponents().getEnvironmentsMap().entrySet()) {
    if (entry.getValue().getDependenciesCount() > 0 && resolvedDependencies == null) {
      throw new RuntimeException(
          "Artifact dependencies provided but not staged for " + entry.getKey());
    }
    newEnvironments.put(
        entry.getKey(),
        entry.getValue().getDependenciesCount() == 0
            ? entry.getValue()
            : entry
                .getValue()
                .toBuilder()
                .clearDependencies()
                .addAllDependencies(resolvedDependencies.get(entry.getKey()))
                .build());
  }
  RunnerApi.Pipeline.Builder builder = pipeline.toBuilder();
  builder.getComponentsBuilder().clearEnvironments().putAllEnvironments(newEnvironments);
  return builder.build();
}
 
Example 2
Source File: DataflowPipelineTranslator.java    From beam with Apache License 2.0 6 votes vote down vote up
private static byte[] serializeWindowingStrategy(
    WindowingStrategy<?, ?> windowingStrategy, PipelineOptions options) {
  try {
    SdkComponents sdkComponents = SdkComponents.create();

    String workerHarnessContainerImageURL =
        DataflowRunner.getContainerImageForJob(options.as(DataflowPipelineOptions.class));
    RunnerApi.Environment defaultEnvironmentForDataflow =
        Environments.createDockerEnvironment(workerHarnessContainerImageURL);
    sdkComponents.registerEnvironment(defaultEnvironmentForDataflow);

    return WindowingStrategyTranslation.toMessageProto(windowingStrategy, sdkComponents)
        .toByteArray();
  } catch (Exception e) {
    throw new RuntimeException(
        String.format("Unable to format windowing strategy %s as bytes", windowingStrategy), e);
  }
}
 
Example 3
Source File: WorkerCustomSourcesTest.java    From beam with Apache License 2.0 6 votes vote down vote up
static com.google.api.services.dataflow.model.Source translateIOToCloudSource(
    BoundedSource<?> io, DataflowPipelineOptions options) throws Exception {
  DataflowPipelineTranslator translator = DataflowPipelineTranslator.fromOptions(options);
  Pipeline p = Pipeline.create(options);
  p.begin().apply(Read.from(io));

  DataflowRunner runner = DataflowRunner.fromOptions(options);
  SdkComponents sdkComponents = SdkComponents.create();
  RunnerApi.Environment defaultEnvironmentForDataflow =
      Environments.createDockerEnvironment("dummy-image-url");
  sdkComponents.registerEnvironment(defaultEnvironmentForDataflow);
  RunnerApi.Pipeline pipelineProto = PipelineTranslation.toProto(p, sdkComponents, true);

  Job workflow =
      translator
          .translate(p, pipelineProto, sdkComponents, runner, new ArrayList<DataflowPackage>())
          .getJob();
  Step step = workflow.getSteps().get(0);

  return stepToCloudSource(step);
}
 
Example 4
Source File: ProcessPythonEnvironmentManager.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RunnerApi.Environment createEnvironment() throws IOException {
	Map<String, String> env = constructEnvironmentVariables();

	if (dependencyInfo.getRequirementsFilePath().isPresent()) {
		LOG.info("Trying to pip install the Python requirements...");
		PythonEnvironmentManagerUtils.pipInstallRequirements(
			dependencyInfo.getRequirementsFilePath().get(),
			dependencyInfo.getRequirementsCacheDir().orElse(null),
			requirementsDirectory,
			dependencyInfo.getPythonExec(),
			env);
	}
	String runnerScript = PythonEnvironmentManagerUtils.getPythonUdfRunnerScript(dependencyInfo.getPythonExec(), env);

	return Environments.createProcessEnvironment(
		"",
		"",
		runnerScript,
		env);
}
 
Example 5
Source File: PortablePipelineJarCreator.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Stages all dependencies in pipeline into the jar file at outputStream, returning a new pipeline
 * that references these artifacts as classpath artifacts.
 */
@VisibleForTesting
protected Pipeline writeArtifacts(Pipeline pipeline, String jobName) throws IOException {
  Pipeline.Builder result = pipeline.toBuilder();
  for (Map.Entry<String, RunnerApi.Environment> env :
      pipeline.getComponents().getEnvironmentsMap().entrySet()) {
    result
        .getComponentsBuilder()
        .putEnvironments(env.getKey(), writeArtifacts(env.getValue(), jobName));
  }
  return result.build();
}
 
Example 6
Source File: PortablePipelineJarCreator.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Stages all dependencies in environment into the jar file at outputStream, returning a new
 * environment that references these artifacts as classpath artifacts.
 */
private RunnerApi.Environment writeArtifacts(RunnerApi.Environment environment, String jobName)
    throws IOException {
  RunnerApi.Environment.Builder result = environment.toBuilder();
  result.clearDependencies();
  for (RunnerApi.ArtifactInformation artifact : environment.getDependenciesList()) {
    result.addDependencies(writeArtifact(artifact, jobName));
  }
  return result.build();
}
 
Example 7
Source File: DefaultArtifactResolver.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public RunnerApi.Pipeline resolveArtifacts(RunnerApi.Pipeline pipeline) {
  ImmutableMap.Builder<String, RunnerApi.Environment> environmentMapBuilder =
      ImmutableMap.builder();
  for (Map.Entry<String, RunnerApi.Environment> entry :
      pipeline.getComponents().getEnvironmentsMap().entrySet()) {
    List<RunnerApi.ArtifactInformation> resolvedDependencies =
        entry
            .getValue()
            .getDependenciesList()
            .parallelStream()
            .flatMap(resolver)
            .collect(Collectors.toList());
    environmentMapBuilder.put(
        entry.getKey(),
        entry
            .getValue()
            .toBuilder()
            .clearDependencies()
            .addAllDependencies(resolvedDependencies)
            .build());
  }
  return pipeline
      .toBuilder()
      .setComponents(
          pipeline.getComponents().toBuilder().putAllEnvironments(environmentMapBuilder.build()))
      .build();
}
 
Example 8
Source File: External.java    From beam with Apache License 2.0 5 votes vote down vote up
private RunnerApi.Environment resolveArtifacts(
    ArtifactRetrievalServiceGrpc.ArtifactRetrievalServiceBlockingStub retrievalStub,
    RunnerApi.Environment environment)
    throws IOException {
  return environment
      .toBuilder()
      .clearDependencies()
      .addAllDependencies(resolveArtifacts(retrievalStub, environment.getDependenciesList()))
      .build();
}
 
Example 9
Source File: DataflowPipelineTranslatorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private SdkComponents createSdkComponents(PipelineOptions options) {
  SdkComponents sdkComponents = SdkComponents.create();

  String workerHarnessContainerImageURL =
      DataflowRunner.getContainerImageForJob(options.as(DataflowPipelineOptions.class));
  RunnerApi.Environment defaultEnvironmentForDataflow =
      Environments.createDockerEnvironment(workerHarnessContainerImageURL);

  sdkComponents.registerEnvironment(defaultEnvironmentForDataflow);
  return sdkComponents;
}
 
Example 10
Source File: ProcessEnvironment.java    From beam with Apache License 2.0 5 votes vote down vote up
public static RemoteEnvironment create(
    ProcessManager processManager,
    RunnerApi.Environment environment,
    String workerId,
    InstructionRequestHandler instructionHandler) {
  return new ProcessEnvironment(processManager, environment, workerId, instructionHandler);
}
 
Example 11
Source File: ProcessEnvironment.java    From beam with Apache License 2.0 5 votes vote down vote up
private ProcessEnvironment(
    ProcessManager processManager,
    RunnerApi.Environment environment,
    String workerId,
    InstructionRequestHandler instructionHandler) {

  this.processManager = processManager;
  this.environment = environment;
  this.workerId = workerId;
  this.instructionHandler = instructionHandler;
}
 
Example 12
Source File: StaticGrpcProvisionService.java    From beam with Apache License 2.0 4 votes vote down vote up
public void registerEnvironment(String workerId, RunnerApi.Environment environment) {
  environments.put(workerId, environment);
}
 
Example 13
Source File: EnvironmentFactory.java    From beam with Apache License 2.0 4 votes vote down vote up
/** Creates an active {@link Environment} and returns a handle to it. */
RemoteEnvironment createEnvironment(RunnerApi.Environment environment, String workerId)
    throws Exception;
 
Example 14
Source File: ProcessEnvironment.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public RunnerApi.Environment getEnvironment() {
  return environment;
}
 
Example 15
Source File: PythonEnvironmentManager.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Creates the Environment object used in Apache Beam Fn API.
 *
 * @return The Environment object which represents the environment(process, docker, etc) the python worker would run
 *         in.
 */
RunnerApi.Environment createEnvironment() throws Exception;
 
Example 16
Source File: AbstractPythonFunctionRunner.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a specification which specifies the portability Python execution environment.
 * It's used by Beam's portability framework to creates the actual Python execution environment.
 */
protected RunnerApi.Environment createPythonExecutionEnvironment() throws Exception {
	return environmentManager.createEnvironment();
}