Subscription operations

Updated at:

Use the DataHub Java SDK to create, delete, update, list, and retrieve subscriptions.

Create a subscription

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

String

The name of the topic.

comment

String

The subscription comment.

The comment format is:

{"application":"application","description":"description"}

Exceptions

Exception class

Error code

Description

DatahubClientException

-

The base class for all exceptions.

InvalidParameterException

InvalidParameter

InvalidCursor

The parameters are invalid.

AuthorizationFailureException

Unauthorized

Authorization signature parsing failed. Verify that your AccessKey is correct.

ResourceNotFoundException

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist.

Note

This exception may be thrown if you send other requests immediately after a Split/Merge operation.

Sample code

public static void createSubscription(String projectName,String topicName) {
  try {
      CreateSubscriptionResult createSubscriptionResult = datahubClient.createSubscription(projectName, topicName, Constant.subscribtionComment);
      System.out.println("create subscription successful");
      System.out.println(createSubscriptionResult.getSubId());
  } catch (DatahubClientException e) {
        System.out.println(e.getErrorMessage());      
  }
}

Delete a subscription

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

String

The name of the topic.

subId

String

The subscription ID.

Exceptions

Exception class

Error code

Description

DatahubClientException

-

The base class for all exceptions.

InvalidParameterException

InvalidParameter

InvalidCursor

The parameters are invalid.

AuthorizationFailureException

Unauthorized

Authorization signature parsing failed. Verify that your AccessKey is correct.

ResourceNotFoundException

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist.

Note

This exception may be thrown if you send other requests immediately after a Split/Merge operation.

Sample code

public static void deleteSubscription(String projectName,String topicName,String subId) {
  try {
      datahubClient.deleteSubscription(projectName, topicName, subId);
      System.out.println("delete subscription successful");
  } catch (DatahubClientException e) {
        System.out.println(e.getErrorMessage());      
  }
}

Update a subscription

Update an existing subscription. Only the comment field can be updated.

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

String

The name of the topic.

subId

String

The subscription ID.

comment

String

The new comment value.

Exceptions

Exception class

Error code

Description

DatahubClientException

-

The base class for all exceptions.

InvalidParameterException

InvalidParameter

InvalidCursor

The parameters are invalid.

AuthorizationFailureException

Unauthorized

Authorization signature parsing failed. Verify that your AccessKey is correct.

ResourceNotFoundException

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist.

Note

This exception may be thrown if you send other requests immediately after a Split/Merge operation.

Sample code

public static void updateSubscription(String projectName, String topicName, String subId, String comment){
        try {
            datahubClient.updateSubscription(projectName,topicName,subId,comment)
        } catch (DatahubClientException e) {
            System.out.println(e.getErrorMessage());

        }

    }

List subscriptions

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

String

The name of the topic.

pageNum

int

The page number for pagination.

pageSize

int

The number of subscriptions per page.

Note

The pageNum and pageSize parameters of listSubscription are used to retrieve subscription information within a specified range. For example:

  • pageNum = 1, pageSize = 10: Retrieves subscriptions 1-10.

  • pageNum = 2, pageSize = 5: Retrieves subscriptions 6-10.

Exceptions

Exception class

Error code

Description

DatahubClientException

-

The base class for all exceptions.

InvalidParameterException

InvalidParameter

InvalidCursor

The parameters are invalid.

AuthorizationFailureException

Unauthorized

Authorization signature parsing failed. Verify that your AccessKey is correct.

ResourceNotFoundException

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist.

Note

This exception may be thrown if you send other requests immediately after a Split/Merge operation.

Sample code

public static void listSubscription(String projectName, String topicName, int pageNum, int pageSize) {
    try {
        ListSubscriptionResult listSubscriptionResult = datahubClient.listSubscription(projectName, topicName, pageNum, 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 (DatahubClientException e) {
        System.out.println(e.getErrorMessage());

    }
}

Get a subscription

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

String

The name of the topic.

subId

String

The subscription ID.

Exceptions

Exception class

Error code

Description

DatahubClientException

-

The base class for all exceptions.

InvalidParameterException

InvalidParameter

InvalidCursor

The parameters are invalid.

AuthorizationFailureException

Unauthorized

Authorization signature parsing failed. Verify that your AccessKey is correct.

ResourceNotFoundException

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist.

Note

This exception may be thrown if you send other requests immediately after a Split/Merge operation.

Sample code

public static void getSubscription(String projectName, String topicName, String subId) {
    try {
        GetSubscriptionResult getSubscriptionResult = datahubClient.getSubscription(projectName, topicName, subId);
        System.out.println(getSubscriptionResult.getSubId() + "\t"
                + getSubscriptionResult.getState() + "\t"
                + getSubscriptionResult.getType() + "\t"
                + getSubscriptionResult.getComment());
    }  catch (DatahubClientException e) {
        System.out.println(e.getErrorMessage());

    }
}

Update subscription status

A subscription has two states: OFFLINE and ONLINE.

Parameters

Parameter

Type

Description

projectName

String

The name of the project.

topicName

String

The name of the topic.

subId

String

The subscription ID.

state

SubscriptionState

The target state.

Exceptions

Exception class

Error code

Description

DatahubClientException

-

The base class for all exceptions.

InvalidParameterException

InvalidParameter

InvalidCursor

The parameters are invalid.

AuthorizationFailureException

Unauthorized

Authorization signature parsing failed. Verify that your AccessKey is correct.

ResourceNotFoundException

ResourceNotFound

NoSuchProject

NoSuchTopic

NoSuchShard

NoSuchSubscription

NoSuchConnector

NoSuchMeteringInfo

The specified resource does not exist.

Note

This exception may be thrown if you send other requests immediately after a Split/Merge operation.

Sample code

public static void updateSubscriptionState(String projectName, String topicName,String subId) {
    try {
        datahubClient.updateSubscriptionState(projectName, topicName, subId, SubscriptionState.ONLINE);
        System.out.println("update subscription state successful");
    } catch (DatahubClientException e) {
        System.out.println(e.getErrorMessage());        
    }
}