github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/builder/encoder_builder.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package builder
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/pingcap/tiflow/pkg/config"
    20  	cerror "github.com/pingcap/tiflow/pkg/errors"
    21  	"github.com/pingcap/tiflow/pkg/sink/codec"
    22  	"github.com/pingcap/tiflow/pkg/sink/codec/avro"
    23  	"github.com/pingcap/tiflow/pkg/sink/codec/canal"
    24  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    25  	"github.com/pingcap/tiflow/pkg/sink/codec/craft"
    26  	"github.com/pingcap/tiflow/pkg/sink/codec/csv"
    27  	"github.com/pingcap/tiflow/pkg/sink/codec/debezium"
    28  	"github.com/pingcap/tiflow/pkg/sink/codec/maxwell"
    29  	"github.com/pingcap/tiflow/pkg/sink/codec/open"
    30  	"github.com/pingcap/tiflow/pkg/sink/codec/simple"
    31  )
    32  
    33  // NewRowEventEncoderBuilder returns an RowEventEncoderBuilder
    34  func NewRowEventEncoderBuilder(
    35  	ctx context.Context,
    36  	cfg *common.Config,
    37  ) (codec.RowEventEncoderBuilder, error) {
    38  	switch cfg.Protocol {
    39  	case config.ProtocolDefault, config.ProtocolOpen:
    40  		return open.NewBatchEncoderBuilder(ctx, cfg)
    41  	case config.ProtocolCanal:
    42  		return canal.NewBatchEncoderBuilder(cfg), nil
    43  	case config.ProtocolAvro:
    44  		return avro.NewBatchEncoderBuilder(ctx, cfg)
    45  	case config.ProtocolMaxwell:
    46  		return maxwell.NewBatchEncoderBuilder(cfg), nil
    47  	case config.ProtocolCanalJSON:
    48  		return canal.NewJSONRowEventEncoderBuilder(ctx, cfg)
    49  	case config.ProtocolCraft:
    50  		return craft.NewBatchEncoderBuilder(cfg), nil
    51  	case config.ProtocolDebezium:
    52  		return debezium.NewBatchEncoderBuilder(cfg, config.GetGlobalServerConfig().ClusterID), nil
    53  	case config.ProtocolSimple:
    54  		return simple.NewBuilder(ctx, cfg)
    55  	default:
    56  		return nil, cerror.ErrSinkUnknownProtocol.GenWithStackByArgs(cfg.Protocol)
    57  	}
    58  }
    59  
    60  // NewTxnEventEncoderBuilder returns an TxnEventEncoderBuilder.
    61  func NewTxnEventEncoderBuilder(
    62  	c *common.Config,
    63  ) (codec.TxnEventEncoderBuilder, error) {
    64  	switch c.Protocol {
    65  	case config.ProtocolCsv:
    66  		return csv.NewTxnEventEncoderBuilder(c), nil
    67  	case config.ProtocolCanalJSON:
    68  		return canal.NewJSONTxnEventEncoderBuilder(c), nil
    69  	default:
    70  		return nil, cerror.ErrSinkUnknownProtocol.GenWithStackByArgs(c.Protocol)
    71  	}
    72  }