Azure 批量插入:错误请求错误
Azure Batch Insert: Bad Request Error
我尝试在 Azure Table 存储中插入多个实体时出现以下错误:
com.microsoft.azure.storage.table.TableServiceException: Bad Request
at com.microsoft.azure.storage.table.TableBatchOperation.postProcessResponse(TableBatchOperation.java:525)
at com.microsoft.azure.storage.table.TableBatchOperation.postProcessResponse(TableBatchOperation.java:433)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:146)
下面是批量插入的Java代码:
public BatchInsertResponse batchInsert(BatchInsertRequest request){
BatchInsertResponse response = new BatchInsertResponse();
String erpName = request.getErpName();
HashMap<String,List<TableEntity>> tableNameToEntityMap = request.getTableNameToEntityMap();
HashMap<String,List<TableEntity>> errorMap = new HashMap<String,List<TableEntity>>();
HashMap<String,List<TableEntity>> successMap = new HashMap<String,List<TableEntity>>();;
CloudTable cloudTable=null;
for (Map.Entry<String, List<TableEntity>> entry : tableNameToEntityMap.entrySet()){
try {
cloudTable = azureStorage.getTable(entry.getKey());
} catch (Exception e) {
e.printStackTrace();
}
// Define a batch operation.
TableBatchOperation batchOperation = new TableBatchOperation();
List<TableEntity> value = entry.getValue();
for (int i = 0; i < value.size(); i++) {
TableEntity entity = value.get(i) ;
batchOperation.insertOrReplace(entity);
if (i!=0 && i % batchSize == 0) {
try {
cloudTable.execute(batchOperation);
batchOperation.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
cloudTable.execute(batchOperation);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我将 batchSize 值分配给 10,上面的代码工作正常,但如果我将分配给 1000 或 100,它将抛出 Bad request 错误。
请帮我解决这个错误。我正在使用 Spring 引导和 Azure 存储 Java SDK 版本 4.3.0。
正如 Aravind 提到的,400 错误通常意味着您的数据有问题。从this
link开始,如果不满足以下一个或多个条件,实体批量交易将失败:
- 作为事务的一部分进行操作的所有实体必须具有相同的 PartitionKey 值。
- 一个实体在事务中只能出现一次,并且只能对其执行一次操作。
- 事务可以包含最多 100 个实体,并且其总有效负载的大小不能超过 4 MB。
- 所有实体均受Understanding the Table Service Data Model中所述的限制。
请根据这四项规则检查您的实体,确保您没有违反其中一项规则。
我尝试在 Azure Table 存储中插入多个实体时出现以下错误:
com.microsoft.azure.storage.table.TableServiceException: Bad Request
at com.microsoft.azure.storage.table.TableBatchOperation.postProcessResponse(TableBatchOperation.java:525)
at com.microsoft.azure.storage.table.TableBatchOperation.postProcessResponse(TableBatchOperation.java:433)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:146)
下面是批量插入的Java代码:
public BatchInsertResponse batchInsert(BatchInsertRequest request){
BatchInsertResponse response = new BatchInsertResponse();
String erpName = request.getErpName();
HashMap<String,List<TableEntity>> tableNameToEntityMap = request.getTableNameToEntityMap();
HashMap<String,List<TableEntity>> errorMap = new HashMap<String,List<TableEntity>>();
HashMap<String,List<TableEntity>> successMap = new HashMap<String,List<TableEntity>>();;
CloudTable cloudTable=null;
for (Map.Entry<String, List<TableEntity>> entry : tableNameToEntityMap.entrySet()){
try {
cloudTable = azureStorage.getTable(entry.getKey());
} catch (Exception e) {
e.printStackTrace();
}
// Define a batch operation.
TableBatchOperation batchOperation = new TableBatchOperation();
List<TableEntity> value = entry.getValue();
for (int i = 0; i < value.size(); i++) {
TableEntity entity = value.get(i) ;
batchOperation.insertOrReplace(entity);
if (i!=0 && i % batchSize == 0) {
try {
cloudTable.execute(batchOperation);
batchOperation.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
cloudTable.execute(batchOperation);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我将 batchSize 值分配给 10,上面的代码工作正常,但如果我将分配给 1000 或 100,它将抛出 Bad request 错误。
请帮我解决这个错误。我正在使用 Spring 引导和 Azure 存储 Java SDK 版本 4.3.0。
正如 Aravind 提到的,400 错误通常意味着您的数据有问题。从this
link开始,如果不满足以下一个或多个条件,实体批量交易将失败:
- 作为事务的一部分进行操作的所有实体必须具有相同的 PartitionKey 值。
- 一个实体在事务中只能出现一次,并且只能对其执行一次操作。
- 事务可以包含最多 100 个实体,并且其总有效负载的大小不能超过 4 MB。
- 所有实体均受Understanding the Table Service Data Model中所述的限制。
请根据这四项规则检查您的实体,确保您没有违反其中一项规则。