Problem description
When I use Tablestore SDK for Java to write data to a data table, the following error is reported:
The count of attribute columns exceeds the maximum:128Cause
When you use the TableStoreWriter tool class to write data, you can write up to 128 attribute columns by default.
Solution
When you use Tablestore SDK for Java to initialize the TableStoreWriter, you can modify the MaxColumnsCount parameter to increase the maximum number of attribute columns that you can write. The maximum value of the parameter is 1024, which specifies that you can use TableStoreWriter to write up to 1,024 attribute columns.
Before you run the code, configure the AccessKey pair of your Alibaba Cloud account or RAM user by using environment variables. For more information, see Configure access credentials.
// Specify the name of the instance.
final String instanceName = "yourInstanceName";
// Specify the endpoint of the instance.
final String endPoint = "yourEndpoint";
// Obtain the AccessKey ID and AccessKey secret from the environment variables.
String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");
ClientConfiguration cc = new ClientConfiguration();
cc.setRetryStrategy(new DefaultRetryStrategy()); // Specify a custom retry policy. To ensure a high success rate, you can use a more aggressive retry policy.
AsyncClient asyncClient = new AsyncClient(endPoint, accessKeyId, accessKeySecret, instanceName, cc);
// Initialize the TableStoreWriter.
WriterConfig config = new WriterConfig();
config.setMaxBatchSize(4 * 1024 * 1024); // Specify the maximum size of a batch import request. By default, the maximum size of a batch import request is 4 MB.
config.setMaxColumnsCount(128); // Specify the maximum number of columns that can be written to a row. Default value: 128.
config.setBufferSize(1024); // Specify the maximum number of data rows that can be buffered in the memory. Default value: 1024. The value of this parameter must be an exponential multiple of 2.
config.setMaxBatchRowsCount(100); // Specify the maximum number of rows that can be imported at the same time. Default value: 100.
config.setConcurrency(10); // Specify the maximum number of parallel requests that TableStoreWriter uses to write data in the buffer to Tablestore. Default value: 10.
config.setMaxAttrColumnSize(2 * 1024 * 1024); // Specify the maximum size of the values of the attribute columns. By default, the maximum size of the values of the attribute columns is 2 MB.
config.setMaxPKColumnSize(1024); // Specify the maximum size of the values of the primary key columns. By default, the maximum size of the values of the primary key columns is 1 KB.
config.setFlushInterval(10000); // Specify the interval at which the flush operation is triggered in the buffer. Default value: 10. Unit: seconds.
// Configure a callback. OTSWriter uses this callback to report the rows that are imported or failed to be imported. This callback counts the number of rows that are imported and the number of rows that failed to be imported.
AtomicLong succeedCount = new AtomicLong();
AtomicLong failedCount = new AtomicLong();
TableStoreCallback<RowChange, ConsumedCapacity> callback = new SampleCallback(succeedCount, failedCount);
ExecutorService executor = Executors.newFixedThreadPool(2);
TableStoreWriter tablestoreWriter = new DefaultTableStoreWriter(asyncClient, tableName, config, callback, executor);For more information about TableStoreWriter, see Use TableStoreWriter to concurrently write data.