컨테이너 기반 구성 요소 빌드하기
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
컨테이너 기반 구성 요소는 Docker 컨테이너에서 해당 코드를 실행할 수만 있으면 모든 언어로 작성된 코드를 파이프라인에 통합할 수 있는 유연성을 제공합니다.
TFX 파이프라인을 처음 사용하는 경우, TFX 파이프라인의 핵심 개념에 대해 자세히 알아보세요.
컨테이너 기반 구성 요소 만들기
컨테이너 기반 구성 요소는 컨테이너화된 명령줄 프로그램의 지원을 받습니다. 이미 컨테이너 이미지가 있는 경우에는 create_container_component
함수를 사용하여 입력 및 출력을 선언함으로써 TFX를 사용하여 이 이미지로부터 구성 요소를 만들 수 있습니다. 함수 매개변수는 다음과 같습니다.
- name: 구성 요소의 이름입니다.
- inputs: 입력 이름을 유형에 매핑하는 사전입니다. outputs: 출력 이름을 유형에 매핑하는 사전입니다. parameters: 매개변수 이름을 유형에 매핑하는 사전입니다.
- image: 컨테이너 이미지 이름 및 선택적으로 이미지 태그입니다.
- command: 컨테이너 진입점 명령줄입니다. 쉘 내에서 실행되지 않습니다. 명령줄은 컴파일 시 입력, 출력 또는 매개변수로 대체되는 자리 표시자 객체를 사용할 수 있습니다. 자리 표시자 객체는
tfx.dsl.component.experimental.placeholders
에서 가져올 수 있습니다. Jinja 템플릿은 지원되지 않습니다.
Return value: 파이프라인 내에서 인스턴스화하고 사용할 수 있는 base_component.BaseComponent에서 상속된 Component 클래스입니다.
자리 표시자
입력 또는 출력이 있는 구성 요소의 경우, command
에 런타임 시 실제 데이터로 대체되는 자리 표시자가 필요한 경우가 많습니다. 이를 위해 몇 가지 자리 표시자가 제공됩니다.
InputValuePlaceholder
: 입력 아티팩트의 값에 대한 자리 표시자입니다. 런타임 시 이 자리 표시자는 아티팩트 값의 문자열 표현으로 대체됩니다.
InputUriPlaceholder
: 입력 아티팩트 인수의 URI에 대한 자리 표시자입니다. 런타임 시 이 자리 표시자는 입력 아티팩트 데이터의 URI로 대체됩니다.
OutputUriPlaceholder
: 출력 아티팩트 인수의 URI에 대한 자리 표시자입니다. 런타임 시 이 자리 표시자는 구성 요소가 출력 아티팩트의 데이터를 저장해야 하는 URI로 대체됩니다.
TFX 구성 요소 명령줄 자리 표시자에 대해 자세히 알아보세요.
컨테이너 기반 구성 요소의 예
다음은 데이터를 다운로드, 변환 및 업로드하는 비 Python 구성 요소의 예입니다.
from tfx.dsl.component.experimental import container_component from tfx.dsl.component.experimental import placeholders from tfx.types import standard_artifacts grep_component = container_component.create_container_component( name='FilterWithGrep', inputs={ 'text': standard_artifacts.ExternalArtifact, }, outputs={ 'filtered_text': standard_artifacts.ExternalArtifact, }, parameters={ 'pattern': str, }, # The component code uses gsutil to upload the data to Google Cloud Storage, so the # container image needs to have gsutil installed and configured. image='google/cloud-sdk:278.0.0', command=[ 'sh', '-exc', ''' pattern="$1" text_uri="$3"/data # Adding suffix, because currently the URI are "directories". This will be fixed soon. text_path=$(mktemp) filtered_text_uri="$5"/data # Adding suffix, because currently the URI are "directories". This will be fixed soon. filtered_text_path=$(mktemp) # Getting data into the container gsutil cp "$text_uri" "$text_path" # Running the main code grep "$pattern" "$text_path" >"$filtered_text_path" # Getting data out of the container gsutil cp "$filtered_text_path" "$filtered_text_uri" ''', '--pattern', placeholders.InputValuePlaceholder('pattern'), '--text', placeholders.InputUriPlaceholder('text'), '--filtered-text', placeholders.OutputUriPlaceholder('filtered_text'), ], )
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2021-01-15(UTC)
[null,null,["최종 업데이트: 2021-01-15(UTC)"],[],[],null,["# Building Container-based components\n\nContainer-based components provide the flexibility to integrate code written in\nany language into your pipeline, so long as you can execute that code in a\nDocker container.\n\nIf you are new to TFX pipelines,\n[learn more about the core concepts of TFX pipelines](/tfx/guide/understanding_tfx_pipelines).\n\nCreating a Container-based Component\n------------------------------------\n\nContainer-based components are backed by containerized command-line programs. If\nyou already have a container image, you can use TFX to create a component from\nit by using the\n[`create_container_component` function](https://github.com/tensorflow/tfx/blob/master/tfx/dsl/component/experimental/container_component.py)\nto declare inputs and outputs. Function parameters:\n\n- **name:** The name of the component.\n- **inputs:** A dictionary that maps input names to types. outputs: A dictionary that maps output names to types parameters: A dictionary that maps parameter names to types.\n- **image:** Container image name, and optionally image tag.\n- **command:** Container entrypoint command line. Not executed within a shell. The command line can use placeholder objects that are replaced at compilation time with the input, output, or parameter. The placeholder objects can be imported from [`tfx.dsl.component.experimental.placeholders`](https://github.com/tensorflow/tfx/blob/master/tfx/dsl/component/experimental/placeholders.py). Note that Jinja templates are not supported.\n\n**Return value:** a Component class inheriting from base_component.BaseComponent\nwhich can be instantiated and used inside the pipeline.\n\n### Placeholders\n\nFor a component that has inputs or outputs, the `command` often needs to have\nplaceholders that are replaced with actual data at runtime. Several placeholders\nare provided for this purpose:\n\n- `InputValuePlaceholder`: A placeholder for the value of the input artifact.\n At runtime, this placeholder is replaced with the string representation of\n the artifact's value.\n\n- `InputUriPlaceholder`: A placeholder for the URI of the input artifact\n argument. At runtime, this placeholder is replaced with the URI of the input\n artifact's data.\n\n- `OutputUriPlaceholder`: A placeholder for the URI of the output artifact\n argument. At runtime, this placeholder is replaced with the URI where the\n component should store the output artifact's data.\n\nLearn more about\n[TFX component command-line placeholders](https://github.com/tensorflow/tfx/blob/master/tfx/dsl/component/experimental/placeholders.py).\n\n### Example Container-based Component\n\nThe following is an example of a non-python component that downloads,\ntransforms, and uploads the data: \n\n import tfx.v1 as tfx\n\n grep_component = tfx.dsl.components.create_container_component(\n name='FilterWithGrep',\n inputs={\n 'text': tfx.standard_artifacts.ExternalArtifact,\n },\n outputs={\n 'filtered_text': tfx.standard_artifacts.ExternalArtifact,\n },\n parameters={\n 'pattern': str,\n },\n # The component code uses gsutil to upload the data to Google Cloud Storage, so the\n # container image needs to have gsutil installed and configured.\n image='google/cloud-sdk:278.0.0',\n command=[\n 'sh', '-exc',\n '''\n pattern=\"$1\"\n text_uri=\"$3\"/data # Adding suffix, because currently the URI are \"directories\". This will be fixed soon.\n text_path=$(mktemp)\n filtered_text_uri=\"$5\"/data # Adding suffix, because currently the URI are \"directories\". This will be fixed soon.\n filtered_text_path=$(mktemp)\n\n # Getting data into the container\n gsutil cp \"$text_uri\" \"$text_path\"\n\n # Running the main code\n grep \"$pattern\" \"$text_path\" \u003e\"$filtered_text_path\"\n\n # Getting data out of the container\n gsutil cp \"$filtered_text_path\" \"$filtered_text_uri\"\n ''',\n '--pattern', tfx.dsl.placeholders.InputValuePlaceholder('pattern'),\n '--text', tfx.dsl.placeholders.InputUriPlaceholder('text'),\n '--filtered-text', tfx.dsl.placeholders.OutputUriPlaceholder('filtered_text'),\n ],\n )"]]