IoTSharp + SonnetDB 多模型 Profile:关系、时序、缓存、对象桶与搜索怎么组合

IoTSharp 的数据底座不是一种数据。主数据通常走 EF Core 关系库,遥测走时序库,缓存走 Redis/LiteDB/InMemory,对象走 BlobStorage/S3,搜索又可能需要全文或向量。SonnetDB 最近的多模型路线,就是在这些能力之间提供一个可选的本地化 Profile。

这不是说所有场景都必须把 PostgreSQL、Redis、S3、搜索服务一次性替掉。更合理的方式是:边缘、小规模私有化、PoC、离线交付可以优先用 SonnetDB 收敛部署;大规模云端仍保留外部 PostgreSQL、Redis、S3 和专业搜索服务作为并列后端。

一个应用里的几类数据

using SonnetDB.Engine;
using SonnetDB.Sql;

using var db = Tsdb.Open(new TsdbOptions
{
    RootDirectory = "data/iotsharp-profile"
});

SqlExecutor.Execute(db, """
    CREATE TABLE devices (
        id STRING,
        tenant STRING,
        name STRING,
        PRIMARY KEY (id)
    )
    """);

SqlExecutor.Execute(db, """
    CREATE MEASUREMENT telemetry (
        device_id TAG,
        key TAG,
        value FIELD FLOAT
    )
    """);

SqlExecutor.Execute(db, """
    CREATE DOCUMENT COLLECTION device_docs
    """);

关系表放设备维度,measurement 放遥测,document collection 放设备文档或资产扩展属性。它们可以分别增长,也可以在查询时关联。

对象桶放附件和固件

using System.Text;
using SonnetDB.Data.ObjectStorage;

using var objects = new SndbObjectStorageClient(
    "Data Source=data/iotsharp-profile");

await objects.CreateBucketAsync("iotsharp-attachments", "device attachments");

await using var content = new MemoryStream(Encoding.UTF8.GetBytes("inspection report"));
await objects.PutObjectAsync(
    "iotsharp-attachments",
    "devices/pump-1/report.txt",
    content,
    "text/plain");

嵌入式连接字符串使用本地目录,远程连接字符串可以换成:

Data Source=sonnetdb+http://127.0.0.1:5080/iotsharp;Token=...;Timeout=30

全文和向量增强

给设备文档创建全文索引:

SqlExecutor.Execute(db, """
    CREATE FULLTEXT INDEX ft_device_docs
    ON device_docs ('$.name', '$.description')
    USING unicode
    """);

给事件向量创建 measurement:

SqlExecutor.Execute(db, """
    CREATE MEASUREMENT incident_vectors (
        device_id TAG,
        embedding FIELD VECTOR(3)
    )
    """);

然后就可以做设备文档搜索、故障相似度、知识库推荐和租户过滤。

该怎么定位这个 Profile

对 IoTSharp 来说,SonnetDB Profile 的第一价值是“部署收敛”:

  • 本地演示不需要拉起一堆服务。
  • 私有化交付可以减少外部依赖。
  • 边缘现场能在断网时继续写入、查询和审计。
  • 对象、文档、搜索、遥测有统一备份和迁移路径。

但生产边界也要诚实:

  • 大规模多租户云平台仍可能继续使用 PostgreSQL/Redis/外部 S3。
  • 缓存语义、对象桶配额、迁移双写和长稳压测需要按版本逐步验收。
  • 向量和全文索引是可重建派生数据,主数据仍要保留在 measurement/document collection 里。

这种定位最适合工程团队:先把能收敛的收敛,把不能假装的边界写清楚,然后一点点把 Profile 做成可靠选项。


官网地址:https://sonnetdb.com

技术文章站:https://iotpaper.net

开源仓库:https://github.com/IoTSharp/SonnetDB

posted @ 2026-06-14 23:04  IoTSharp  阅读(3)  评论(0)    收藏  举报