External State Join in YTsaurus Flow (Go)

An example of a pipeline that enriches events from a queue with data from a read-only external state. The reference table is attached through TSimpleExternalStateJoiner, and the Go computation looks a row up by the event key.

Source code

Structure

  • event_reader — a native queue source that publishes the event stream.
  • lookup_join (lookupJoin) — a Go transform computation grouped by key. It reads /reference through external_state_joiners and publishes the enriched message to the enriched stream.
  • A queue sink stores the messages from enriched.

The joiner path in the spec may point to a Cypress link. This lets you switch the reference data to a new version of the table atomically, without restarting the pipeline.

main.go

The entry point registers the schemas of the input and output streams, adds the computation, and starts the pipeline.

Source code: main.go

type eventMessage struct {
	flow.YSONMessage
	Key uint64 `yson:"key"`
}

type enrichedMessage struct {
	flow.YSONMessage
	Key  uint64 `yson:"key"`
	Name string `yson:"name"`
}

func main() {
	pipeline := flow.NewPipeline()
	pipeline.AddStreams(
		flow.NewYSONStream[eventMessage]("event"),
		flow.NewYSONStream[enrichedMessage](enrichedStreamID),
	)
	pipeline.Add(flow.NewRowComputation("lookup_join", &lookupJoin{}))

	if err := pipeline.Run(); err != nil {
		fmt.Fprintf(os.Stderr, "external_state_join: %v\n", err)
		os.Exit(1)
	}
}

lookup_join.go

The computation decodes the typed input message, reads the joined state row, and creates a typed output message.

Source code: lookup_join.go

type lookupJoin struct{}

var _ flow.RowFunction = (*lookupJoin)(nil)

func (*lookupJoin) OnMessage(
	ctx context.Context,
	rt flow.Runtime,
	msg flow.ExtendedMessage,
	out flow.OutputCollector,
) error {
	var event eventMessage
	if err := msg.ConvertTo(&event); err != nil {
		return err
	}

	name, ok, err := referenceName(rt, msg)
	if err != nil || !ok {
		return err
	}

	enriched := flow.NewYSONMessage[enrichedMessage](enrichedStreamID)
	enriched.Key = event.Key
	enriched.Name = name
	encoded, err := flow.ConvertFrom(rt, enriched)
	if err != nil {
		return err
	}
	out.AddMessage(encoded)
	return nil
}

func referenceName(rt flow.Runtime, msg flow.ExtendedMessage) (string, bool, error) {
	reference, err := flow.OpenJoinedExternalState(rt, referenceStateName, msg)
	if errors.Is(err, flow.ErrStateNotRead) {
		return "", false, nil
	}
	if err != nil {
		return "", false, err
	}

	var state referenceState
	exists, err := reference.ConvertTo(&state)
	if err != nil || !exists || state.Name == nil {
		return "", false, err
	}
	return *state.Name, true, nil
}

Key patterns

  • flow.OpenJoinedExternalState(rt, referenceStateName, msg) opens the read-only state for the key of the current message.
  • flow.ErrStateNotRead means that there is no joined row for the key; in this case the example publishes no result.
  • ConvertTo stays at the SDK boundary: the business logic works with eventMessage, referenceState, and enrichedMessage rather than with raw wire-protocol rows.
  • The joined external state isn’t changed from the computation: the ConvertFrom method isn’t used for it.

See also