Azure Storage Table DynamicTableEntity实现属性动态写入[Java]

这篇博客介绍了如何在Java中通过DynamicTableEntity类实现对Azure Storage Table的动态属性写入,包括必要的maven依赖和代码示例。
摘要由CSDN通过智能技术生成

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值