如何配置FIWARE Scorpio将历史实体数据存储至PostgreSQL?
解决FIWARE Scorpio历史上下文数据存储到PostgreSQL的配置方法
1. 修正Docker Compose环境变量配置
确保Scorpio服务启用PostgreSQL作为历史存储后端,添加以下核心环境变量:
services: scorpio: image: scorpiobroker/scorpio:latest environment: # 启用PostgreSQL作为历史存储后端 - HISTORICAL_REPOSITORY=postgresql # PostgreSQL连接参数 - POSTGRESQL_HOST=postgres - POSTGRESQL_PORT=5432 - POSTGRESQL_DB=scorpio_db - POSTGRESQL_USER=scorpio_user - POSTGRESQL_PASSWORD=scorpio_pass # 开启PostGIS支持(匹配你使用的PostGIS镜像) - POSTGIS_ENABLED=true # 可选:将主上下文实体存储也指向PostgreSQL,实现当前状态持久化 - CONTEXT_BROKER_REPOSITORY=postgresql # NGSI-LD核心上下文配置 - NGSI_LD_CONTEXT=https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld depends_on: postgres: condition: service_healthy ports: - "9090:9090" postgres: image: postgis/postgis:15-3.3 environment: - POSTGRES_DB=scorpio_db - POSTGRES_USER=scorpio_user - POSTGRES_PASSWORD=scorpio_pass volumes: - postgres_scorpio_data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U scorpio_user -d scorpio_db"] interval: 10s timeout: 5s retries: 5 volumes: postgres_scorpio_data:
2. 确保请求触发历史数据存储
发送实体更新时,必须携带Fiware-Options: history请求头(或URL参数?options=history),告知Scorpio记录该版本的历史数据:
示例:更新WeatherObserved实体的温度测量值
curl -X PATCH http://localhost:9090/ngsi-ld/v1/entities/urn:ngsi-ld:WeatherObserved:station001/attrs/temperature \ -H "Content-Type: application/ld+json" \ -H "Fiware-Service: openiot" \ -H "Fiware-ServicePath: /" \ -H "Fiware-Options: history" \ -d '{ "value": 25.7, "observedAt": "2024-05-20T13:00:00Z" }'
3. 验证历史数据表是否生成
进入PostgreSQL容器,查看Scorpio自动创建的历史数据表:
# 替换为你的PostgreSQL容器ID或名称 docker exec -it postgres_scorpio psql -U scorpio_user -d scorpio_db # 列出所有数据库表 \dt
你会看到类似ngsi_ld_entity_history的全局历史表,或按实体类型拆分的表(如weatherobserved_history),历史上下文数据会存储在这些表中。
常见问题排查
- 如果历史表未生成:清空PostgreSQL数据卷(
docker volume rm postgres_scorpio_data)后重新启动容器,确保Scorpio在PostgreSQL完全就绪后完成初始化。 - 如果历史数据未写入:检查请求是否携带
Fiware-Options: history头,或确认Scorpio的HISTORICAL_REPOSITORY配置值为postgresql。 - 版本兼容问题:确保使用的Scorpio镜像与PostGIS镜像版本匹配(建议使用最新稳定版)。
内容的提问来源于stack exchange,提问作者TheArcturus
相关产品推荐
相关产品推荐

