Connecting to EventStoreDB

Bearded confident maintenance engineer in white shirt is working in database center
Reading Time: 3 minutes

EventStoreDB is an industrial-strength Event Sourcing database that stores your critical data in streams of immutable events.

It was built from the ground up for Event Sourcing and offers an unrivaled solution for building event-sourced systems.

The core features such as guaranteed write concurrency model, granular stream, and stream APIs make EventStoreDB the best choice for event-sourced systems – especially when compared with other database solutions initially built for other purposes.

And on top of that, it’s open source.

Introduction to EventStoreDB

Each change that took place in the domain is recorded in the database.

Event stores are the database category that is natively focused on store events.

Usually, they do that by having the append-only log as the central point. 

An event store is a different kind of database from traditional databases (graph, document, relational, etc).

Specifically designed to store the history of changes. The state is represented by the append-only log of events.

The events are stored in chronological order, and new events are appended to the previous event.

Example of the business context being kept.

Every stage the invoice has gone through has been kept. Along with the dates and times of all events.

This information would need to be kept for auditing purposes, but imagine applying this level of information to any and all parts of the business.

Having an audit-level amount of information for every event in the system.

The event store doesn’t lose information, and with the right configuration, you can have useful reports and analyses of the data whenever you need it.

EventStore Connection

For your application to start communicating with EventStoreDB, you need to instantiate the client and configure it accordingly.

The EventStoreConnection class maintains a full-duplex connection between the client and the EventStoreDB server.

 EventStoreConnection is thread-safe, and we recommend that you create one node per application.

Required Packages

Install the client SDK package to your project.

# Maven

<dependency>

  <groupId>com.eventstore</groupId>

  <artifactId>db-client-java</artifactId>

  <version>0.5</version>

</dependency>

# Gradle

implementation ‘com.eventstore:db-client-java:0.5’

# SBT

libraryDependencies += “com.eventstore” % “db-client-java” % “0.6”

Connection String

Each SDK has its own way to configure the client, but it’s always possible to use the connection string. You can use https://configurator.eventstore.com/ to generate the connection string for your EventStoreDB deployment.

The connection string generated with this tool should work with each official SDK of EventStoreDB.

Creating a Client

First thing first, we need a client.

The client instance can be used as a singleton across the whole application. It doesn’t need to open or close the connection.

EventStoreDBClientSettings settings = EventStoreDBConnectionString.parse(“{connectionString}”);

EventStoreDBClient client = EventStoreDBClient.create(settings);

Creating an Event

You can write anything to EventStoreDB as events. The client needs a byte array as the event payload. Normally, you’d use a serialized object and it’s up to you to choose the serialization method.

User-defined server-side projections require events to be serialized to JSON format.

The code snippet below creates an event object instance.

TestEvent event = new TestEvent();

event.setId(UUID.randomUUID().toString());

event.setImportantData(” My first event!”);

EventData eventData = EventData

        .builderAsJson(“TestEvent”, event)

        .build();

Appending Events

Each event in the database has its own unique identifier (UUID).

The database uses it to ensure idempotent writes, but it only works if you specify the stream revision when appending events to the stream.

client.appendToStream(“some-stream”, eventData)

        .get();

Reading Events

When you read events from the stream, you get a collection of ResolvedEvent structures. The event payload is returned as a byte array and needs to be deserialized.

ReadStreamOptions options = ReadStreamOptions.get()

        .forwards()

        .fromStart()

        .maxCount(10);

ReadResult result = client.readStream(“some-stream”, options)

        .get();

Advantages of EventStoreDB

  • Multiple client interfaces.
  • Immutable data store.
  • High availability.
  • Optimistic concurrency checks.
  • Multiple hosting options.
  • Atom subscriptions.
  • Projections.
  • Subscribe to streams with competing consumers.

Conclusion

Traditional databases are designed to lose data. EventStoreDB captures and retains all your data at an atomic level.

In sequence from anywhere inside your organization, and works seamlessly with any downstream system in real-time.  EventStoreDB is the real source of truth built for today’s fast-moving event-driven systems.

Reference: https://www.eventstore.com/blog/new-eventstoredb-java-client

Written by 

Chiranjeev kumar is a Software intern at Knoldus. He is passionate about the java programming . He is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. He is a quick learner & curious to learn new technologies. His hobbies include listening music , playing video games.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading