我有一个简单的列族,其中只有一行数据。这是查询表时的 CQL Shell 结果:
Connected to Test Cluster at localhost:9160.
[cqlsh 3.1.6 | Cassandra 1.2.8 | CQL spec 3.0.0 | Thrift protocol 19.36.0]
cqlsh:system> use cache
cqlsh:cache> SELECT locationid, payload FROM yellowbotcache;
locationid | payload
------------+------------------
f~123456x | This is a test 3
cqlsh:cache>
Run Code Online (Sandbox Code Playgroud)
这是用于插入表数据然后查询行的 C# 代码:
Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
using (Session sess = cluster.Connect())
{
sess.Execute("INSERT INTO cache.yellowbotcache (locationid, payload) VALUES ('f~123456x', 'This is a test 3');");
RowSet result = sess.Execute("SELECT locationid,payload FROM cache.yellowbotcache WHERE locationid = 'f~123456x';");
var rows = result.GetRows();
if (rows.Count() > 0)
{
foreach (Row row in rows)
{
string payLoad = row.GetValue<string>("payload");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Rows 返回长度为 0 的单行。payload = ... 语句导致“索引超出范围”错误。如果我将 locationid 更改为不正确的值,则查询不会返回任何行。
关于这里发生了什么的任何想法?我上周从 DataStax 站点下载了 Cassandra 和驱动程序。
您正在尝试两次获取数据。首先是在您调用时rows.Count() > 0
,然后是在迭代期间。
一旦执行和迭代,Rows 伪集合就无效了。因此,对您来说最简单的解决方案是首先将所有结果复制到列表中。
尝试这个,
var rows = rowSet.GetRows().ToList();
Run Code Online (Sandbox Code Playgroud)
现在遍历列表。