maven依赖
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.0.0</version>
</dependency>
代码示例
package com.gridsum.service;
import com.alibaba.fastjson.JSONObject;
import com.gridsum.util.DateUtil;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.table.*;
import org.junit.jupiter.api.Test;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Zhiqiang Lin
* @Description
* @create 2019/9/20.
*/
public class TableServiceTest {
private static final String COMMON_PREFIX = "c_";
private static final String SPECIAL_PREFIX = "s_";
private static final String DYNAMIC_PREFIX = "d_";
private CloudTable userTable;
private String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=platformsearchdevelop;AccountKey=U2nro8i2C40G9Dvwe3N0BFKC+txX3Wo/k1fcr0xzzqkpzztbhw/cE1P9gnKYoaKaW2gbv2Su5Vu1McgSwIva6w==;EndpointSuffix=core.chinacloudapi.cn";
private HashMap<String, Class> schemaMap = new HashMap<>();
@Test
public void test() {
try {
init();
insertUserTest();
getUserTest("000", "001");
} catch (StorageException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
private void init() throws URISyntaxException, InvalidKeyException, StorageException {
schemaMap.put("c_string", String.class);
schemaMap.put("c_stringArray", List.class);
schemaMap.put("c_int", Integer.class);
schemaMap.put("c_date", Date.class);
schemaMap.put("c_intArray", List.class);
CloudStorageAccount account = CloudStorageAccount.parse(this.storageConnectionString);
CloudTableClient tableClient = account.createCloudTableClient();
userTable = tableClient.getTableReference("userTable");
}
private void insertUserTest() throws StorageException {
DynamicTableEntity userEntity = new DynamicTableEntity("000", "001");
HashMap<String, EntityProperty> values = new HashMap<>();
values.put("c_string", new EntityProperty("myString"));
values.put("c_stringArray", new EntityProperty("{\"v\":[\"myString1\",\"myString2\"]}"));
values.put("c_int", new EntityProperty(1));
values.put("c_date", new EntityProperty(DateUtil.format(new Date())));
values.put("c_intArray", new EntityProperty("{\"v\":[1,2,3]}"));
values.put("s_string", new EntityProperty("{\"v\":\"myString\"}"));
values.put("s_stringArray", new EntityProperty("{\"v\":[\"myString1\",\"myString2\"]}"));
values.put("s_int", new EntityProperty("{\"v\":1}"));
values.put("s_date", new EntityProperty("{\"v\":\"" + DateUtil.format(new Date()) + "\"}"));
values.put("s_intArray", new EntityProperty("{\"v\":[1,2,3]}"));
userEntity.setProperties(values);
TableOperation userEntityInsert = TableOperation.insertOrMerge(userEntity);
userTable.execute(userEntityInsert);
}
private void getUserTest(String partitionKey, String rowKey) throws StorageException {
TableOperation userEntityInsert = TableOperation.retrieve(partitionKey, rowKey, DynamicTableEntity.class);
DynamicTableEntity userEntity = userTable.execute(userEntityInsert).getResultAsType();
JSONObject result = parseEntityToJSONObject(userEntity);
System.out.println(result);
}
private JSONObject parseEntityToJSONObject(DynamicTableEntity entity) {
JSONObject result = new JSONObject();
JSONObject commonObject = new JSONObject();
JSONObject specialObject = new JSONObject();
JSONObject dynamicObject = new JSONObject();
HashMap<String, EntityProperty> values = entity.getProperties();
for (Map.Entry<String, EntityProperty> entry : values.entrySet()) {
String key = entry.getKey();
if (key.startsWith(SPECIAL_PREFIX)) {
String value = entry.getValue().getValueAsString();
specialObject.put(key, JSONObject.parseObject(value).get("v"));
} else if (key.startsWith(DYNAMIC_PREFIX)) {
String value = entry.getValue().getValueAsString();
dynamicObject.put(key, JSONObject.parseObject(value).get("v"));
} else if (key.startsWith(COMMON_PREFIX)) {
Class clazz = schemaMap.get(key);
if (clazz == Integer.class) {
commonObject.put(key, entry.getValue().getValueAsInteger());
} else if (clazz == Date.class) {
commonObject.put(key, entry.getValue().getValueAsString());
} else if (clazz == List.class) {
String value = entry.getValue().getValueAsString();
dynamicObject.put(key, JSONObject.parseObject(value).get("v"));
} else {
commonObject.put(key, entry.getValue().getValueAsString());
}
}
}
result.put("common", commonObject);
result.put("special", specialObject);
result.put("dynamic", dynamicObject);
return result;
}
}