tencent cloud

Feedback

Data Reporting SDK

Last updated: 2022-05-18 11:50:51

    Overview

    This document uses Java as an example to describe how to integrate the data reporting SDK for Java with the client to quickly report data to DataHub.

    Directions

    Step 1. Create an HTTP access point

    Create an HTTP access point in the DataHub console as instructed in Reporting over HTTP and get the DatahubId identifying the reporting endpoint.

    Step 2. Import the SDK for Java

    Import the data reporting SDK through Maven or Gradle into the Java project.

    Step 3. Report the data

    After importing the SDK, you can call the SendMessage API of the SDK to report a single data entry or batch report data entries as follows:

    1. Instantiate the authentication object.
    2. Instantiate the client object.
    3. Call SendMessage to request to report data.
    4. Process the returned result.
      import com.tencentcloudapi.common.Credential;
      import com.tencentcloudapi.common.profile.ClientProfile;
      import com.tencentcloudapi.common.profile.HttpProfile;
      import com.tencentcloudapi.common.exception.TencentCloudSDKException;
      import com.tencentcloudapi.ckafka.v20190819.CkafkaClient;
      import com.tencentcloudapi.ckafka.v20190819.models.*;

      public class SendMessage
      {
      public static void main(String [] args)
      {
      try{
      // Instantiate an authentication object. Pass in `secretID` and `secretKey` of your Tencent Cloud account as the input parameters and keep them confidential.
      // You can get them at https://console.intl.cloud.tencent.com/cam/capi
      Credential cred = new Credential("SecretId", "SecretKey");
      // (Optional) Instantiate an HTTP option
      HttpProfile httpProfile = new HttpProfile();
      httpProfile.setEndpoint("ckafka.tencentcloudapi.com");
      // (Optional) Instantiate a client option
      ClientProfile clientProfile = new ClientProfile();
      clientProfile.setHttpProfile(httpProfile);
      // Instantiate the client object of the requested product. `clientProfile` is optional
      CkafkaClient client = new CkafkaClient(cred, "ap-beijing", clientProfile);
      // Instantiate a request object. Each API corresponds to a request object
      SendMessageRequest req = new SendMessageRequest();
      req.setDataHubId("datahub-r6gkngy3");

      BatchContent[] batchContents1 = new BatchContent[2];
      BatchContent batchContent1 = new BatchContent();
      batchContent1.setBody("test1");
      batchContents1[0] = batchContent1;

      BatchContent batchContent2 = new BatchContent();
      batchContent2.setBody("test2");
      batchContents1[1] = batchContent2;

      req.setMessage(batchContents1);

      // The returned `resp` is an instance of `SendMessageResponse` which corresponds to the request object
      SendMessageResponse resp = client.SendMessage(req);
      // A string response packet in JSON format is output
      System.out.println(SendMessageResponse.toJsonString(resp));
      } catch (TencentCloudSDKException e) {
      System.out.println(e.toString());
      }
      }
      }

    Step 4. Query the message

    After the data is sent, you can check whether it is sent successfully on the message query page. For more information, see Querying Message.

    Source Code Demo

    import com.tencentcloudapi.common.Credential;
    import com.tencentcloudapi.common.profile.ClientProfile;
    import com.tencentcloudapi.common.profile.HttpProfile;
    import com.tencentcloudapi.common.exception.TencentCloudSDKException;
    import com.tencentcloudapi.ckafka.v20190819.CkafkaClient;
    import com.tencentcloudapi.ckafka.v20190819.models.*;

    public class SendMessage
    {
    public static void main(String [] args)
    {
    try{
    // Instantiate an authentication object. Pass in `secretID` and `secretKey` of your Tencent Cloud account as the input parameters and keep them confidential.
    // You can get them at https://console.intl.cloud.tencent.com/cam/capi
    Credential cred = new Credential("SecretId", "SecretKey");
    // (Optional) Instantiate an HTTP option
    HttpProfile httpProfile = new HttpProfile();
    httpProfile.setEndpoint("ckafka.tencentcloudapi.com");
    // (Optional) Instantiate a client option
    ClientProfile clientProfile = new ClientProfile();
    clientProfile.setHttpProfile(httpProfile);
    // Instantiate the client object of the requested product. `clientProfile` is optional
    CkafkaClient client = new CkafkaClient(cred, "ap-beijing", clientProfile);
    // Instantiate a request object. Each API corresponds to a request object
    SendMessageRequest req = new SendMessageRequest();
    req.setDataHubId("datahub-r6gkngy3");

    BatchContent[] batchContents1 = new BatchContent[2];
    BatchContent batchContent1 = new BatchContent();
    batchContent1.setBody("test1");
    batchContents1[0] = batchContent1;

    BatchContent batchContent2 = new BatchContent();
    batchContent2.setBody("test2");
    batchContents1[1] = batchContent2;

    req.setMessage(batchContents1);

    // The returned `resp` is an instance of `SendMessageResponse` which corresponds to the request object
    SendMessageResponse resp = client.SendMessage(req);
    // A string response packet in JSON format is output
    System.out.println(SendMessageResponse.toJsonString(resp));
    } catch (TencentCloudSDKException e) {
    System.out.println(e.toString());
    }
    }
    }
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support