在 Java 中将实体添加到 Azure 的 table

Adding an entity to a table at Azure in Java

我正在尝试将一个实体添加到 Azure 的 table,但我遇到了很多错误。这是我的代码:

package table;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.table.*;
import com.microsoft.azure.storage.table.TableQuery.*;

public class tableTutorial {

 public static final String storageConnectionString=
"DefaultEndpointsProtocol=https;"+
"AccountName=my_storage_name;"+
"AccountKey=my_storage_account_key;"+
"EndpointSuffix=core.windows.net";

public static void main (String args[]) {
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();
       }

    }

}



class CustomerEntity extends TableServiceEntity {
public CustomerEntity(String lastName, String firstName) {
    this.partitionKey = lastName;
    this.rowKey = firstName;
   }

 public CustomerEntity() { 

   String email;
   String phoneNumber;
 }
 public String getEmail() {
    return this.email;
  }

 public void setEmail(String email) {
    this.email = email;
  }

  public String getPhoneNumber() {
    return this.phoneNumber;
  }

  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }
}

我遇到的错误是:

com.microsoft.azure.storage.StorageException: 试图在序列化期间访问实体的不可访问成员。 在 com.microsoft.azure.storage.table.TableServiceEntity.writeEntity(TableServiceEntity.java:468) 在 com.microsoft.azure.storage.table.TableEntitySerializer.getPropertiesFromDictionary(TableEntitySerializer.java:213) 在 com.microsoft.azure.storage.table.TableEntitySerializer.writeJsonEntity(TableEntitySerializer.java:129) 在 com.microsoft.azure.storage.table.TableEntitySerializer.writeSingleEntityToStream(TableEntitySerializer.java:63) 在 com.microsoft.azure.storage.table.TableOperation.insertImpl(TableOperation.java:381) 在 com.microsoft.azure.storage.table.TableOperation.performInsert(TableOperation.java:362) 在 com.microsoft.azure.storage.table.TableOperation.execute(TableOperation.java:682) 在 com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:529) 在 com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:496) 在 table.tableTutorial.main(tableTutorial.java:86) Caused by: java.lang.IllegalAccessException: class com.microsoft.azure.storage.table.PropertyPair 无法使用修饰符 "public" 访问 class table.CustomerEntity 的成员 在 java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(来源不明) 在 java.base/java.lang.reflect.AccessibleObject.checkAccess(来源不明) 在 java.base/java.lang.reflect.Method.invoke(来源不明) 在 com.microsoft.azure.storage.table.PropertyPair.generateEntityProperty(PropertyPair.java:291) 在 com.microsoft.azure.storage.table.TableServiceEntity.writeEntityWithReflection(TableServiceEntity.java:211) 在 com.microsoft.azure.storage.table.TableServiceEntity.writeEntity(TableServiceEntity.java:465)

Caused by: java.lang.IllegalAccessException: class com.microsoft.azure.storage.table.PropertyPair cannot access a member of class table.CustomerEntity with modifiers "public"

要序列化您的实体,您的 CustomerEntity 应该是 public,因此它应该在单独的文件中定义。另请注意不要在构造方法中声明属性。

这是代码供您参考。

package table;
import com.microsoft.azure.storage.table.TableServiceEntity;

public class CustomerEntity extends TableServiceEntity {

public CustomerEntity(String lastName, String firstName) {
    this.partitionKey = lastName;
    this.rowKey = firstName;
}

public CustomerEntity() { }

String email;
String phoneNumber;

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhoneNumber() {
    return this.phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
}