[英]Fiware Cygnus: Can we store Orion data in MongoDB under other types than String?
[英]How to store hierarchical data through FIWARE Cygnus with MongoDB
我已经部署了FIWARE配置,以从TheThingsNetwork接收MQTT消息。 该配置使用IoTAgent-JSON,Orion和Cygnus,以及用于Cygnus的MongoDB后端。
消息已正确持久保存到MongoDB。 不幸的是,消息数据具有层次结构,消息的某些部分在MongoDB中显示为字符串,而不是嵌入式子文档。
这使得查询数据变得困难。
详细信息如下:
消息格式由TTN MQTT数据API定义。
我在Orion上定义了一个实体类型,如下所示:
curl http://localhost:1026/v2/entities -X POST -H "content-type: application/json" -H "fiware-service: myservice" -H "fiware-servicepath: /mypath" -d @- << EOF
{
"id": "TtnMqttMessage",
"type": "TtnMqttMessge",
"app_id": { "type": "Text", "value": "my-app-id" },
"dev_id": { "type": "Text", "value": "my-dev-id" },
...
"metadata": { "type": "StructuredValue", "value": {
"airtime": 46336000,
"time": "1970-01-01T00:00:00Z",
...
"gateways":
[
{
"gw_id": "gw1",
"timestamp": 12345,
"time": "1970-01-01T00:00:00Z",
...
"altitude": 6
}
]
} }
}
EOF
特别是,在上述配置中, metadata
是一个结构化值,包含gateways
数组。
在IoTAgent-JSON上,提供了服务和设备:
curl http://localhost:4041/iot/services -X POST -H "content-type: application/json" -H "fiware-service: myservice" -H "fiware-servicepath: /mypath" -d @- << EOF
{
"services": [
{
"apikey": "my_app_id",
"entity_type": "TtnMqttMessage",
"resource": "/iot/json"
}
]
}
EOF
curl http://localhost:4041/iot/devices?options=keyValues -X POST -H "content-type: application/json" -H "fiware-service: myservice" -H "fiware-servicepath: /mypath" -d @- << EOF
{
"devices": [{
"device_id": "my_device_id",
"entity_name": "TtnMqttMessage",
"entity_type": "TtnMqttMessage",
"timezone": "Europe/Zurich",
"transport": "MQTT"
}]
}
EOF
最后,从Orion到Cygnus建立了通知订阅:
curl http://localhost:1026/v1/subscribeContext -H "content-type: application/json" -H "fiware-service: myservice" -H "fiware-servicepath: /mypath" -X POST -d @- << EOF
{
"entities": [
{
"type": "TtnMqttMessage",
"isPattern": "false",
"id": "TtnMqttMessage"
}
],
"attributes": [ "app_id", "dev_id", "hardware_serial", "port", "counter", "is_retry", "confirmed", "payload_raw", "payload_fields", "metadata" ],
"reference": "http://cygnus:5050/notify",
"duration": "P100Y",
"notifyConditions": [
{
"type": "ONCHANGE",
"condValues": [ "app_id", "dev_id", "payload_raw", "counter" ]
}
]
}
EOF
收到的消息将保留到MongoDB:
> mongo
> ...
> db['..collectionname...'].findOne();
{
"_id" : ObjectId("5adf0b904cedfd001cd72113"),
"recvTime" : ISODate("2018-04-24T10:48:47.605Z"),
"app_id" : "my-app-id",
"confirmed" : "false",
"counter" : "2",
"dev_id" : "my-dev-id",
"hardware_serial" : "0102030405060708",
"is_retry" : "false",
"metadata" : "{\"airtime\":4.6336e+07,\"time\":\"1970-01-01T00:00:00Z\",\"frequency\":868.1,\"modulation\":\"LORA\",\"data_rate\":\"SF7BW125\",\"bit_rate\":50000,\"coding_rate\":\"4/5\",\"latitude\":52.2345,\"longitude\":6.2345,\"altitude\":2,\"gateways\":[{\"gw_id\":\"gw1\",\"timestamp\":12345,\"time\":\"1970-01-01T00:00:00Z\",\"channel\":0,\"rssi\":-25,\"snr\":5,\"rf_chain\":0,\"latitude\":52.1234,\"longitude\":6.1234,\"altitude\":6}]}",
"payload_fields" : "{}",
"payload_raw" : "AQIDBA",
"port" : "1"
}
从上面可以看出,属性metadata
(尤其是它包含的数组gateways
存储为字符串而不是JSON子文档。
如何以易于查询的格式保留数据? 例如。
metadata
下具有嵌入式子文档(非规范化形式) metadata
是引用消息的主文档的单独文档。 创建的实体看起来结构良好,我注意到您使用v2创建了实体,但是您订阅了使用v1来接收通知,我知道这样做是一种正确的方法,但是由于该原因可能会产生错误的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.