使用 java API 或 HTTP API 查询 EventStoreDB [英] Querying EventStoreDB with java API or HTTP API

查看:75
本文介绍了使用 java API 或 HTTP API 查询 EventStoreDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用出色的 Greg Young 出色的 Event Store DB,而且它很好.但是文档很糟糕,它已经过时,有时不正确,并且通过网站分发.有时您必须查找多个不相关的页面才能破解文档背后的代码!

I have been using the brilliant Event Store DB by brilliant Greg Young lately and its fine. But the documentation is awful, it's outdated, sometimes incorrect, and distributed through the website. Sometimes you have to lookup multiple non relevant pages to break the code behind documentation!

无论如何,不​​仅是文档,而且 Java API 也很糟糕,大多数情况下您必须改用 HTTP API.

Anyway, not only the documentation, but also the Java API sucks and most of the times you have to use the HTTP API instead.

我在 HTTP API 上构建了自己的 Java API.

I have built my own Java API upon the HTTP API.

我一直在为这些投影而苦苦挣扎,我能够创建多个投影(连续的、一次性的、瞬态的)并且它工作得很好,结果流填充了投影发出的事件.

I have been struggling with the projections and I was able to create multiple projections (continuous, one time, transient) and it works just fine and the resulting streams are populated with the events emitted by projection.

所以现在,我知道我应该如何创建新的投影,以创建充满我想要的事件的流,这些事件由 when 子句中的给定条件过滤

So now, I know how should I create new projections which will create streams filled with my desired events filtered by the given criteria in the when clause

我还在文档中读到我们可以对事件存储进行时间查询,但是我们应该在 Catch-up 订阅的帮助下填充另一个读取模型,我对此很好,并且知道 CQRS 是什么.我正在使用 MongoDB 为报告创建读取模型.

I also read in the documentation that we can do temporal queries on event store, but we should populate another read model by the help of Catch-up subscription and I am fine with that and know what CQRS is. I'm using MongoDB to create read models for reports.

我遇到的困难是时间查询.我找不到任何关于我应该如何进行查询的文档.但是我猜我应该首先为每个查询创建一个一次性投影,然后我应该将结果发送到一个新的流名称query-{id}".然后我立即尝试从新查询流中读取事件,但是创建查询流并用结果事件填充它需要时间.因此,我的查询 API 创建了一个投影并立即在结果流上请求事件,但它得到 404,这意味着投影尚未开始处理和生成新流.

The thing I'm having difficulties with is the temporal queries. I couldn't find any documentation around how should I do queries. But I guessed that I should first create a one-time projection for each query and I should emit the results to a new stream name "query-{id}". Then I immediately try to read events off of the new query stream, but it takes time to create the query stream and fill it with the resulting events. So my API for queries creates a projection and immediately asks for events on the resulting stream, but it gets 404 which means the projection has not started processing and producing new stream yet.

那么,有没有什么办法可以通过事件存储一个投影并询问它的事件?

So, is there any way to pass event store a projection and ask it for the events?

我理解的一件事是实际上没有办法实时查询事件存储,但是我们应该创建一个投影来创建一个流并用结果填充它,同时我们应该继续询问流是否是已创建以及是否已完成投影.

One thing I understood was that there is no way actually to query the event store in real time, but we should create a projection which will create a stream and fill it with result and mean while we should keep asking whether the stream is created and whether the projection is done.

我为我的每个聚合创建了一个新流,一切正常.我有一个单独的有界上下文来报告整个系统,它使用 MongoDB 并且效果很好.但有时我需要查询事件以检查不变量.例如,我需要检查产品"是否存在.标题是否唯一并抛出异常.为了进行这样的检查,我应该为每个有界上下文中的聚合创建一个单独的读取模型.但是我只想通过向事件存储询问所有ProductCreated"来执行此类查询.具有相同标题的事件,然后我想处理所有这些事件,看看我们是否有一个具有相同标题的产品.

I create a new stream for each of my aggregates and everything just works fine. And I have a separate bounded context for the reporting of the whole system and it uses MongoDB and that works fine to. But sometimes I need to query the events to check invariants. For example I need to check whether the "Product" title is unique or not and throw exception. In order to do such check, I should create a separate read model for aggregates inside each bounded context. But I want to do such queries simply by asking the event store for all "ProductCreated" events with the same title and then I want to process all those events to see if we have got a Product with the same title.

那么,我如何向事件存储询问所有标题等于某个值的 ProductCreated 事件并立即获得结果?

So how could I ask the event store for all ProductCreated events where title equals some value and get the result immediately?

也许事件存储数据库不是为此类用例设计的?!

Or maybe event store db is not designed for such usecases?!

推荐答案

很遗憾听到您对文档的体验.我不确定您是否注意到,但我们几周前发布了新文档,旧文档网站在横幅中明确指出它已过时.

Sorry to hear about your experience with the documentation. I am not sure if you noticed, but we published the new docs a couple of weeks ago and the old docs website clearly states in the banner that it's obsolete.

JVM 客户端(不是 Java 客户端)不是由 Event Store 制作的.新客户端目前正在开发中,但它是 20.6 客户端,仅支持 gRPC.

The JVM client (not Java client) is not made by Event Store. The new client is being worked on right now but it's 20.6 client and will only support gRPC.

您的问题中有很多内容,我不确定我是否理解所有内容.如果可能,最好将问题分开.

There are quite a few things in your question, and I am not sure I understand all of it. It would be great to separate questions, if possible.

我可以回复一个我理解的,关于阅读相同类型的事件.系统 按事件类型 投影产生您可能愿意从中读取的流(在您的示例中为 $et-ProductCreated),但我不确定它是否能解决您的用例,因为您可能还有诸如 ProductRenamed 之类的事件,您也需要处理它.

I can reply to the one I understand, about reading events of the same type. The system by event type projection produces streams that you might be willing to read from ($et-ProductCreated in your example), but I am not sure if it solves your use-case since you might also have events like ProductRenamed and you'd need to handle it too.

我不会真正将这种情况视为不变量"因为如果两个产品意外获得相同的标题,您的系统不太可能变得不一致.这取决于您希望在系统中同时注册和重命名的产品数量.如果您以每秒 100 个产品的速度注册新产品,您可能无论如何都无法读取所有 ProductCreated 事件来确定新名称是否与另一个名称重复.如果您每小时有一个产品,那么对 MongoDB 进行简单的投影将是检查重复项的最简单、最快捷的方法.

I won't really see this case as an "invariant" since your system would unlikely become inconsistent if two products accidentally get the same title. It depends on the number of products you expect to be registered and renamed simultaneously in the system. If you have new products being registered with a speed of 100 products per second, you probably won't be able to read all ProductCreated events anyway to find out if the new name is a duplicate of another. If you have like one product per hour, having a simple projection to MongoDB would be the easiest and fastest way to check for duplicates.

我建议打开另一个关于时间查询的问题.

I'd suggest opening another question concerning temporal queries.

这篇关于使用 java API 或 HTTP API 查询 EventStoreDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆