All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jcustenborder.kafka.connect.mqtt.MessageConverter Maven / Gradle / Ivy

There is a newer version: 0.1.5
Show newest version
/**
 * Copyright © 2017 Jeremy Custenborder ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jcustenborder.kafka.connect.mqtt;

import com.google.common.collect.ImmutableMap;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.connect.data.Schema;
import org.apache.kafka.connect.data.SchemaBuilder;
import org.apache.kafka.connect.data.Struct;
import org.apache.kafka.connect.source.SourceRecord;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.Map;

class MessageConverter {

  public static final Schema VALUE_SCHEMA;
  public static final Schema KEY_SCHEMA;

  final MqttSourceConnectorConfig config;
  final Time time;

  static {
    KEY_SCHEMA = SchemaBuilder.struct()
        .name("com.github.jcustenborder.kafka.connect.mqtt.Message")
        .field("messageId",
            SchemaBuilder.int32()
                .doc("The MQTT id of the message.")
                .build()
        )
        .build();
    VALUE_SCHEMA = SchemaBuilder.struct()
        .name("com.github.jcustenborder.kafka.connect.mqtt.Message")
        .field("messageId",
            SchemaBuilder.int32()
                .doc("The MQTT id of the message.")
                .build()
        )
        .field("payload",
            SchemaBuilder.bytes()
                .doc("Payload of the message.")
                .build()
        )
        .field("qos",
            SchemaBuilder.int32()
                .doc("QOS of the message.")
                .build()
        )
        .field("duplicate",
            SchemaBuilder.bool()
                .doc("Flag to determine whether or not this message might be a duplicate of one that " +
                    "has already been received.")
                .build()
        )
        .field("retained",
            SchemaBuilder.bool()
                .doc("Flag to whether or not this message should be/was retained by the server.")
                .build()
        )
        .field("topic",
            SchemaBuilder.string()
                .doc("Name of the Mqtt topic the message was published to.")
                .build()
        )
        .build();
  }

  MessageConverter(MqttSourceConnectorConfig config, Time time) {
    this.config = config;
    this.time = time;
  }

  static final Map EMPTY_MAP = ImmutableMap.of();

  public SourceRecord convert(String mqttTopic, MqttMessage message) {
    final String kafkaTopic = String.format("%s%s", this.config.topicPrefix, mqttTopic)
        .replace('/', '.');
    final Struct key = new Struct(KEY_SCHEMA)
        .put("messageId", message.getId());
    final Struct value = new Struct(VALUE_SCHEMA)
        .put("messageId", message.getId())
        .put("payload", message.getPayload())
        .put("qos", message.getQos())
        .put("duplicate", message.isDuplicate())
        .put("retained", message.isRetained())
        .put("topic", mqttTopic);
    final SourceRecord result = new SourceRecord(
        EMPTY_MAP,
        EMPTY_MAP,
        kafkaTopic,
        null,
        key.schema(),
        key,
        value.schema(),
        value,
        this.time.milliseconds()
    );
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy