Azure Table 存储在单个操作中创建或更新
Azure Table Storage Create or Update in single operation
是否可以通过一次操作在 Azure Table 存储中创建或更新实体,还是我必须执行多个步骤?
首先,READ
查看实体是否已经存在,如果不存在,我将执行第二个操作,即 REPLACE
?
我想应该是InsertOrReplace操作。
根据您的需要,我建议您使用azure table storage SDK
中的insertOrReplace
方法。
我不知道你目前使用的是什么语言。我只是提供 Java SDK
作为参考。
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount =
CloudStorageAccount.parse(storageConnectionString);
// Create the table client.
CloudTableClient tableClient = storageAccount.createCloudTableClient();
// Create a cloud table object for the table.
CloudTable cloudTable = tableClient.getTableReference("people");
// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.setEmail("Walter@contoso.com");
customer1.setPhoneNumber("425-555-0101");
// Create an operation to add the new customer to the people table.
TableOperation insertCustomer1 = TableOperation.insertOrReplace(customer1);
// Submit the operation to the table service.
cloudTable.execute(insertCustomer1);
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
TableOperation insertCustomer1 =
TableOperation.insertOrReplace(customer1);
您也可以参考official doc。
希望对你有帮助。
是否可以通过一次操作在 Azure Table 存储中创建或更新实体,还是我必须执行多个步骤?
首先,READ
查看实体是否已经存在,如果不存在,我将执行第二个操作,即 REPLACE
?
我想应该是InsertOrReplace操作。
根据您的需要,我建议您使用azure table storage SDK
中的insertOrReplace
方法。
我不知道你目前使用的是什么语言。我只是提供 Java SDK
作为参考。
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount =
CloudStorageAccount.parse(storageConnectionString);
// Create the table client.
CloudTableClient tableClient = storageAccount.createCloudTableClient();
// Create a cloud table object for the table.
CloudTable cloudTable = tableClient.getTableReference("people");
// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.setEmail("Walter@contoso.com");
customer1.setPhoneNumber("425-555-0101");
// Create an operation to add the new customer to the people table.
TableOperation insertCustomer1 = TableOperation.insertOrReplace(customer1);
// Submit the operation to the table service.
cloudTable.execute(insertCustomer1);
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
TableOperation insertCustomer1 = TableOperation.insertOrReplace(customer1);
您也可以参考official doc。
希望对你有帮助。