Consume data

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

Prerequisites

Make sure you have HStreamDB running and accessible.

Consumer

Before you can consume data, you first need to create a Consumer object using the HStreamClient.newConsumer() method:

  1. Consumer consumer = client.newConsumer()
  2. .subscription("test_subscription")
  3. .stream("test_stream")
  4. .maxPollRecords(100)
  5. .pollTimeoutMs(1000)
  6. .build();

A consumer must be associated with a subscription, and a subscription contains a stream.

Once the consumer is created successfully, it can be used to continuously receive data from the subscribed stream.

Receive Raw Records

You can receive receive raw records using the Consumer.pollRawRecords() method:

  1. while(true) {
  2. List<ReceivedRawRecord> receivedRawRecords = consumer.pollRawRecords();
  3. for(ReceivedRawRecord receivedRawRecord: receivedRawRecords) {
  4. System.out.println(receivedRawRecord.getRecordId());
  5. }
  6. }

Receive HRecords

You can receive receive hrecords using the Consumer.pollHRecords() method:

  1. while(true) {
  2. List<ReceivedHRecord> receivedHRecords = consumer.pollHRecords();
  3. for(ReceivedHRecord receivedHRecord: receivedHRecords) {
  4. System.out.println(receivedHRecord.getRecordId());
  5. }
  6. }