如何使用C# ILP客户端向QuestDB提交空值?
问题描述
已创建如下trades表:
CREATE TABLE 'trades' ( symbol SYMBOL, side SYMBOL, price DOUBLE, revised_price DOUBLE, amount DOUBLE, timestamp TIMESTAMP ) timestamp (timestamp) PARTITION BY DAY WAL;
使用C#客户端(net-questdb-client)发送数据时,首次提交仅revised_price字段未知,需要传入空值,尝试代码如下:
using var sender = Sender.New("http::addr=localhost:9000;"); await sender.Table("trades") .Symbol("symbol", "ETH-USD") .Symbol("side", "sell") .Column("price", 2615.54) .Column("amount", 0.00044) .Column("revised_price", null) .AtAsync(new DateTime(2021, 11, 25, 0, 46, 26)); await sender.SendAsync();
运行时触发错误:
Unhandled exception. QuestDB.Utils.IngressError: ServerFlushError : Bad request. Server Response ( Code: `invalid` Message: `failed to parse line protocol:errors encountered on line(s): error in line 1: table: storage path, column: state; cast error from protocol type: STRING to column type: DOUBLE` Line: `1` ErrorId: `c8712c23edc1-1` )
解决方案
QuestDB的Ingress协议中,直接传递null会被解析为字符串类型,与DOUBLE字段类型冲突,正确处理空值的方式有两种:
方法1:省略空值字段
如果字段值为空,直接不调用对应的.Column()方法,QuestDB会自动将该字段设为NULL:
using var sender = Sender.New("http::addr=localhost:9000;"); await sender.Table("trades") .Symbol("symbol", "ETH-USD") .Symbol("side", "sell") .Column("price", 2615.54) .Column("amount", 0.00044) .AtAsync(new DateTime(2021, 11, 25, 0, 46, 26)); await sender.SendAsync();
方法2:使用可空类型显式传递空值
通过Nullable<double>(即double?)类型传递空值,确保客户端生成正确的协议格式:
using var sender = Sender.New("http::addr=localhost:9000;"); double? revisedPrice = null; await sender.Table("trades") .Symbol("symbol", "ETH-USD") .Symbol("side", "sell") .Column("price", 2615.54) .Column("amount", 0.00044) .Column("revised_price", revisedPrice) .AtAsync(new DateTime(2021, 11, 25, 0, 46, 26)); await sender.SendAsync();
内容的提问来源于stack exchange,提问作者Nick Woolmer
相关产品推荐
相关产品推荐

