编解码

    Schema Registry 目前可支持三种格式的编解码: , Protobuf 编解码 - 图1 (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 返回数据示例

    1. {
    2. "code":0,
    3. "data":{
    4. "name":"sensor_notify_avro",
    5. "schema":"{\"type\":\"record\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"int\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}",
    6. "parser_type":"avro",
    7. "parser_addr":null,
    8. "parser_opts":{},
    9. "description":"Test Avro Schema"
    10. }

    cURL 示例

    创建 Avro Schema:

    1. ## This appid and secret can be created in emqx dashboard.
    2. $ APPSECRET='a78ed1495de28:Mjg5MzU2MDY1NTU5MTM4Mjk4Nzg3MjgwOTEwNDExMzY2NDA'
    3. $ SCHEMA='{"type": "record", "fields": [{"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]}]}'
    4. $ curl --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/schemas' -d \
    5. '{"name":"sensor_notify_avro", "parser_type": "avro", "description":"Test Avro Schema", "schema": '$SCHEMA'}'
    6. {"code":0,"data":{"name":"sensor_notify_avro","schema":"...","parser_type":"avro","parser_addr":null,"parser_opts":{},"description":"Test Avro Schema"}}

    创建 Protobuf Schema:

    1. ## ProtoBuf
    2. $ APPSECRET='a78ed1495de28:Mjg5MzU2MDY1NTU5MTM4Mjk4Nzg3MjgwOTEwNDExMzY2NDA'
    3. $ SCHEMA='message Person {
    4. required int32 id = 2;
    5. optional string email = 3;
    6. enum PhoneType {
    7. MOBILE = 0;
    8. HOME = 1;
    9. WORK = 2;
    10. }
    11. message PhoneNumber {
    12. required string number = 1;
    13. optional PhoneType type = 2 [default = HOME];
    14. }
    15. repeated PhoneNumber phones = 4;
    16. }
    17. repeated Person people = 1;
    18. }'
    19. $ curl --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/schemas' -d \
    20. '{"name":"sensor_notify_protobuf", "parser_type": "protobuf", "schema": "'$SCHEMA'"}'

    创建第三方编解码:

    Important

    创建第三方编码时,会尝试连接指定地址的服务。如果连接失败,创建将会失败。

    查询

    列出全部 Schema:

    1. GET api/v3/schemas

    查询指定 Schema:

    1. GET api/v3/schemas/${schema_id}

    API 请求示例

    GET http://localhost:8080/api/v3/schemas/sensor_notify_avro 编解码 - 图2 (opens new window)

    API 返回数据示例

    1. {
    2. "code":0,
    3. "data":[
    4. {
    5. "name":"sensor_notify_avro",
    6. "schema":" ... ",
    7. "parser_type":"avro",
    8. "parser_addr":null,
    9. "parser_opts":{},
    10. "description":"Schema for notification report from sensors, in avro format"
    11. }
    12. ]
    13. }

    cURL 示例

    查询 sensor_notify_avro :

    删除

    删除指定 Schema:

    1. DELETE api/v3/schemas/${schema_id}

    API 请求示例

    删除 sensor_notify_avro:

    DELETE

    API 返回数据示例

    1. {
    2. "code":0
    3. }

    cURL 示例

    删除 sensor_notify_avro :

    1. $ APPSECRET='a78ed1495de28:Mjg5MzU2MDY1NTU5MTM4Mjk4Nzg3MjgwOTEwNDExMzY2NDA'
    2. $ curl -XDELETE -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/schemas/sensor_notify_avro'
    3. {"code":0}

    Footnotes