概述
阿里云流数据处理平台 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 参考
请求签名机制
来源: https://yq.aliyun.com/articles/684740