Use Tablestore SDK for Java to create a BaseData, Stream, or BaseAndStream tunnel for a data table.
Prerequisites
Feature description
Call createTunnel to create a tunnel for a data table. You can create multiple tunnels for the same data table. The tunnel type determines the data to consume: BaseData consumes only full data, Stream consumes only incremental data, and BaseAndStream consumes full data and then incremental data.
If Stream is disabled for a data table, creating a Stream or BaseAndStream tunnel automatically enables Stream and sets the retention period of incremental logs to seven days.
CreateTunnelResponse createTunnel(CreateTunnelRequest request)
throws TableStoreException, ClientException
The following sample code creates a BaseData tunnel named example_tunnel for the example_table data table.
String tableName = "example_table";
String tunnelName = "example_tunnel";
CreateTunnelRequest request =
new CreateTunnelRequest(
tableName, tunnelName, TunnelType.BaseData);
CreateTunnelResponse response = tunnelClient.createTunnel(request);
System.out.println("TunnelId: " + response.getTunnelId());
Parameters
CreateTunnelRequest contains the following parameters:
|
Name |
Type |
Description |
|
tableName (required) |
|
The name of the data table. |
|
tunnelName (required) |
|
The tunnel name. |
|
tunnelType (required) |
|
The tunnel type. |
|
streamTunnelConfig (optional) |
|
The incremental data range configuration for |
|
streamRecordOptions (optional) |
|
The incremental record configuration for |
Incremental data range
streamTunnelConfig is of the StreamTunnelConfig type and contains the following parameters:
|
Name |
Type |
Description |
|
flag (optional) |
|
The start position used when |
|
startOffset (optional) |
|
The start timestamp of incremental data. Unit: milliseconds. Valid range: [Current system time - Stream retention period + 5 minutes, current system time). If this parameter is specified, |
|
endOffset (optional) |
|
The end timestamp of incremental data. Unit: milliseconds. If both timestamps are specified, this parameter must be greater than |
The Stream retention period is the retention period of incremental logs. The maximum value is seven days. You can set the period when you enable Stream for a data table. The period cannot be modified after it is set.
Incremental record content
streamRecordOptions is of the StreamRecordOptions type and contains the following parameters:
|
Name |
Type |
Description |
|
getVersionGeneratorValue (optional) |
|
Specifies whether incremental records contain the value generated by the version generator. Default value: |
|
getSysColumns (optional) |
|
Specifies whether incremental records contain system columns. Default value: |
|
getNewRowInfo (optional) |
|
Specifies whether incremental records contain the latest row information. Default value: |
|
oldColumnsToGet (optional) |
|
The attribute columns to return from the original row. |
|
newColumnsToGet (optional) |
|
The attribute columns to return from the latest row. |
Incremental record columns
streamRecordOptions.oldColumnsToGet and streamRecordOptions.newColumnsToGet are of the StreamColumn type and contain the following parameters:
|
Name |
Type |
Description |
|
columnType (required) |
|
The attribute column selection mode. |
|
columnNames (optional) |
|
The names of the attribute columns to return when |
Response
CreateTunnelResponse contains the following response field:
|
Field |
Type |
Description |
|
tunnelId |
|
The ID of the created tunnel. Call |
Scenarios
Specify an incremental data range
The following sample code creates a Stream tunnel and specifies the incremental data range of the last hour.
long endTime = System.currentTimeMillis() - 1_000L;
long startTime = endTime - 60 * 60 * 1000L;
StreamTunnelConfig streamConfig =
new StreamTunnelConfig(startTime, endTime);
CreateTunnelRequest request =
new CreateTunnelRequest(
"example_table",
"example_stream_tunnel",
TunnelType.Stream);
request.setStreamTunnelConfig(streamConfig);
CreateTunnelResponse response = tunnelClient.createTunnel(request);
System.out.println("TunnelId: " + response.getTunnelId());
Configure incremental record content
The following sample code creates a Stream tunnel and configures incremental records to return the version generator value, system columns, latest row information, the value attribute column from the original row, and all attribute columns from the latest row.
StreamColumn oldColumns =
new StreamColumn(StreamColumnType.SPECIFIED_COLUMN);
oldColumns.addColumnName("value");
StreamRecordOptions recordOptions = new StreamRecordOptions();
recordOptions.setGetVersionGeneratorValue(true);
recordOptions.setGetSysColumns(true);
recordOptions.setGetNewRowInfo(true);
recordOptions.setOldColumnsToGet(oldColumns);
recordOptions.setNewColumnsToGet(
new StreamColumn(StreamColumnType.ALL_COLUMNS));
CreateTunnelRequest request =
new CreateTunnelRequest(
"example_table", "example_stream_tunnel", TunnelType.Stream);
request.setStreamRecordOptions(recordOptions);
CreateTunnelResponse response = tunnelClient.createTunnel(request);
System.out.println("TunnelId: " + response.getTunnelId());