阿里雲DataHub RestAPI使用示例

概述

阿里雲流數據處理平臺DataHub是流式數據(Streaming Data)的處理平臺,提供對流式數據的發佈(Publish),訂閱(Subscribe)和分發功能,讓您可以輕鬆構建基於流式數據的分析和應用。目前官方提供了Java和Python的SDK,用戶可以方便的使用SDK完成相關功能的集成開發。目前管理API還沒有提供對應的SDK,本文以通用的org.apache.http.client.HttpClient爲依賴,演示API如何創建Project,其它類型API的使用方法類似。


pom.xml

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4</version>
</dependency>

Code Sample

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;


//創建DataHub Project
public class Demo {

    public static void main(String[] args) {

        String accesskey = "********";
        String accessSecret = "********";
        String projectName = "testproject";//項目的名稱
        String resource = "/projects/" +projectName;

        //獲取系統時間
        Calendar cd = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // 設置時區爲GMT
        String str = sdf.format(cd.getTime());

        //生成Authorization認證信息
        String strToSign = "POST\n" +
                "application/json\n" +
                 str +
                "\n" +
                "x-datahub-client-version:1.1\n" +
                resource;

        byte[] crypto = hmacsha1Signature(strToSign.getBytes(),
                accessSecret.getBytes());

        String signature = Base64.encodeBase64String(crypto).trim();
        String authorization = "DATAHUB " + accesskey + ":" + signature;

        HttpClient httpclient = HttpClients.createDefault();
        try {
            //根據自己創建服務所在的區域設置域名
            URIBuilder builder = new URIBuilder("https://dh-cn-hangzhou.aliyuncs.com" + resource);
            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.addHeader("Authorization", authorization);
            request.addHeader("x-datahub-client-version", "1.1");
            request.addHeader("Date", str);
            request.addHeader("Content-Type","application/json");

            // Request body
            StringEntity reqEntity = new StringEntity(" {\n" +
                    "      \"Action\": \"create\",\n" +
                    "      \"Comment\": \"create project\"\n" +
                    "  }");

            request.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    //創建認證簽名
    public static byte[] hmacsha1Signature(byte[] data, byte[] key) {
        try {
            SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            return mac.doFinal(data);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

注意

  • DATE 表示此次操作的時間,不能爲空,目前只支持GMT格式,如果請求時間和服務器時間相差超過15分鐘,服務器會判定此請求的認證信息過期,所以在請求的過程中注意本地機器與標準時間儘量保持同步;
  • strToSign 參數不同的請求類型會不同,請參考官方API文檔說明,在使用到其它類型API時進行調整。

更多參考

DataHub API參考

請求籤名機制

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章