编解码
Schema Registry 目前可支持三种格式的编解码: , Protobuf (opens new window) ,以及自定义编码。其中 Avro 和 Protobuf 是依赖 Schema 的数据格式,编码后的数据为二进制,解码后为 Map 1 格式 。解码后的数据可直接被规则引擎和其他插件使用。用户自定义的 (3rd-party) 编解码服务通过 HTTP 或 TCP 回调的方式,进行更加贴近业务需求的编解码。
Important
Schema Registry 为 Avro 和 Protobuf 等内置编码格式维护 Schema 文本,但对于自定义编解码 (3rd-party) 格式,如需要,Schema 文本需由编解码服务自己维护
设计原则和教程参见
创建 Schema 的时候,需要给定 Schema 的名字。名字是 Schema 的唯一标识,不可重复。
Schema 名字的为如下格式的字符串:
- 首字符为字母
- 其余字符为字母数字或者下划线
[A-Za-z0-9_]
HTTP API
创建
API 定义
参数定义
API 请求示例
API 请求消息体
API 返回数据示例
{
"code":0,
"data":{
"name":"sensor_notify_avro",
"schema":"{\"type\":\"record\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"int\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}",
"parser_type":"avro",
"parser_addr":null,
"parser_opts":{},
"description":"Test Avro Schema"
}
cURL 示例
创建 Avro Schema:
## This appid and secret can be created in emqx dashboard.
$ APPSECRET='a78ed1495de28:Mjg5MzU2MDY1NTU5MTM4Mjk4Nzg3MjgwOTEwNDExMzY2NDA'
$ SCHEMA='{"type": "record", "fields": [{"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]}]}'
$ curl --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/schemas' -d \
'{"name":"sensor_notify_avro", "parser_type": "avro", "description":"Test Avro Schema", "schema": '$SCHEMA'}'
{"code":0,"data":{"name":"sensor_notify_avro","schema":"...","parser_type":"avro","parser_addr":null,"parser_opts":{},"description":"Test Avro Schema"}}
创建 Protobuf Schema:
## ProtoBuf
$ APPSECRET='a78ed1495de28:Mjg5MzU2MDY1NTU5MTM4Mjk4Nzg3MjgwOTEwNDExMzY2NDA'
$ SCHEMA='message Person {
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phones = 4;
}
repeated Person people = 1;
}'
$ curl --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/schemas' -d \
'{"name":"sensor_notify_protobuf", "parser_type": "protobuf", "schema": "'$SCHEMA'"}'
创建第三方编解码:
Important
创建第三方编码时,会尝试连接指定地址的服务。如果连接失败,创建将会失败。
查询
列出全部 Schema:
GET api/v3/schemas
查询指定 Schema:
GET api/v3/schemas/${schema_id}
API 请求示例
GET http://localhost:8080/api/v3/schemas/sensor_notify_avro (opens new window)
API 返回数据示例
{
"code":0,
"data":[
{
"name":"sensor_notify_avro",
"schema":" ... ",
"parser_type":"avro",
"parser_addr":null,
"parser_opts":{},
"description":"Schema for notification report from sensors, in avro format"
}
]
}
cURL 示例
查询 sensor_notify_avro :
删除
删除指定 Schema:
DELETE api/v3/schemas/${schema_id}
API 请求示例
删除 sensor_notify_avro:
DELETE
API 返回数据示例
{
"code":0
}
cURL 示例
删除 sensor_notify_avro :
$ APPSECRET='a78ed1495de28:Mjg5MzU2MDY1NTU5MTM4Mjk4Nzg3MjgwOTEwNDExMzY2NDA'
$ curl -XDELETE -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/schemas/sensor_notify_avro'
{"code":0}
Footnotes