使用 Java SDK 写入数据时报错:The count of attribute columns exceeds the maximum:128

问题现象

使用表格存储 Java SDK 写入数据时出现如下报错:

The count of attribute columns exceeds the maximum:128

原因分析

使用TableStoreWriter工具类写入数据时,默认的属性列写入个数上限为128。

解决方案

使用 Java SDK 初始化 TableStoreWriter 时,可以通过修改 MaxColumnsCount 参数来提高属性列写入个数的上限。最高可以设置为1024,即一行最多写入1024个属性列。

运行代码前,请在系统环境变量中配置阿里云账号或RAM用户的AccessKey信息。具体操作,请参见配置访问凭证
// yourInstanceName 填写您的实例名称
final String instanceName = "yourInstanceName";
// yourEndpoint 填写您的实例访问地址
final String endPoint = "yourEndpoint";
// 获取环境变量里的 AccessKey ID 和 AccessKey Secret
String accessKeyId = System.getenv("TABLESTORE_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("TABLESTORE_ACCESS_KEY_SECRET");

ClientConfiguration cc = new ClientConfiguration();
cc.setRetryStrategy(new DefaultRetryStrategy()); // 可定制重试策略,如果需要保证数据写入成功率,可采用更激进的重试策略。
AsyncClient asyncClient = new AsyncClient(endPoint, accessKeyId, accessKeySecret, instanceName, cc);

// 初始化
WriterConfig config = new WriterConfig();
config.setMaxBatchSize(4 * 1024 * 1024); // 配置一次批量导入请求的大小限制,默认值为 4 MB。
config.setMaxColumnsCount(128); // 配置一行的列数的上限,默认值为 128。
config.setBufferSize(1024); // 配置内存中最多缓冲的数据行数,默认值为 1024,必须是 2 的指数倍。
config.setMaxBatchRowsCount(100); // 配置一次批量导入的行数上限,默认值为 100。
config.setConcurrency(10); // 配置最大并发数,默认值为 10。
config.setMaxAttrColumnSize(2 * 1024 * 1024); // 配置属性列的值大小上限,默认值为 2 MB。
config.setMaxPKColumnSize(1024); // 配置主键列的值大小上限,默认值为 1 KB。
config.setFlushInterval(10000); // 配置缓冲区 flush 的时间间隔,默认值为 10。单位为秒。

// 配置一个 callback ,OTSWriter 通过该 callback 反馈哪些导入成功,哪些行导入失败,该 callback 只简单的统计写入成功和失败的行数。
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);

关于 TableStoreWriter 的更多信息,请参见使用TableStoreWriter并发写入数据