C# select 来自 Cassandra 计数器 table

C# select from Cassandra counter table

我是 Cassandra 计数器领域的新手。所以我创建了一个测试环境。 我可以成功地插入到数据库中,但是当我尝试从数据库中插入 select 时,出现以下错误:

An unhandled exception of type 'System.InvalidCastException' occurred in Cassandra.dll Additional information: Specified cast is not valid.

我得到了以下数据库:

create table counterInt (
MeterID int, 
DayStamp timestamp,
NumberOfValues counter,
Value1 counter,
PRIMARY KEY ((MeterID), Daystamp));

这是我的 select 方法: 编辑:当它命中 row.GetValue<double>("value1"); 时发生错误 主键得到 selected.

public List<CSVMeter> selectBasic()
{
    Connect();
    var resultList = new List<CSVMeter>();
    RowSet result = session.Execute("select * FROM counterInt");
    foreach (Row row in result.GetRows())
    {
        CSVMeter csvMeter = new CSVMeter();
        csvMeter.MeterID = row.GetValue<int>("meterid");
        csvMeter.PeriodStart = row.GetValue<DateTime>("daystamp");
        csvMeter.Value = row.GetValue<double>("value1");
        csvMeter.NumberOfValues = row.GetValue<double>("numberofvalues");
        resultList.Add(csvMeter);
    }
    CloseConnection();
    return resultList;
}

我的对象:

public int MeterID { get; set; }
public DateTime PeriodStart { get; set; }
public double Value { get; set; }
public double Value2 { get; set; }

public CSVMeter(int meterID, DateTime periodStart, double value, double numberOfValues)
{
    this.MeterID = meterID;
    this.PeriodStart = periodStart;
    this.Value = value;
    this.Value2 = value2;
    this.NumberOfValues = numberOfValues;
    this.qualityScore = qualityScore;
}

counterInttable中有值。我做错了什么吗?

Specified cast is not valid

问题当然是因为您使用的 double 类型是小数类型,但计数器是整数,不能转换为小数。您当然应该改用 long

来自Documentation

A counter column value is a 64-bit signed integer

您正在检索它作为 double。您应该改用 long:

csvMeter.Value = row.GetValue<long>("value1");