全部产品
Search
文档中心

DataHub:订阅数据

更新时间:Aug 02, 2021

订阅数据

Subscribtion操作

订阅服务提供了服务端保存用户消费点位的功能,只需要通过简单配置和处理,就可以实现高可用的点位存储服务。

创建 Subscription

说明

CreateSubscriptionResult createSubscription(String projectName, String topicName, String comment);

  • 参数
    • projectName The name of the project.
    • topicName The name of the topic.
    • comment The comment of the subscription.
  • Exception
    • DatahubClientException
    • InvalidParameterException
    • AuthorizationFailureException
    • ResourceNotFoundException
  • 示例
public static void createSubscription() {
  try {
      CreateSubscriptionResult createSubscriptionResult = datahubClient.createSubscription(Constant.projectName, Constant.topicName, Constant.subscribtionComment);
      System.out.println("create subscription successful");
      System.out.println(createSubscriptionResult.getSubId());
  } catch (InvalidParameterException e) {
      System.out.println("invalid parameter, please check your parameter");
      System.exit(1);
  } catch (AuthorizationFailureException e) {
      System.out.println("AK error, please check your accessId and accessKey");
      System.exit(1);
  } catch (ResourceNotFoundException e) {
      System.out.println(" project or topic not found,please create project first");
      System.exit(1);
  } catch (DatahubClientException e) {
      System.out.println("other error");
      System.out.println(e);
      System.exit(1);
  }
}

删除 Subscription

说明

DeleteSubscriptionResult deleteSubscription(String projectName, String topicName, String subId);

  • 参数
    • projectName The name of the project.
    • topicName The name of the topic.
    • subId The id of the subscription.
  • Exception
    • DatahubClientException
    • InvalidParameterException
    • AuthorizationFailureException
    • ResourceNotFoundException
  • 示例
public static void deleteSubscription() {
  try {
      datahubClient.deleteSubscription(Constant.projectName, Constant.topicName, subId);
      System.out.println("delete subscription successful");
  } catch (InvalidParameterException e) {
      System.out.println("invalid parameter, please check your parameter");
      System.exit(1);
  } catch (AuthorizationFailureException e) {
      System.out.println("AK error, please check your accessId and accessKey");
      System.exit(1);
  } catch (ResourceNotFoundException e) {
      System.out.println(" project or topic or subId not found,delete successful");
  } catch (DatahubClientException e) {
      System.out.println("other error");
      System.out.println(e);
      System.exit(1);
  }
}

更新 Subscription

更新已存在的Subscription,目前只支持更新comment。

说明

UpdateSubscriptionResult updateSubscription(String projectName, String topicName, String subId, String comment);

  • 参数
    • projectName The name of the project.
    • topicName The name of the topic.
    • subId The id of the subscription.
    • comment The comment you want to update.
  • Exception
    • DatahubClientException
    • InvalidParameterException
    • AuthorizationFailureException
    • ResourceNotFoundException
  • 示例

列出 Subscription

listSubscription的参数pageNum和pageSize取指定范围的subscription信息,如pageNum =1, pageSize =10,获取1-10个subscription; pageNum =2, pageSize =5则获取6-10的subscription。

说明

ListSubscriptionResult listSubscription(String projectName, String topicName, int pageNum, int pageSize);

  • 参数
    • projectName The name of the project.
    • topicName The name of the topic.
    • pageNum The page number used to list subscriptions.
    • pageSize The page size used to list subscriptions.
  • Exception
    • DatahubClientException
    • InvalidParameterException
    • AuthorizationFailureException
    • ResourceNotFoundException
  • 示例代码
  • 示例代码
public static void listSubscription() {
    try {
        ListSubscriptionResult listSubscriptionResult = datahubClient.listSubscription(Constant.projectName, Constant.topicName, Constant.pageNum, Constant.pageSize);
        if (listSubscriptionResult.getSubscriptions().size() > 0) {
            System.out.println(listSubscriptionResult.getTotalCount());
            System.out.println(listSubscriptionResult.getSubscriptions().size());
            for (SubscriptionEntry entry : listSubscriptionResult.getSubscriptions()) {
                System.out.println(entry.getSubId() + "\t"
                        + entry.getState() + "\t"
                        + entry.getType() + "\t"
                        + entry.getComment());
            }
        }
    } catch (InvalidParameterException e) {
        System.out.println("invalid parameter, please check your parameter");
        System.exit(1);
    } catch (AuthorizationFailureException e) {
        System.out.println("AK error, please check your accessId and accessKey");
        System.exit(1);
    } catch (ResourceNotFoundException e) {
        System.out.println(" project or topic not found,delete successful");
        System.exit(1);
    } catch (DatahubClientException e) {
        System.out.println("other error");
        System.out.println(e);
        System.exit(1);
    }
}

查询 Subscription

说明

GetSubscriptionResult getSubscription(String projectName, String topicName, String subId);

  • 参数
    • projectName The name of the project.
    • topicName The name of the topic.
    • subId The id of the subscription.
  • Exception
    • DatahubClientException
    • InvalidParameterException
    • AuthorizationFailureException
    • ResourceNotFoundException
public static void getSubscription() {
    try {
        GetSubscriptionResult getSubscriptionResult = datahubClient.getSubscription(Constant.projectName, Constant.topicName, subId);
        System.out.println(getSubscriptionResult.getSubId() + "\t"
                + getSubscriptionResult.getState() + "\t"
                + getSubscriptionResult.getType() + "\t"
                + getSubscriptionResult.getComment());
    } catch (InvalidParameterException e) {
        System.out.println("invalid parameter, please check your parameter");
        System.exit(1);
    } catch (AuthorizationFailureException e) {
        System.out.println("AK error, please check your accessId and accessKey");
        System.exit(1);
    } catch (ResourceNotFoundException e) {
        System.out.println(" project or topic or subscription not found,delete successful");
        System.exit(1);
    } catch (DatahubClientException e) {
        System.out.println("other error");
        System.out.println(e);
        System.exit(1);
    }
}

更新 Subscription 状态

Subscription有两种状态,OFFLINE和ONLINE,分别表示离线和在线。

说明

UpdateSubscriptionStateResult updateSubscriptionState(String projectName, String topicName, String subId, SubscriptionState state);

  • 参数
    • projectName The name of the project.
    • topicName The name of the topic.
    • subId The id of the subscription.
    • state The state you want to change.
  • Exception
    • DatahubClientException
    • InvalidParameterException
    • AuthorizationFailureException
    • ResourceNotFoundException
  • 示例
public static void updateSubscriptionState() {
    try {
        datahubClient.updateSubscriptionState(Constant.projectName, Constant.topicName, subId, SubscriptionState.ONLINE);
        System.out.println("update subscription state successful");
    } catch (InvalidParameterException e) {
        System.out.println("invalid parameter, please check your parameter");
        System.exit(1);
    } catch (AuthorizationFailureException e) {
        System.out.println("AK error, please check your accessId and accessKey");
        System.exit(1);
    } catch (ResourceNotFoundException e) {
        System.out.println(" project or topic not found,delete successful");
        System.exit(1);
    } catch (DatahubClientException e) {
        System.out.println("other error");
        System.out.println(e);
        System.exit(1);
    }
}