All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.datahub.model.serialize.MergeShardResultJsonDeser Maven / Gradle / Ivy

There is a newer version: 2.25.6
Show newest version
package com.aliyun.datahub.model.serialize;

import com.aliyun.datahub.DatahubConstants;
import com.aliyun.datahub.common.transport.Response;
import com.aliyun.datahub.common.util.JacksonParser;
import com.aliyun.datahub.exception.DatahubServiceException;
import com.aliyun.datahub.model.MergeShardRequest;
import com.aliyun.datahub.model.MergeShardResult;
import com.aliyun.datahub.model.ShardDesc;
import com.aliyun.datahub.rest.DatahubHttpHeaders;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class MergeShardResultJsonDeser implements Deserializer {

    @Override
    public MergeShardResult deserialize(MergeShardRequest request, Response response) throws DatahubServiceException {
        if (!response.isOK()) {
            throw JsonErrorParser.getInstance().parse(response);
        }

        MergeShardResult rs = new MergeShardResult();
        rs.setRequestId(response.getHeader(DatahubHttpHeaders.HEADER_DATAHUB_REQUEST_ID));

        ObjectMapper mapper = JacksonParser.getObjectMapper();

        JsonNode tree = null;
        try {
            tree = mapper.readTree(response.getBody());
            ShardDesc desc = new ShardDesc();
            desc.setShardId(tree.get(DatahubConstants.ShardId).asText());
            desc.setBeginHashKey(tree.get(DatahubConstants.BeginHashKey).asText());
            desc.setEndHashKey(tree.get(DatahubConstants.EndHashKey).asText());
            rs.setChildShard(desc);
        } catch(IOException e) {
            DatahubServiceException ex = new DatahubServiceException("JsonParseError",
                    "Parse body failed:" + response.getBody(), response);
            throw ex;
        }
        return rs;
    }

    private static MergeShardResultJsonDeser instance;

    private MergeShardResultJsonDeser() {

    }

    public static MergeShardResultJsonDeser getInstance() {
        if (instance == null)
            instance = new MergeShardResultJsonDeser();
        return instance;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy