我正在尝试使用C#在Cassandra中使用手动分页。
以下是版本详细信息:
它是驱动程序版本3.11.0
我的服务器版本是[cqlsh 5.0.1|Cassandra 3.11.4|CQL规范3.4.4|Native protocol v4]
下面是我正在尝试的代码
var cluster = Cluster.Builder()
.AddContactPoints("x.x.x.x", "x.x.x.x", "x.x.x.x")
.WithCompression(CompressionType.Snappy)
.Build();
var session = cluster.Connect("shekhar");
var statement = new SimpleStatement("select * from table ALLOW FILTERING");
statement.SetAutoPage(false);
statement.SetPageSize(1000);
var rs = await session.ExecuteAsync(statement);
int rowNo = 0;
foreach (var row in rs)
Console.WriteLine("{0}: Cooment has Id {1}, has tweet {2}", rowNo++, row.GetValue<BigInteger>("id"), row.GetValue<string>("xxx"));
//rs.FetchMoreResults();
//Console.WriteLine(rs.IsExhausted());
//Console.WriteLine(rs.IsFullyFetched);
//Console.WriteLine(rs.PagingState);
通过设置语句.SetAutoPage(false(它只获取一页
最后一条语句Console.WriteLine(rs.IsFullyFetched);
和Console.WriteLine(rs.IsFullyFetched);
的值是true
,因此也使用它们不会有任何区别。
我也遵循了关于https://docs.datastax.com/en/developer/csharp-driver/3.13/features/paging/在下一个语句中使用PagingState
的部分手动分页
使用此功能可以保持一次又一次地提取旧数据。
这是我对同一的实现
var statement = new SimpleStatement("select * from table ALLOW FILTERING");
statement.SetAutoPage(false);
statement.SetPageSize(1000);
int rowNum = 0;
await GetAllData_PagedData(session, statement, null, rowNum);
Console.ReadLine();
static async Task GetAllData_PagedData(ISession session, IStatement statement, byte[] pagingState, int rowNum)
{
var statement2 = statement
.SetPagingState(pagingState);
var rs2 = session.Execute(statement2);
var pagingState1 = rs2.PagingState;
foreach (var row in rs2)
{
Console.WriteLine("{0}: Cooment has Id {1}, has tweet {2}", rowNum++, row.GetValue<BigInteger>("id"), row.GetValue<string>("column2"));
}
if (!rs2.IsFullyFetched)
await GetAllData_PagedData(session, statement2, pagingState1, rowNum);
else
Console.WriteLine("All Data fetched...");
}
尝试用if (pagingState1 != null)
替换if (!rs2.IsFullyFetched)
。
在禁用AutoPage
的情况下,IsFullyFetched
方法总是返回true
,因为它旨在与RowSet
的自动分页功能一起使用。