---
metadata:
  - name: generator
    content: Diplodoc Platform v5.64.2
alternate:
  - https://ytsaurus.tech/docs/en/flow/python/examples/static_table_join.md
  - https://ytsaurus.tech/docs/ru/flow/python/examples/static_table_join.md
  - href: https://ytsaurus.tech/docs/ru/flow/python/examples/static_table_join.md
    type: text/markdown
    title: Markdown version
  - href: https://ytsaurus.tech/docs/ru/llms.txt?revision=c426f39927f2807c73168512150ef54f4b741847
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://ytsaurus.tech/docs/ru/llms.txt?revision=c426f39927f2807c73168512150ef54f4b741847

<!-- source: ru/_includes/flow/python/examples/static_table_join.md -->
# Static Table Join в YTsaurus Flow (Python)

Пайплайн загружает статический справочник во внешний стейт и обогащает события из очереди по общему ключу.

[Исходники](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/python/static_table_join)

## Структура пайплайна {#structure}

- `reference_reader` читает статическую таблицу;
- `reference_loader` нормализует значения и записывает их в `/reference_state`;
- `event_reader` читает события из очереди;
- `enricher` выполняет read-only lookup через joiner `/reference_state` и пишет успешные совпадения в `enriched`.

Менеджер и joiner ссылаются на одну динамическую таблицу стейта. Её ключи должны совпадать с `group_by_schema` компьютейшенов. См. [спеку пайплайна](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/python/static_table_join/pipeline.yson).

## Загрузка и join {#implementation}

Загрузчик получает стейт через `ctx.external_state(...)` и сохраняет нормализованное значение. Обогатитель вызывает `ctx.joined_external_state(...)`; если `normalized_name` отсутствует, он не публикует результат.

```python
class ReferenceLoader(RowFunction):
    """Trims and lowercases the reference name, storing it in external state."""

    def on_message(self, message, output, ctx):
        name = message.payload["name"]
        normalized_name = name.strip().lower()

        state = ctx.external_state("/reference_state", message)
        builder = state.to_builder()
        builder.set("normalized_name", normalized_name)
        state.set(builder.finish())

```

```python
class Enricher(RowFunction):
    """Joins each event against the reference state and emits the enriched row."""

    def on_message(self, message, output, ctx):
        state = ctx.joined_external_state("/reference_state", message)
        normalized_name = state.get("normalized_name")
        if normalized_name is None:
            return

        builder = ctx.message_builder("enriched")
        builder.set("key", message.payload["key"])
        builder.set("name", normalized_name)
        output.add_message(builder.finish())

```

## См. также

- [External State (Python)](https://ytsaurus.tech/docs/ru/flow/python/external-state.md)
- [Пример External State Join](https://ytsaurus.tech/docs/ru/flow/python/examples/external_state_join.md)
<!-- endsource: ru/_includes/flow/python/examples/static_table_join.md -->
