Consume data

This page shows how to consume data from HStreamDB using Java SDK.

Prerequisites

Make sure you have HStreamDB running and accessible.

Concepts

A client can consume data from a consumer object. A consumer object will join a consumer group with a subscriptionID, and then the client can fetch data from the subscribed stream.

Each consumer object will contain a RawRecordReceiver and a HRecordReceiver, so users can consume raw records or HRecords according to their needs.

When you received a record from server, you should use responder.ack() to send an ack response to server. HStreamDB now support at-least-once consume, if you don’t send acks, records will be retransmitted by the server after ackTimeoutSecond.

Consume Records

You can consume records like this:

  1. // first, create a subscription for the stream
  2. Subscription subscription =
  3. Subscription
  4. .newBuilder()
  5. .subscription("my_subscription")
  6. .stream("my_stream")
  7. .offset(new SubscriptionOffset(SubscriptionOffset.SpecialOffset.LATEST))
  8. .ackTimeoutSeconds(600)
  9. .build();
  10. client.createSubscription(subscription);
  11. // second, create a consumer attach to the subscription
  12. Consumer consumer =
  13. client
  14. .newConsumer()
  15. .subscription("my_subscription")
  16. .rawRecordReceiver(
  17. ((receivedRawRecord, responder) -> {
  18. System.out.println(receivedRawRecord.getRecordId());
  19. responder.ack();
  20. }))
  21. .build();
  22. // third, start the consumer
  23. consumer.startAsync().awaitRunning();
  • the example use rawRecordReceiver() to consume raw records, if you want to consume HReacord, just use hRecordReceiver() instead.