0
我正嘗試用Azure插入一批實體。 對於我的「CustomerEntity」,所有的作品如預期,但對我的「OrderEntity」,我只能在我的批處理操作的單一實體...Azure不能插入超過1個實體(批量插入) - JAVA
這裏是我的代碼:
public void batchInsertTransaction(ArrayList<Transaction> transactions){
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount =
CloudStorageAccount.parse(storageConnectionString);
// Create the table client.
CloudTableClient tableClient = storageAccount.createCloudTableClient();
// Define a batch operation.
TableBatchOperation batchCustomerOperation = new TableBatchOperation();
TableBatchOperation batchOrderOperation = new TableBatchOperation();
// Create a cloud table object for the table.
CloudTable cloudCustomerTable = tableClient.getTableReference("Customer");
CloudTable cloudOrderTable = tableClient.getTableReference("Order");
String partitionKey = "transaction-" + PropertiesManager.country + "-" + PropertiesManager.city;
for(int i = 0; i < transactions.size(); i++){
Transaction transaction = transactions.get(i);
Order order = transaction.getOrder();
Customer customer = transaction.getCustomer();
// Create a customer entity to add to the table.
CustomerEntity customerEntity = new CustomerEntity(partitionKey, customer.getGlobalId());
customerEntity.setCountry(customer.getCountry());
customerEntity.setName(customer.getName());
customerEntity.setGlobalId(customer.getGlobalId());
batchCustomerOperation.insertOrReplace(customerEntity);
OrderEntity orderEntity = new OrderEntity(partitionKey, order.getGlobalId());
orderEntity.setComplete(order.getComplete());
orderEntity.setCustomerId(order.getCustomerId());
orderEntity.setGlobalId(order.getGlobalId());
orderEntity.setOrderDate(order.getOrderDate());
orderEntity.setPrice(order.getPrice());
orderEntity.setSku(order.getSku());
orderEntity.setId(order.getId());
batchOrderOperation.insertOrReplace(orderEntity);
}
// Execute the batch of operations on the "people" table.
cloudCustomerTable.execute(batchCustomerOperation);
cloudOrderTable.execute(batchOrderOperation);
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
}
這裏是我的「OrderEntity」
package entities;
import com.microsoft.azure.storage.table.TableServiceEntity;
public class OrderEntity extends TableServiceEntity {
int orderId;
int customerId;
String globaOrderlId;
String sku;
String orderDate;
double price;
int complete;
public OrderEntity(){ }
public OrderEntity(String partitionKey, String globalId){
this.partitionKey = partitionKey;
this.rowKey = globalId;
}
public void setComplete(int complete){
this.complete = complete;
}
public void setCustomerId(int id){
this.customerId = id;
}
public void setGlobalId(String id){
this.globaOrderlId = id;
}
public void setPrice(double price){
this.price = price;
}
public void setOrderDate(String date){
this.orderDate = date;
}
public void setSku(String sku){
this.sku = sku;
}
public void setId(int id){
this.orderId = id;
}
public String getGlobalId(){
return this.globaOrderlId;
}
public int getId(){
return this.orderId;
}
public int getCustomerId(){
return this.customerId;
}
public String getSku(){
return this.sku;
}
public String getOrderDate(){
return this.orderDate;
}
public double getPrice(){
return this.price;
}
public int getComplete(){
return this.complete;
}
}
我已經嘗試註釋掉的客戶代碼,以及所有訂單實體的屬性集,但仍...我只能在我的「batchOrderOperation」一個單一的實體。
如果我有任何更多的,我得到一個錯誤:
com.microsoft.azure.storage.table.TableServiceException: Bad Request at
com.microsoft.azure.storage.table.TableBatchOperation$1.postProcessResponse(TableBatchOperation.java:548)
at com.microsoft.azure.storage.table.TableBatchOperation$1.postProcessResponse(TableBatchOperation.java:434)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:148)
at com.microsoft.azure.storage.table.TableBatchOperation.execute(TableBatchOperation.java:419)
at com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:495)
at com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:452)
at managers.TableManager.batchInsertTransaction(TableManager.java:120)
at managers.QueueManager.process(QueueManager.java:40)
at App.main(App.java:32)
有誰知道問題是什麼?
您應該使用Fiddler攔截操作並驗證實際的請求和響應。這幾乎總是在使用Azure存儲時識別問題的最快方式。 – 2014-12-07 21:06:39