【问题标题】:Can't insert more than 1 entity (batch insert) with Azure - JAVA无法使用 Azure 插入超过 1 个实体(批量插入) - JAVA
【发布时间】:2014-12-07 20:46:05
【问题描述】:

我正在尝试使用 Azure 插入一批实体。 对于我的“CustomerEntity”,一切都按预期工作,但对于我的“OrderEntity”,我的批处理操作中只能有一个实体......

这是我的代码:

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();
    }

}

这是我的“订单实体”

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 存储时发现问题的最快方法。

标签: java azure


【解决方案1】:

有趣的是,我如何花费数小时寻找解决方案,一旦我求助于帮助,我就会找到答案......

事实证明,我的 rowKeys 是相同的,并且 rowKeys 对于任何给定的分区都必须是唯一的:

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

行键是给定实体内的唯一标识符 分区

希望有一天这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 2020-02-02
    • 2011-06-11
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多