2014-12-07 84 views
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) 

有谁知道问题是什么?

+0

您应该使用Fiddler拦截操作并验证实际的请求和响应。这几乎总是在使用Azure存储时识别问题的最快方式。 – 2014-12-07 21:06:39

回答

1

滑稽我如何度过寻求的解决方案小时,只要我求助于求助,我找到了答案......

事实证明,我的rowKeys是相同的,并且rowKeys必须是独一无二的任何给定的分区:

http://msdn.microsoft.com/en-us/library/dd179338.aspx

The row key is a unique identifier for an entity within a given partition

希望这有助于别人一天。