本章节为您介绍如何将群体发布成API供业务调用。

前提条件

新建实体/关系并绑定RDS/ADS云计算资源上的物理表生成标签。

新建实体/关系并绑定MaxCompute云计算资源上的物理表生成标签。

背景信息

生成的群体信息发布API供第三方调用。

  1. 登录DataQuotient 画像分析控制台
  2. 在导航栏群体画像 > 群体 > 群体列表,单击新建群体
  3. 页面跳转至标签群圈,选择实体,在标签设置里面设置实体、实体的关系和实体关系关联的实体筛选的条件。选择完标签的设置后,可以在圈定结果查看圈定结果数量和筛选。
  4. 单击设置展示标签出现设置群体展示标签勾选展示标签,设置好后单击确定
  5. 设置好群体展示标签后,单击预览,可以查看圈群结果。
  6. 预览群体后,单击发布群体 > 发布API,填写API名称、path、分组和描述(选填),单击确定
  7. 发布后可以在发布API页面单击API列表查看发布的API。单击API的名称可以查看API的基本信息、调用例子、参数信息、调用例子、调用参数、返回结果和示例代码。
    说明 API调用示例代码现在支持node.js和java格式。
  8. Java格式调用代码。
    
    import java.io.BufferedReader;
    
    import java.io.IOException;
    
    import java.io.InputStreamReader;
    
    import java.io.PrintWriter;
    
    import java.net.HttpURLConnection;
    
    import java.net.URL;
    
    import java.security.MessageDigest;
    
    import java.text.SimpleDateFormat;
    
    import java.util.Date;
    
    import java.util.Locale;
    
    import javax.crypto.Mac;
    
    import javax.crypto.spec.SecretKeySpec;
    
    import sun.misc.BASE64Encoder;
    
     
    
    public
    class Main {
    
      public
    static String
    MD5Base64(String s) {
    
            if (s == null)
    
                return null;
    
            String encodeStr = "";
    
            byte[] utfBytes = s.getBytes();
    
            MessageDigest mdTemp;
    
            try {
    
                mdTemp = MessageDigest.getInstance("MD5");
    
     mdTemp.update(utfBytes);
    
                byte[] md5Bytes = mdTemp.digest();
    
                BASE64Encoder b64Encoder = new BASE64Encoder();
    
                encodeStr =
    b64Encoder.encode(md5Bytes);
    
            } catch (Exception e) {
    
                throw new Error("Failed to generate MD5 : " + e.getMessage());
    
            }
    
            return encodeStr;
    
        }
    
        public static String
    HMACSha1(String data, String key) {
    
            String result;
    
            try {
    
                SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
    
                        "HmacSHA1");
    
     Mac mac = Mac.getInstance("HmacSHA1");
    
                mac.init(signingKey);
    
                byte[] rawHmac = mac.doFinal(data.getBytes());
    
                result = (new BASE64Encoder()).encode(rawHmac);
    
            } catch (Exception e) {
    
                throw new Error("Failed to generate HMAC : " + e.getMessage());
    
            }
    
            return result;
    
        }
    
        public static String
    toGMTString(Date date) {
    
            SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z",
    
                    Locale.UK);
    
            df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
    
            return df.format(date);
    
        }
    
        public static String
    sendPost(String url, String body, String ak_id, String ak_secret) {
    
            return sendPostOrPut(url, "POST",
    body, ak_id, ak_secret);
    
        }
    
        public static String
    sendPut(String url, String body, String ak_id, String ak_secret) {
    
            return sendPostOrPut(url, "PUT",
    body, ak_id, ak_secret);
    
        }
    
        private static String
    sendPostOrPut(String url, String method, String body, String ak_id, String ak_secret) {
    
            PrintWriter out = null;
    
            BufferedReader in = null;
    
            String result = "";
    
            try {
    
                URL realUrl = new URL(url);
    
             String accept = "json";
    
                String content_type = "application/json";
    
           String path = realUrl.getFile();
    
                String date = toGMTString(new
    Date());
    
               String bodyMd5 = MD5Base64(body);
    
                String stringToSign = method + "\n"
    + accept + "\n" + bodyMd5 + "\n" +
    
                        content_type + "\n" + date +
    
                    "\n" + path;
    
                String signature = HMACSha1(stringToSign, ak_secret);
    
                String authHeader = "dtboost-proxy "
    + ak_id + ":" + signature;
    
                HttpURLConnection conn =
    (HttpURLConnection) realUrl.openConnection();
    
                conn.setRequestMethod(method);
    
                conn.setRequestProperty("X-Access-TenantCode", "dt_ng_1819452387394251");
    
                conn.setRequestProperty("accept", accept);
    
                conn.setRequestProperty("content-type", content_type);
    
                conn.setRequestProperty("date", date);
    
                System.out.println(authHeader);
    
                conn.setRequestProperty("Authorization", authHeader);
    
           conn.setRequestProperty("Content-MD5", bodyMd5);
    
                conn.setDoOutput(true);
    
                conn.setDoInput(true);
    
                out = new PrintWriter(conn.getOutputStream());
    
                out.print(body);
    
                out.flush();
    
                System.out.println(conn.getResponseCode());
    
                if (conn.getResponseCode() != 200)
    {
    
                    in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
    
                } else {
    
             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
                }
    
                String line;
    
                while ((line = in .readLine()) != null) {
    
         result += line;
    
                }
    
            } catch (Exception e) {
    
                System.out.println(" " + method + " " + e);
    
         e.printStackTrace();
    
            }
    
            finally {
    
                try {
    
                    if (out != null) {
    
                        out.close();
    
                    }
    
                    if ( in != null) { in .close();
    
                    }
    
                } catch (IOException ex) {
    
           ex.printStackTrace();
    
                }
    
            }
    
            return result;
    
        }
    
        public static String
    sendGet(String url, String ak_id, String ak_secret) {
    
            String result = "";
    
            BufferedReader in = null;
    
            try {
    
                URL realUrl = new URL(url);
    
                String method = "GET";
    
                String accept = "json";
    
                String content_type = "application/json";
    
                String path = realUrl.getFile();
    
       String
    date = toGMTString(new
    Date());
    
                String stringToSign = method + "\n"
    +accept + "\n" + "" + "\n" +
    
                        content_type + "\n" + date +
    
            "\n" + path;
    
                String signature = HMACSha1(stringToSign, ak_secret);
    
            String authHeader = "dtboost-proxy "
    + ak_id + ":" + signature;
    
                HttpURLConnection connection =
    (HttpURLConnection)
    
                        realUrl.openConnection();
    
            connection.setRequestProperty("X-Access-TenantCode", "dt_ng_1819452387394251");
    
                connection.setRequestProperty("accept", accept);
    
                connection.setRequestProperty("content-type", content_type);
    
             connection.setRequestProperty("date", date);
    
                connection.setRequestProperty("Authorization", authHeader);
    
                connection.connect();
    
                if (connection.getResponseCode() != 200) {
    
                    in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
    
                } else {
    
                    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    
                }
    
                String line;
    
                while ((line = in .readLine()) != null) {
    
                    result += line;
    
          }
    
            } catch (Exception e) {
    
                System.out.println(e);
    
                e.printStackTrace();
    
            }
    
            finally {
    
                try {
    
                    if ( in !=null) { in .close();
    
     
    
                    }
    
                } catch (Exception e) {
    
      e.printStackTrace();
    
                }
    
            }
    
            return result;
    
        }
    
        public static void
    main(String[] args) throws Exception {
    
            String ak_id = "";// 请到右上角账号- 用户配置 中获取你拿的ak,请注意防止泄露
    
            String ak_secret = "";// 请到右上角账号- 用户配置 中获取你拿的ak,请注意防止泄露
    
            String url = "https://dataq-poc.aliyun.com/analysis/api/template/getcarbasicinfo?workspaceCode=demoworkspace";
    
            String body = "{\"params\":{}}";
    
          System.out.println("response body:" + sendPostOrPut(url, "POST", body,
    
    ak_id, ak_secret));
    
        }
    
    }                       
  9. node.js调用代码:
    
    
    const request = require('request');
    
    const
    url = require('url');
    
    const
    crypto = require('crypto');
    
    // 请到账号右上角- 用户配置 中获取你拿的ak,请注意防止泄露
    
    const
    ak_id = '';
    
    const
    ak_secret = '';
    
    const
    options = {
    
      url:
    'https://dataq-poc.aliyun.com/analysis/api/template/getcarbasicinfo?workspaceCode=demoworkspace',
    
      method:
    'POST',
    
      body:
    JSON.stringify({
    
        params:{
    
     
    
        }
    
      }),
    
      headers:
    {
    
        'accept':
    'json',
    
        'content-type':
    'application/json',
    
        'date':
    new Date().toUTCString(),
    
        'Authorization':
    '',
    
        'x-access-tenantCode': 'dt_ng_1819452387394251'}
    
    };
    
    const
    md5 = function (buffer) {
    
      return
    crypto.createHash('md5').update(buffer).digest('base64');
    
    };
    
    const
    sha1 = function (stringToSign, secret) {
    
      return
    crypto.createHmac('sha1',
    secret).update(stringToSign).digest().toString('base64');
    
    };
    
    let
    body = options.body || '';
    
    let
    bodymd5;
    
    if
    (options.method === 'GET' || body === void 0 || body === '') {
    
      bodymd5 = body;
    
    } else {
    
      bodymd5 = md5(Buffer.from(body));
    
    }
    
    let
    stringToSign = options.method + "\n"
    + options.headers.accept + "\n"
    + bodymd5 +
    
      "\n"
    + options.headers['content-type'] + "\n" + options.headers.date + "\n"
    +
    
      url.parse(options.url).path;
    
    let
    signature = sha1(stringToSign, ak_secret);
    
    let
    authHeader = "dtoobst-proxy " + ak_id + ":" + signature;
    
    options.headers.Authorization
    = authHeader;
    
     
    
    function callback(error,
    response, body) {
    
      if
    (error) {
    
        console.error("error", error)
    
      }
    
      console.log("result:
    ", response.statusCode, body)
    
    }
    
    request(options, callback);