Read and write data
Use the DataHub Java SDK to read and write data.
Usage notes
-
You can read data from shards in the
CLOSEDorACTIVEstate. -
You can write data only to shards in the
ACTIVEstate.
We recommend using the High-Level SDK for reading and writing unless you have specific requirements. The High-Level SDK wraps the read/write APIs of the Low-Level SDK and is better suited for data I/O scenarios. For details, see Producer.
Read data
1. Obtain a cursor.
-
To read data from a topic, specify a shard and a cursor. There are four cursor types:
-
OLDEST: Points to the oldest available record in the shard.
-
LATEST: Points to the most recently written record.
-
SEQUENCE: Points to a record at a specified sequence number.
-
SYSTEM_TIME: Points to the first record with a timestamp equal to or greater than the specified timestamp.
-
-
Cursor selection guidelines:
-
To read data from the beginning, use
OLDEST. -
To check the most recent data, use
LATEST. -
To read the last N records, use
SEQUENCEwithSEQUENCE - N. -
To sample and validate data after a specific point in time, use
SYSTEM_TIME. Data reading starts from the position atSEQUENCE + 1.
NoteEnsure the data is within its retention period. Otherwise, a read operation will fail.
-
-
Once a cursor is obtained, pass it to the
getRecordsmethod.NoteDataHub provides a subscription feature for data consumption. The read operation is primarily for sampling and data validation.
Obtain a cursor
Read data using the getRecords operation
Write data
Starting from version 2.12, the server supports the putRecordsByShard
operation. In earlier versions, only the putRecords
operation is available. With putRecordsByShard, you must specify the target shard; otherwise, data is written to the first shard in the ACTIVE state by default. Both operations accept a List of RecordEntry objects as the records parameter, and all records must be of the same type—either Tuple or Blob. DataHub supports two types of data writing:
-
Shard-specific writing (server version 2.12 or later), using the
putRecordsByShardoperation. -
Mixed-shard writing, using the
putRecordsoperation.
With putRecords
operation, check the PutRecordsResult to determine whether the write succeeded. In contrast, putRecordsByShard
operation indicates success or failure directly through exceptions. If the server version supports it, use putRecordsByShard.
Parameters
|
Name |
Type |
Description |
|
projectName |
String |
The project name. |
|
topicName |
String |
The topic name. |
|
shardId |
String |
The shard ID. |
Exception description
|
Class name |
Error code |
Exception description |
|
DatahubClientException |
- |
The base class for all exceptions. |