Updated on 2023-06-21 GMT+08:00

Initializing a DIS SDK Client Instance

You can initialize the DIS SDK client instance using either of the following methods. The first method is recommended. For details about endpoint, ak, sk, region, and projectId, see Obtaining Authentication Information.

  • Use codes to initialize the DIS SDK client instance.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard()
        .withEndpoint("YOUR_ENDPOINT")
        .withAk("YOUR_AK")
        .withSk("YOUR_SK")
        .withProjectId("YOUR_PROJECT_ID")
        .withRegion("YOUR_REGION")
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    
  • Perform the following steps to initialize the DIS client instance if a proxy needs to be used:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard() 
        .withEndpoint("YOUR_ENDPOINT") 
        .withAk("YOUR_AK") 
        .withSk("YOUR_SK") 
        .withProjectId("YOUR_PROJECT_ID") 
        .withRegion("YOUR_REGION") 
    .withProxyHost("YOUR_PROXY_HOST") //Proxy IP address.
    .withProxyPort("YOUR_PROXY_PORT") //Proxy port.
    .withProxyProtocol(Protocol.HTTP) //Proxy protocol. The default value is HTTP.
    .withProxyUsername("YOUR_PROXY_USER_NAME") //Proxy username (optional).
    .withProxyPassword("YOUR_PROXY_PASSWORD") //Proxy password (optional)
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    
  • To enable transmission compression, initialize the DIS client as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard()
        .withEndpoint("YOUR_ENDPOINT")
        .withAk("YOUR_AK")
        .withSk("YOUR_SK")
        .withProjectId("YOUR_PROJECT_ID")
        .withRegion("YOUR_REGION")
        .withBodyCompressEnabled(true)
    .withBodyCompressType(CompressionType.ZSTD) //Configure the compression algorithm. Currently, lz4 and zstd are supported. The default value is lz4.
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    
  • If data needs to be encrypted before being uploaded to DIS on the client, use the encryption method provided by the DIS SDK. That is, add the DataEncryptEnabled and data.password parameters when building the disclient.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // Create a DIS client instance.
    DIS dic = DISClientBuilder.standard()
        .withEndpoint("YOUR_ENDPOINT")
        .withAk("YOUR_AK")
        .withSk("YOUR_SK")
        .withProjectId("YOUR_PROJECT_ID")
        .withRegion("YOUR_REGION")
        .withDataEncryptEnabled(true)
    .withProperty("data.password", "xxx")//xxx indicates the data encryption key configured by the user.
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    

    If JAVA SDK is used to encrypt the uploaded data, you also need to use JAVA SDK to configure the same key for data reading.

  • Use the configuration file to initialize a DIS SDK client instance.

    Add the following configuration items to the dis.properties file in the dis-sdk-demo\src\main\resources directory:

    • ak/sk: AK/SK created on the IAM
    • region: region of the stream
    • endpoint: access address of the DIS
    • projectId: project ID of the stream
    1
    2
    //Create a DIS SDK client instance.
                DIS dic = DISClientBuilder.standard().build();