Kafka 系列 —(25)物模型 TSL → Kafka Schema 转换模板

目录

1. 模板说明

2. 输入:物模型 TSL(JSON)

3. 输出:Kafka Schema(可选三种格式)

A. Kafka Avro Schema 模板

B. Kafka JSON Schema 模板

C. Protobuf Schema 模板

4. 自动转换规则

5. 示例:智能锁(SmartLock)TSL → Kafka Schema

输入 TSL:

输出:Kafka JSON Schema(自动生成)

输出:Kafka Avro Schema(自动生成)

输出:Protobuf(自动生成)


1. 模板说明

此模板用于在 IoT 项目中:

  • 设备 TSL 数据模型 (Properties, Events, Services)

转换为:

  • Kafka Schema Registry 中可用的 Avro Schema、JSON Schema 或 Protobuf

保证:

  • 字段类型对齐

  • 设备端上报字段与 Kafka 流数据一致

  • 避免互联网设备数据格式混乱

  • 为 Flink / Spark / Kafka Streams 提供强 schema


2. 输入:物模型 TSL(JSON)

TSL 示例(阿里云风格)

{
  "properties": [
    {
      "identifier": "batteryLevel",
      "name": "Battery Level",
      "dataType": {
        "type": "int",
        "min": "0",
        "max": "100"
      }
    },
    {
      "identifier": "lockState",
      "name": "Lock State",
      "dataType": {
        "type": "enum",
        "enumValues": {
          "0": "locked",
          "1": "unlocked"
        }
      }
    }
  ],
  "events": [
    {
      "identifier": "doorOpened",
      "outputData": [
        {
          "identifier": "openTime",
          "dataType": { "type": "string" }
        }
      ]
    }
  ],
  "services": [
    {
      "identifier": "unlock",
      "inputData": [
        {
          "identifier": "method",
          "dataType": {
            "type": "string"
          }
        }
      ]
    }
  ]
}

3. 输出:Kafka Schema(可选三种格式)


A. Kafka Avro Schema 模板

{
  "type": "record",
  "name": "${DeviceModelName}",
  "namespace": "iot.smartlock.schema",
  "fields": [
    ${TSL_FIELDS_AVRO}
  ]
}

字段生成模板:

{
  "name": "${identifier}",
  "type": ${fieldType},
  "doc": "${name}"
}

B. Kafka JSON Schema 模板

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "${DeviceModelName}",
  "type": "object",
  "properties": {
    ${TSL_PROPERTIES_JSON}
  },
  "required": [${TSL_REQUIRED}]
}

字段生成模板:

"${identifier}": {
  "type": "${jsonType}",
  "description": "${name}"
}

C. Protobuf Schema 模板

syntax = "proto3";

message ${DeviceModelName} {
  ${TSL_FIELDS_PROTO}
}

字段生成模板:

${protoType} ${identifier} = ${index};

4. 自动转换规则

TSL 类型 Kafka JSON Avro Protobuf
int integer "int" int32
float/double number "double" double
string string "string" string
bool boolean "boolean" bool
enum string {"type":"enum",…} string
struct object record message
array array array repeated
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

34号树洞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值