Xây dựng các thành phần dựa trên Container
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Các thành phần dựa trên vùng chứa cung cấp tính linh hoạt để tích hợp mã được viết bằng bất kỳ ngôn ngữ nào vào quy trình của bạn, miễn là bạn có thể thực thi mã đó trong vùng chứa Docker.
Nếu bạn chưa quen với quy trình TFX, hãy tìm hiểu thêm về các khái niệm cốt lõi của quy trình TFX .
Tạo thành phần dựa trên vùng chứa
Các thành phần dựa trên vùng chứa được hỗ trợ bởi các chương trình dòng lệnh được chứa trong vùng chứa. Nếu bạn đã có hình ảnh vùng chứa, bạn có thể sử dụng TFX để tạo thành phần từ hình ảnh đó bằng cách sử dụng hàm create_container_component
để khai báo đầu vào và đầu ra. Thông số chức năng:
- name: Tên của thành phần.
- đầu vào: Một từ điển ánh xạ tên đầu vào thành các loại. kết quả đầu ra: Một từ điển ánh xạ tên đầu ra thành các tham số loại: Một từ điển ánh xạ tên tham số thành các loại.
- image: Tên hình ảnh vùng chứa và thẻ hình ảnh tùy chọn.
- lệnh: Dòng lệnh điểm vào vùng chứa. Không được thực thi trong shell. Dòng lệnh có thể sử dụng các đối tượng giữ chỗ được thay thế tại thời điểm biên dịch bằng đầu vào, đầu ra hoặc tham số. Các đối tượng giữ chỗ có thể được nhập từ
tfx.dsl.component.experimental.placeholders
. Lưu ý rằng các mẫu Jinja không được hỗ trợ.
Giá trị trả về: một lớp Thành phần kế thừa từ base_comComponent.BaseComponent có thể được khởi tạo và sử dụng bên trong đường dẫn.
Phần giữ chỗ
Đối với một thành phần có đầu vào hoặc đầu ra, command
thường cần có phần giữ chỗ được thay thế bằng dữ liệu thực tế khi chạy. Một số phần giữ chỗ được cung cấp cho mục đích này:
InputValuePlaceholder
: Trình giữ chỗ cho giá trị của tạo phẩm đầu vào. Trong thời gian chạy, phần giữ chỗ này được thay thế bằng chuỗi biểu diễn giá trị của tạo phẩm.
InputUriPlaceholder
: Trình giữ chỗ cho URI của đối số tạo tác đầu vào. Trong thời gian chạy, phần giữ chỗ này được thay thế bằng URI của dữ liệu của tạo phẩm đầu vào.
OutputUriPlaceholder
: Trình giữ chỗ cho URI của đối số tạo tác đầu ra. Trong thời gian chạy, trình giữ chỗ này được thay thế bằng URI nơi thành phần sẽ lưu trữ dữ liệu của tạo phẩm đầu ra.
Tìm hiểu thêm về phần giữ chỗ dòng lệnh của thành phần TFX .
Ví dụ về Thành phần dựa trên vùng chứa
Sau đây là ví dụ về thành phần không phải Python có chức năng tải xuống, chuyển đổi và tải lên dữ liệu:
import tfx.v1 as tfx
grep_component = tfx.dsl.components.create_container_component(
name='FilterWithGrep',
inputs={
'text': tfx.standard_artifacts.ExternalArtifact,
},
outputs={
'filtered_text': tfx.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', tfx.dsl.placeholders.InputValuePlaceholder('pattern'),
'--text', tfx.dsl.placeholders.InputUriPlaceholder('text'),
'--filtered-text', tfx.dsl.placeholders.OutputUriPlaceholder('filtered_text'),
],
)
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-25 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-25 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 )"]]