feat(mqtt): 新增 MQTT 数据源连接与测试发消息支持

This commit is contained in:
Syngnat
2026-06-14 11:38:05 +08:00
parent d805f288ae
commit 0fa8afd517
31 changed files with 1896 additions and 18 deletions

View File

@@ -16,6 +16,8 @@ func normalizeRunConfig(config connection.ConnectionConfig, dbName string) conne
}
switch strings.ToLower(strings.TrimSpace(config.Type)) {
case "mqtt", "mqtts":
// MQTT 的 Database 字段表示默认 Topic不能被树上的 synthetic database(topics) 覆盖。
case "kafka", "apache-kafka", "apache_kafka":
// Kafka 的 Database 字段表示默认 Topic不能被树上的 synthetic database(topics) 覆盖。
case "oceanbase":
@@ -53,7 +55,7 @@ func normalizeSchemaAndTable(config connection.ConnectionConfig, dbName string,
// Elasticsearch索引名可能含多个点如 iot_pro_biz_operate_log.index.20240626
// 不能按点分割,直接返回原始数据库名和完整表名。
if dbType == "elasticsearch" || dbType == "iotdb" || dbType == "kafka" || dbType == "rabbitmq" {
if dbType == "elasticsearch" || dbType == "iotdb" || dbType == "mqtt" || dbType == "kafka" || dbType == "rabbitmq" {
return rawDB, rawTable
}
@@ -112,7 +114,7 @@ func normalizeSchemaAndTable(config connection.ConnectionConfig, dbName string,
func normalizeMetadataSchemaAndTable(config connection.ConnectionConfig, dbName string, tableName string) (string, string) {
schema, table := normalizeSchemaAndTable(config, dbName, tableName)
switch resolveDDLDBType(config) {
case "kafka", "rabbitmq":
case "mqtt", "kafka", "rabbitmq":
return schema, table
case "postgres", "kingbase", "highgo", "vastbase", "opengauss", "gaussdb":
rawTable := strings.TrimSpace(tableName)

View File

@@ -332,6 +332,18 @@ func TestNormalizeSchemaAndTable_KafkaPreservesDottedTopicName(t *testing.T) {
}
}
func TestNormalizeSchemaAndTable_MQTTPreservesTopicFilter(t *testing.T) {
t.Parallel()
schemaOrDb, table := normalizeSchemaAndTable(connection.ConnectionConfig{
Type: "mqtt",
}, "topics", "devices/floor1.sensor.v1")
if schemaOrDb != "topics" || table != "devices/floor1.sensor.v1" {
t.Fatalf("expected mqtt topic filter to stay intact, got %q.%q", schemaOrDb, table)
}
}
func TestNormalizeSchemaAndTable_RabbitMQPreservesDottedQueueName(t *testing.T) {
t.Parallel()
@@ -356,6 +368,18 @@ func TestNormalizeMetadataSchemaAndTable_KafkaPreservesDottedTopicName(t *testin
}
}
func TestNormalizeMetadataSchemaAndTable_MQTTPreservesTopicFilter(t *testing.T) {
t.Parallel()
schemaOrDb, table := normalizeMetadataSchemaAndTable(connection.ConnectionConfig{
Type: "mqtt",
}, "topics", "devices/floor1.sensor.v1")
if schemaOrDb != "topics" || table != "devices/floor1.sensor.v1" {
t.Fatalf("expected mqtt metadata topic filter to stay intact, got %q.%q", schemaOrDb, table)
}
}
func TestNormalizeMetadataSchemaAndTable_RabbitMQPreservesDottedQueueName(t *testing.T) {
t.Parallel()

View File

@@ -315,8 +315,8 @@ func normalizeSchemaAndTableByType(dbType string, dbName string, tableName strin
return rawDB, rawTable
}
// Elasticsearch / RabbitMQ / Kafka对象名可能含多个点不能按点分割
if dbType == "elasticsearch" || dbType == "kafka" || dbType == "rabbitmq" {
// Elasticsearch / MQTT / RabbitMQ / Kafka对象名可能含多个点或路径,不能按点分割
if dbType == "elasticsearch" || dbType == "mqtt" || dbType == "kafka" || dbType == "rabbitmq" {
return rawDB, rawTable
}

View File

@@ -166,6 +166,15 @@ func TestNormalizeSchemaAndTableByType_KafkaPreservesDottedTopicName(t *testing.
}
}
func TestNormalizeSchemaAndTableByType_MQTTPreservesTopicFilter(t *testing.T) {
t.Parallel()
schema, table := normalizeSchemaAndTableByType("mqtt", "topics", "devices/floor1.sensor.v1")
if schema != "topics" || table != "devices/floor1.sensor.v1" {
t.Fatalf("expected mqtt topic filter to stay intact, got %q.%q", schema, table)
}
}
func TestNormalizeSchemaAndTableByType_RabbitMQPreservesDottedQueueName(t *testing.T) {
t.Parallel()

View File

@@ -1432,6 +1432,8 @@ func normalizeDriverType(driverType string) string {
return "gaussdb"
case "goldendb", "greatdb", "gdb":
return "goldendb"
case "mqtt", "mqtts":
return "mqtt"
case "kafka", "apache-kafka", "apache_kafka":
return "kafka"
case "rabbitmq", "rabbit-mq", "rabbit_mq":
@@ -1505,6 +1507,7 @@ func allDriverDefinitionsWithPackages(packages map[string]pinnedDriverPackage) [
{Type: "oracle", Name: "Oracle", Engine: driverEngineGo, BuiltIn: true},
{Type: "redis", Name: "Redis", Engine: driverEngineGo, BuiltIn: true},
{Type: "postgres", Name: "PostgreSQL", Engine: driverEngineGo, BuiltIn: true},
{Type: "mqtt", Name: "MQTT", Engine: driverEngineGo, BuiltIn: true},
{Type: "kafka", Name: "Kafka", Engine: driverEngineGo, BuiltIn: true},
{Type: "rabbitmq", Name: "RabbitMQ", Engine: driverEngineGo, BuiltIn: true},

View File

@@ -518,6 +518,22 @@ func TestKafkaDriverDefinitionIsBuiltIn(t *testing.T) {
}
}
func TestMQTTDriverDefinitionIsBuiltIn(t *testing.T) {
definition, ok := resolveDriverDefinition("mqtts")
if !ok {
t.Fatal("expected mqtt driver definition")
}
if definition.Name != "MQTT" {
t.Fatalf("unexpected mqtt driver name: %q", definition.Name)
}
if !definition.BuiltIn {
t.Fatal("expected mqtt to be a built-in driver")
}
if definition.PinnedVersion != "" || definition.DefaultDownloadURL != "" {
t.Fatalf("expected mqtt builtin definition to omit optional-agent metadata: %#v", definition)
}
}
func TestRabbitMQDriverDefinitionIsBuiltIn(t *testing.T) {
definition, ok := resolveDriverDefinition("rabbit-mq")
if !ok {