Data subscription
Subscription operations
The subscription feature allows you to save consumption offsets on the server. You can use simple operations and settings to save consumption offsets 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
You can update existing subscriptions. Only the descriptions of subscriptions 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 description that you want to update.
Exceptions
DatahubClientException
InvalidParameterException
AuthorizationFailureException
ResourceNotFoundException
Sample code
Query subscriptions
The pageNum and pageSize parameters of the listSubscription method specify a range for the subscriptions to be queried. For example, if the pageNum parameter is set to 1 and the pageSize parameter to 10, the first ten subscriptions are queried. If the pageNum parameter is set to 2 and the pageSize parameter to 5, the sixth to tenth subscriptions are queried.
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 is in the OFFLINE state or the 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 state that you want to update.
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);
}
}