gpt4 book ai didi

java - 无法使用 Azure - JAVA 插入超过 1 个实体(批量插入)

转载 作者:行者123 更新时间:2023-12-01 12:09:22 26 4
gpt4 key购买 nike

我正在尝试使用 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();
}

}

这是我的“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)

有人知道问题出在哪里吗?

最佳答案

有趣的是,我花了几个小时寻找解决方案,一旦我寻求帮助,我就找到了答案......

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

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

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

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

关于java - 无法使用 Azure - JAVA 插入超过 1 个实体(批量插入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27347620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com