Data subscription
Subscription operations
The subscription feature saves consumption offsets on the server with high availability.
Create a subscription
CreateSubscriptionResult createSubscription(String projectName, String topicName, String comment);
-
Parameters
-
projectName: the name of the project.
-
topicName: the name of the topic.
-
comment: the description of the subscription.
-
-
Exceptions
-
DatahubClientException
-
InvalidParameterException
-
AuthorizationFailureException
-
ResourceNotFoundException
-
-
Sample code
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);
}
}
Delete a subscription
DeleteSubscriptionResult deleteSubscription(String projectName, String topicName, String subId);
-
Parameters
-
projectName: the name of the project.
-
topicName: the name of the topic.
-
subId: the ID of the subscription.
-
-
Exceptions
-
DatahubClientException
-
InvalidParameterException
-
AuthorizationFailureException
-
ResourceNotFoundException
-
-
Sample code
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);
}
}
Update a subscription
Only the description of a subscription can be updated.
UpdateSubscriptionResult updateSubscription(String projectName, String topicName, String subId, String comment);
-
Parameters
-
projectName: the name of the project.
-
topicName: the name of the topic.
-
subId: the ID of the subscription.
-
comment: the new description for the subscription.
-
-
Exceptions
-
DatahubClientException
-
InvalidParameterException
-
AuthorizationFailureException
-
ResourceNotFoundException
-
-
Sample code
Query subscriptions
The pageNum and pageSize parameters of the listSubscription method specify the range of subscriptions to query. For example, if pageNum is set to 1 and pageSize to 10, the first 10 subscriptions are returned. If pageNum is set to 2 and pageSize to 5, the 6th to 10th subscriptions are returned.
ListSubscriptionResult listSubscription(String projectName, String topicName, int pageNum, int pageSize);
-
Parameters
-
projectName: the name of the project.
-
topicName: the name of the topic.
-
pageNum: the number of the page to return.
-
pageSize: the number of subscriptions to return on each page.
-
-
Exceptions
-
DatahubClientException
-
InvalidParameterException
-
AuthorizationFailureException
-
ResourceNotFoundException
-
-
Sample code
-
Sample code
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);
}
}
Query a subscription
GetSubscriptionResult getSubscription(String projectName, String topicName, String subId);
-
Parameters
-
projectName: the name of the project.
-
topicName: the name of the topic.
-
subId: the ID of the subscription.
-
-
Exceptions
-
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);
}
}
Update the status of a subscription
A subscription can be in the OFFLINE or ONLINE state.
UpdateSubscriptionStateResult updateSubscriptionState(String projectName, String topicName, String subId, SubscriptionState state);
-
Parameters
-
projectName: the name of the project.
-
topicName: the name of the topic.
-
subId: the ID of the subscription.
-
state: the target state of the subscription.
-
-
Exceptions
-
DatahubClientException
-
InvalidParameterException
-
AuthorizationFailureException
-
ResourceNotFoundException
-
-
Sample code
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);
}
}