使用流 api 检索数据

retrieving data using stream api

我正在使用 RavenDB 流 API 从我的数据库实例中检索所有 355,000 DocsToProcess 使用以下代码:

_myDocs = new List<DocsToProcess>();
var query = RavenSession.Query<DocsToProcess>();
using (var enumerator = RavenSession.Advanced.Stream(query))
{
    while (enumerator.MoveNext())
    {
        DocsToProcess u = enumerator.Current.Document;
        _myDocs.Add(u);
     }                
}

但是,抛出以下异常消息:

StreamQuery does not support querying dynamic indexes. It is designed to be used with large data-sets and is unlikely to return all data-set after 15 sec of indexing, like Query() does.

如何在我的 C# 应用程序中正确地遍历 DocsToProcess 类型的所有元素?

documentation says explicitly for unbound results:

Important side notes:

  • the index already exists. Creation of a index won't occur and the query error with an IndexDoesNotExistsException exception.

这就是您的例外情况。您必须为流式传输结果创建 static index

要在不创建静态索引的情况下通过,您可以像这样提供一些最小边界:

    using (var session = store.OpenSession())
    {
        IEnumerator<StreamResult<Employee>> stream =
            session.Advanced.Stream<Employee>("employees/");
        while (stream.MoveNext())
        {
            // ....
        }
    }

与上面的 JHo 类似,我提出的解决方案意味着您不需要为流媒体制作静态索引,因为您依赖于默认索引并使用 StartsWith Raven 客户端中 Stream<T> 的重载。

我们发现以下解决方案适用于我们需要从 Raven 实例获取所有内容的大多数用例。

public IEnumerable<T> GetAll()
{
    var results = new List<T>();

    var conventions = _documentStore.Conventions ?? new DocumentConvention();
    var defaultIndexStartsWith = conventions.GetTypeTagName(typeof(T));

    using(var session = _documentStore.OpenSession())
    {
        using(var enumerator = session.Advanced.Stream<T>(defaultIndexStartsWith))
        {
            while(enumerator.MoveNext())
                results.Add(enumerator.Current.Document);
        }
    }

    return results;
}