This project provides materialized view extensions to RxJava. It allows you to generate (and access) materialized views from stream of events.
Right now RxJava View project supports the following types of materialized views:
- document views (i.e. mapping stream of events into persistence store supporting JSON-like data types)
Document views materialize incoming events into a persistence engined based on documents i.e. JSON-like data types. It could be MongoDB, PostgreSQL JSON, Cassandra, ElasticSearch and many more. Document views operate on Java maps as a way to represent documents.
First of all in order to start using document view, add an appropriate dependency to your Maven project. For example for in-memory document view add the following entry:
<dependency>
<groupId>com.github.hekonsek</groupId>
<artifactId>rxjava-view-memory</artifactId>
<version></version>
</dependency>
Or the following one for ElasticSearch document view:
<dependency>
<groupId>com.github.hekonsek</groupId>
<artifactId>rxjava-view-elasticsearch</artifactId>
<version></version>
</dependency>
You can use the following code to generate materialized view from an observable:
import static com.github.hekonsek.rxjava.event.Events.event;
import static com.github.hekonsek.rxjava.event.Headers.ADDRESS;
import static com.github.hekonsek.rxjava.event.Headers.KEY;
import static com.github.hekonsek.rxjava.view.document.MaterializeDocumentViewTransformation.materialize;
...
DocumentView view = new InMemoryDocumentView();
Map<String, Object> document = ImmutableMap.of("foo", "bar", "timestamp", new Date());
Map<String, Object> headers = ImmutableMap.of(ADDRESS, "collection", KEY, "key");
Event<Map<String, Object>> event = event(headers, document);
Observable.just(event).
compose(materialize(view)).subscribe();
As you can see materialize
transformation relies on RxJava Event model conventions to dispatch
incoming event into a proper collection with a proper key. In particular standard address
and key
headers of an incoming event are used to
indicate target collection and event key respectively.
If you would like to count the number of documents in materialized collection, use the following code:
Single<Long> countObservable = materializedView.count("collection");
countObservable.subscribe(count ->
assertThat(count).isEqualTo(3)
);
In order to fetch single document by key, use findById
operation:
Maybe<Map<String, Object>> documentObservable = materializedView.findById("collection");
documentObservable.subscribe(document ->
assertThat(document).isInstanceOf(Map.class)
);
This project is distributed under Apache 2.0 license.