การสร้างส่วนประกอบที่ใช้คอนเทนเนอร์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ส่วนประกอบที่ใช้คอนเทนเนอร์ให้ความยืดหยุ่นในการรวมโค้ดที่เขียนในภาษาใดๆ ลงในไปป์ไลน์ของคุณ ตราบใดที่คุณสามารถรันโค้ดนั้นในคอนเทนเนอร์ Docker ได้
หากคุณยังใหม่กับไปป์ไลน์ TFX เรียนรู้เพิ่มเติมเกี่ยวกับแนวคิดหลักของไปป์ไลน์ TFX
การสร้างส่วนประกอบตามคอนเทนเนอร์
ส่วนประกอบแบบคอนเทนเนอร์ได้รับการสนับสนุนโดยโปรแกรมบรรทัดคำสั่งแบบคอนเทนเนอร์ หากคุณมีอิมเมจคอนเทนเนอร์อยู่แล้ว คุณสามารถใช้ TFX เพื่อสร้างส่วนประกอบจากอิมเมจคอนเทนเนอร์ได้โดยใช้ ฟังก์ชัน create_container_component
เพื่อประกาศอินพุตและเอาต์พุต พารามิเตอร์ฟังก์ชัน:
- name: ชื่อของส่วนประกอบ
- อินพุต: พจนานุกรมที่จับคู่ชื่ออินพุตกับประเภท เอาท์พุต: พจนานุกรมที่จับคู่ชื่อเอาต์พุตกับประเภทพารามิเตอร์: พจนานุกรมที่จับคู่ชื่อพารามิเตอร์เป็นประเภท
- รูปภาพ: ชื่อรูปภาพคอนเทนเนอร์ และแท็กรูปภาพ (ไม่บังคับ)
- คำสั่ง: บรรทัดคำสั่งจุดเข้าคอนเทนเนอร์ ไม่ได้ดำเนินการภายในเชลล์ บรรทัดคำสั่งสามารถใช้ออบเจ็กต์ตัวยึดตำแหน่งที่ถูกแทนที่ในเวลาคอมไพล์ด้วยอินพุต เอาท์พุต หรือพารามิเตอร์ สามารถนำเข้าวัตถุตัวยึดตำแหน่งได้จาก
tfx.dsl.component.experimental.placeholders
โปรดทราบว่าไม่รองรับเทมเพลต Jinja
ค่าที่ส่งคืน: คลาส Component ที่สืบทอดมาจาก base_component.BaseComponent ซึ่งสามารถสร้างอินสแตนซ์และใช้ภายในไปป์ไลน์ได้
ตัวยึดตำแหน่ง
สำหรับส่วนประกอบที่มีอินพุตหรือเอาต์พุต command
มักจะต้องมีตัวยึดตำแหน่งที่ถูกแทนที่ด้วยข้อมูลจริงในขณะรันไทม์ ตัวยึดตำแหน่งหลายรายการมีไว้เพื่อจุดประสงค์นี้:
InputValuePlaceholder
: ตัวยึดตำแหน่งสำหรับค่าของส่วนอินพุต ณ รันไทม์ ตัวยึดนี้จะถูกแทนที่ด้วยการแสดงสตริงของค่าของส่วน
InputUriPlaceholder
: ตัวยึดสำหรับ URI ของอาร์กิวเมนต์สิ่งประดิษฐ์อินพุต ณ รันไทม์ ตัวยึดตำแหน่งนี้จะถูกแทนที่ด้วย URI ของข้อมูลของอินพุต
OutputUriPlaceholder
: ตัวยึดตำแหน่งสำหรับ URI ของอาร์กิวเมนต์ส่วนเอาต์พุต ณ รันไทม์ ตัวยึดตำแหน่งนี้จะถูกแทนที่ด้วย URI โดยที่คอมโพเนนต์ควรจัดเก็บข้อมูลของส่วนเอาต์พุต
เรียนรู้เพิ่มเติมเกี่ยวกับ ตัวยึดบรรทัดคำสั่งคอมโพเนนต์ TFX
ตัวอย่างส่วนประกอบที่ใช้คอนเทนเนอร์
ต่อไปนี้เป็นตัวอย่างขององค์ประกอบที่ไม่ใช่หลามที่ดาวน์โหลด แปลง และอัปโหลดข้อมูล:
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'),
],
)
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 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 )"]]