3.2 KiB
3.2 KiB
theme | color | colorSecondary | backgroundColor | backgroundImage | paginate | marp |
---|---|---|---|---|---|---|
gaia | url('https://marp.app/assets/hero-background.jpg') | true | true |
EMQ X Resource
What is it for
The emqx_resource is a behavior that manages configuration specs and runtime states for resources like mysql or redis backends.
It is intended to be used by the emqx_bridges and all other resources that need CRUD operations to their configs, and need to initialize the states when creating.
The Demo
The bridge for mysql
The callback module 'emqx_mysql_connector'
- include the emqx_resource_behaviour.hrl:
-include_lib("emqx_resource/include/emqx_resource_behaviour.hrl").
- provide the hocon schema for validating the configs:
schema() ->
emqx_connector_schema_lib:relational_db_fields() ++
emqx_connector_schema_lib:ssl_fields().
...
- write the callback functions for starting or stopping the resource instance:
on_start/2,
on_stop/2,
on_query/4,
on_health_check/2
Start the emqx_bridge
application:ensure_all_started(emqx_bridge).
To use the mysql resource from code:
emqx_resource:query(ResourceID, {sql, SQL}).
(emqx@127.0.0.1)2> emqx_resource:list_instances_verbose().
[#{config =>
#{<<"auto_reconnect">> => true,<<"cacertfile">> => [],
<<"certfile">> => [],<<"database">> => "mqtt",
<<"keyfile">> => [],<<"password">> => "public",
<<"pool_size">> => 1,
<<"server">> => {{127,0,0,1},3306},
<<"ssl">> => false,<<"username">> => "root",
<<"verify">> => false},
id => <<"bridge:mysql-def">>,mod => emqx_connector_mysql,
state => #{poolname => 'bridge:mysql-def'},
status => started}]
(emqx@127.0.0.1)3> emqx_resource:query(<<"bridge:mysql-def">>, {sql, <<"SELECT count(1)">>}).
{ok,[<<"count(1)">>],[[1]]}
To get all available data bridges:
curl -q --basic -u admin:public -X GET "http://localhost:8081/api/v4/data_bridges/" | jq .
Create
To create a mysql data bridge:
BridgeMySQL='{
"type": "mysql",
"status": "started",
"config": {
"verify": false,
"username": "root",
"ssl": false,
"server": "127.0.0.1:3306",
"pool_size": 1,
"password": "public",
"keyfile": "",
"database": "mqtt",
"certfile": "",
"cacertfile": "",
"auto_reconnect": true
}
}'
curl -q --basic -u admin:public -X POST "http://localhost:8081/api/v4/data_bridges/mysql-aaaa" -d $BridgeMySQL | jq .
Update
To update an existing data bridge:
BridgeMySQL='{
"type": "mysql",
"status": "started",
"config": {
"verify": false,
"username": "root",
"ssl": false,
"server": "127.0.0.1:3306",
"pool_size": 2,
"password": "public",
"keyfile": "",
"database": "mqtt",
"certfile": "",
"cacertfile": "",
"auto_reconnect": true
}
}'
curl -q --basic -u admin:public -X PUT "http://localhost:8081/api/v4/data_bridges/mysql-aaaa" -d $BridgeMySQL | jq .