public interface TableStoreWriter
addRowChange(com.alicloud.openservices.tablestore.model.RowChange) interface to write a row to TableStoreWriter, it does not mean that the row has been written to TableStore yet. The row will be stored in the local buffer of TableStoreWriter, waiting for flush. Only after a successful flush will the row be written to TableStore.
- The order of rows written to TableStoreWriter does not guarantee consistency with the final order of rows written to TableStore.
- All write operations through TableStoreWriter should be idempotent as TableStoreWriter internally enables retries by default, which may result in multiple writes for a single row.
- The timing of flushing the buffer in TableStoreWriter is controlled by two factors: one is the flushInterval, which triggers regular flushes based on time intervals; the other is
WriterConfig.maxBatchSize, which determines whether to flush based on the amount of data in the buffer.
- When writing data, TableStoreWriter automatically retries rows that fail to import, but it does not guarantee that all rows will eventually be successfully written after retries (e.g., if a row includes an existence check and already exists, it will never succeed).
If some data cannot be written successfully after several retries, these rows will be considered dirty data and fed back to the user via TableStoreCallback.
- Before writing data to TableStoreWriter, make sure to register a Callback. Otherwise, if data fails before registering the Callback, the failed rows will be discarded.
- Before exiting the program, explicitly call flush() or close() to flush any remaining data in the buffer. Otherwise, this data will be lost.
Data processing flow for bulk imports using TableStoreWriter:
1. The addRowChange interface is thread-safe and supports concurrent writes from user threads.
2. Data written via the addRowChange interface is temporarily stored in the buffer.
3. Each TableStoreWriter starts a background import thread that flushes the buffered data. To improve import efficiency, this thread asynchronously sends multiple RPCs in parallel, with the concurrency level configurable.
4. Both successful and failed rows are fed back to the user via callback, which is executed in another ExecutorService (customizable by the user).
How users can use TableStoreWriter:
1. Initialize TableStoreWriter, configuring options such as RestrictionConfig, buffer Queue, Callback ExecutorService, etc.
2. Call addRowChange(com.alicloud.openservices.tablestore.model.RowChange) to concurrently write data into TableStoreWriter.
3. Once finished writing, call flush() to flush the data in the buffer.
4. Call close() to close TableStoreWriter and release resources.| 限定符和类型 | 方法和说明 |
|---|---|
void |
addRowChange(List<RowChange> rowChanges,
List<RowChange> dirtyRows)
Batch write rows to the local buffer.
|
void |
addRowChange(RowChange rowChange)
Add a row of data to the local buffer.
|
Future<WriterResult> |
addRowChangeWithFuture(List<RowChange> rowChanges)
Batch write rows to the local buffer.
|
Future<WriterResult> |
addRowChangeWithFuture(RowChange rowChange)
The interface function is the same as
addRowChange(com.alicloud.openservices.tablestore.model.RowChange),
but it will return a Future of the write result, indicating the success or failure status of writing this row. |
void |
close()
Closes the TableStoreWriter.
|
void |
flush()
Actively flush the data in the buffer.
|
TableStoreCallback<RowChange,ConsumedCapacity> |
getCallback()
已过时。
please change to
getResultCallback() |
TableStoreCallback<RowChange,RowWriteResult> |
getResultCallback()
Get the set Callback.
|
WriterConfig |
getWriterConfig()
Get the configuration of the limit item.
|
WriterStatistics |
getWriterStatistics()
Get the statistical information during data import.
|
void |
setCallback(TableStoreCallback<RowChange,ConsumedCapacity> callback)
已过时。
please change to
setResultCallback(TableStoreCallback) |
void |
setResultCallback(TableStoreCallback<RowChange,RowWriteResult> callback)
Set Callback, which will provide feedback when data writing succeeds or fails.
|
boolean |
tryAddRowChange(RowChange rowChange)
Same with
addRowChange(RowChange), but it won't be blocked if the buffer is full. |
void addRowChange(RowChange rowChange) throws ClientException
WriterConfig.maxPKColumnSize and WriterConfig.maxAttrColumnSize.
- Whether the number of attribute columns in this row exceeds WriterConfig.maxColumnsCount.
- Whether the size of this row exceeds WriterConfig.maxBatchSize.
- Whether there are any column names in the attribute columns that are the same as those in the primary key columns.
If the data is determined to be dirty before being written to the buffer, this portion of the data will not trigger a CallBack invocation.
These checks introduce additional CPU consumption on the SDK side, but they are necessary to reduce unnecessary RPC overhead and prevent dirty data from contaminating rows in the same batch write operation.
Note: If the buffer is full, this operation will be blocked.
If the row is judged to be dirty data, this interface will throw a ClientException.rowChange - The row to be writtenClientException - If the row is determined to be dirty dataFuture<WriterResult> addRowChangeWithFuture(RowChange rowChange) throws ClientException
addRowChange(com.alicloud.openservices.tablestore.model.RowChange),
but it will return a Future of the write result, indicating the success or failure status of writing this row.rowChange - The row to be written.ClientException - If the row is determined to be dirty data.boolean tryAddRowChange(RowChange rowChange) throws ClientException
addRowChange(RowChange), but it won't be blocked if the buffer is full.rowChange - ClientExceptionvoid addRowChange(List<RowChange> rowChanges, List<RowChange> dirtyRows) throws ClientException
addRowChange(RowChange).
If there is any dirty data in the rows being written in the batch, this function will throw a ClientException,
and all dirty data will be written to dirtyRows.rowChanges - Rows for batch writingdirtyRows - List used to return the dirty dataClientException - if there is any dirty dataFuture<WriterResult> addRowChangeWithFuture(List<RowChange> rowChanges) throws ClientException
addRowChange(RowChange); dirty data will be directly updated in the WriterResponse statistics.rowChanges - Rows for batch writingClientException - If there is dirty datavoid setCallback(TableStoreCallback<RowChange,ConsumedCapacity> callback)
setResultCallback(TableStoreCallback)void setResultCallback(TableStoreCallback<RowChange,RowWriteResult> callback)
AsyncClientInterface,
where the callback corresponds one-to-one with each request and can be independent.
However, the Callback here corresponds to RowChange, and all RowChanges share this callback.callback - TableStoreCallback<RowChange,ConsumedCapacity> getCallback()
getResultCallback()getResultCallback()TableStoreCallback<RowChange,RowWriteResult> getResultCallback()
WriterConfig getWriterConfig()
WriterStatistics getWriterStatistics()
void flush()
throws ClientException
ClientExceptionvoid close()
addRowChange(com.alicloud.openservices.tablestore.model.RowChange) is called to write data into the buffer during or after the close process, there is no guarantee that this part of the data will be written to TableStore.
The mutual exclusion between addRowChange and close operations must be ensured by the caller. It is essential to ensure that no other threads continue to use this writer before calling close; otherwise, unexpected behavior may occur.Copyright © 2025. All Rights Reserved.