# Enum in Kafka sink connector

**URL:** <https://forum.confluent.io/t/enum-in-kafka-sink-connector/956>\
**Category:** Kafka Connect\
**Created:** [19 February 2021 13:30 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956 "2021-02-19T13:30:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit.11tyagi](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@ankit.11tyagi](https://forum.confluent.io/u/ankit.11tyagi)\
**Post date:** [19 February 2021 13:30 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/1 "2021-02-19T13:30:46Z")

</div>

I am working on a MSSQL sink connector which simply saves data from a topic to a table. My topic is bound to an Avro schema. For one of the field, I am using enum.

Now when my connector saves data to table, it saves the enum name. But for some reason, I want it to save both Enum numeric value as well as Enum name.

e.g. if I have a below enum for car models, then connector saved Hatchback/Sedan/SUV. I want to save the values,1/2/3 as well. Both values can go to different columns.

```auto
enum Model
{
    Hatchback: 1,
    Sedan: 2,
    SUV: 3
}

```

How can we achieve this?

---

<div class="post-metadata">

**Author:** ![riferrei](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/riferrei/32/1848_2.png) [@riferrei](https://forum.confluent.io/u/riferrei)\
**Post date:** [19 February 2021 14:09 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/2 "2021-02-19T14:09:06Z")

</div>

> [@ankit.11tyagi](#):
>
> I am working on a MSSQL sink connector

Are you using a MSSQL sink connector or writing one? I’m asking this because I’m not aware of any official MSSQL sink connector. Unless you’re using the JDBC sink connector?

---

<div class="post-metadata">

**Author:** ![ankit.11tyagi](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@ankit.11tyagi](https://forum.confluent.io/u/ankit.11tyagi)\
**Post date:** [19 February 2021 15:03 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/3 "2021-02-19T15:03:05Z")

</div>

Sorry for not mentioning it explicitly, I mean the Jdbc sink connector.

---

<div class="post-metadata">

**Author:** ![riferrei](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/riferrei/32/1848_2.png) [@riferrei](https://forum.confluent.io/u/riferrei)\
**Post date:** [19 February 2021 16:33 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/4 "2021-02-19T16:33:47Z")

</div>

No worries. Try setting the property `value.converter.enhanced.avro.schema.support` to `true`. According to the documentation:

> Enable enhanced Avro schema support in the Avro Converter. When set to `true` , this property preserves Avro schema package information and Enums when going from Avro schema to Connect schema. This information is added back in when going from Connect schema to Avro schema.

📄 Source: [Using Kafka Connect with Schema Registry | Confluent Documentation](https://docs.confluent.io/platform/current/schema-registry/connect.html)

Here is an example:

```
key.converter=io.confluent.connect.avro.AvroConverter
key.converter.schema.registry.url=<SCHEMA_REGISTRY_URL>
key.converter.enhanced.avro.schema.support=true
value.converter=io.confluent.connect.avro.AvroConverter
value.converter.schema.registry.url=<SCHEMA_REGISTRY_URL>
value.converter.enhanced.avro.schema.support=true

```

---

<div class="post-metadata">

**Author:** ![ankit.11tyagi](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@ankit.11tyagi](https://forum.confluent.io/u/ankit.11tyagi)\
**Post date:** [22 February 2021 08:29 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/5 "2021-02-22T08:29:53Z")

</div>

I have enabled this setting but connector still saves only enum text in the table. Any idea where can I find the additional info saved by connector when this setting is true?

---

<div class="post-metadata">

**Author:** ![AskMeiPaaS](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/askmeipaas/32/344_2.png) [@AskMeiPaaS](https://forum.confluent.io/u/AskMeiPaaS)\
**Post date:** [22 February 2021 09:54 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/6 "2021-02-22T09:54:58Z")

</div>

Hi @ankit.11tyagi : I doubt the enum type is supported based on the data conversion table provided here: [https://docs.confluent.io/kafka-connect-jdbc/current/sink-connector/index.html](https://docs.confluent.io/kafka-connect-jdbc/current/sink-connector/index.html)

---

<div class="post-metadata">

**Author:** ![ankit.11tyagi](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@ankit.11tyagi](https://forum.confluent.io/u/ankit.11tyagi)\
**Post date:** [22 February 2021 10:46 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/7 "2021-02-22T10:46:46Z")

</div>

Enum type seems to be supported upto some extent as my connector saved the enum text in varchar columns in table. It is the numeric value associated with the enum which is not saving.

---

<div class="post-metadata">

**Author:** ![AskMeiPaaS](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/askmeipaas/32/344_2.png) [@AskMeiPaaS](https://forum.confluent.io/u/AskMeiPaaS)\
**Post date:** [22 February 2021 12:13 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/8 "2021-02-22T12:13:15Z")

</div>

@ankit.11tyagi : My apologies, Ankit, if I am going too far. The supported JDBC conversion ([Understanding data type conversions - JDBC Driver for SQL Server | Microsoft Docs](https://docs.microsoft.com/en-us/sql/connect/jdbc/understanding-data-type-conversions)) does not mention enum. Hence, I assume the “unknown” behaviour by the connector.

---

<div class="post-metadata">

**Author:** ![riferrei](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/riferrei/32/1848_2.png) [@riferrei](https://forum.confluent.io/u/riferrei)\
**Post date:** [22 February 2021 15:25 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/9 "2021-02-22T15:25:49Z")

</div>

I think the limitation here is that the internal structure used by Kafka Connect to represent records known as [Struct](https://github.com/apache/kafka/blob/1a09bac0301a15fe7967e9a0c5bf11d34120561b/connect/api/src/main/java/org/apache/kafka/connect/data/Struct.java) doesn’t support enums with additional values — hence why it tries to capture the enum value and simply force a `toString()` on it. You can check this behavior by taking a look in the [Schema](https://github.com/apache/kafka/blob/1a09bac0301a15fe7967e9a0c5bf11d34120561b/connect/api/src/main/java/org/apache/kafka/connect/data/Schema.java) class that govern how each Struct is modeled.

It seems to me then that you have two options:

1. **Using a SMT in the sink connector** : perhaps you can try to change the Struct schema on-the-fly by applying a custom SMT that depicts the enum value into two new fields. **Chris Matta** from Confluent wrote [a very nice blog about this](https://www.confluent.io/blog/kafka-connect-single-message-transformation-tutorial-with-examples/).

2. **Model the enum as a complex type** : if you have the chance you can change the inbound Avro to model the entity as a complex type with two distinct columns instead of using an enum. I think this option may cause some friction at first — but in the long run seems to be the right thing to do.

Thanks,

— @riferrei

---

<div class="post-metadata">

**Author:** ![ankit.11tyagi](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@ankit.11tyagi](https://forum.confluent.io/u/ankit.11tyagi)\
**Post date:** [23 February 2021 06:39 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/10 "2021-02-23T06:39:24Z")

</div>

Thanks @riferrei for the information. You are right the option #2 will give more control. However, I will have to check with the team managing the topic schema if they can implement this change as this does not seem to be backward compatible.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [9 March 2021 06:40 UTC](https://forum.confluent.io/t/enum-in-kafka-sink-connector/956/11 "2021-03-09T06:40:19Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
