Updated on 2024-03-06 GMT+08:00

Initializing the DLI Client

To use DLI SDK to access DLI, you need to initialize the DLI client. During DLI client initialization, you can perform authentication using the Access Key ID/Secret Access Key (AK/SK) or token. The sample code is as follows:

Prerequisites

  • You have configured the Java SDK environment by following the instructions provided Overview.
  • You have initialized the DLI Client by following the instructions provided in Initializing the DLI Client.

Sample Code for AK/SK Authentication

  • Sample code
    1
    2
    3
    4
    5
    6
      String ak = System.getenv("xxx_SDK_AK");//Access key ID
      String sk = System.getenv("xxx_SDK_SK");//Key used together with the access key ID
      String regionName = "regionname";
      String projectId = "project_id";
      DLIInfo dliInfo = new DLIInfo(regionName, ak, sk, projectId);
      DLIClient client = new DLIClient(AuthenticationMode.AKSK, dliInfo);
    
  • Parameter description and acquisition method
    • Parameter description
      • ak: Account access key
      • sk: Account secret access key

        Hard coding AKs and SKs or storing them in code in plaintext poses significant security risks. You are advised to store them in encrypted form in configuration files or environment variables and decrypt them when needed to ensure security.

        In this example, the AK and SK stored in the environment variables are used. Specify the environment variables xxx_SDK_AK and xxx_SDK_SK in the local environment first.

      • regionName: region name
      • projectId: project ID
    • You can perform the following operations to obtain the Access Keys, project ID, and Region:
      1. Log in to the management console.
      2. Hover the cursor on the username in the upper right corner and select My Credentials from the drop-down list.
      3. In the navigation pane on the left, choose Access Keys and click Create Access Key. Confirm that you want to proceed with the operation and click OK.
      4. On the displayed page, click Download. Open the credential file to obtain the AK/SK information.
      5. In the navigation pane on the left, choose API Credentials. In the Projects pane, locate project_id and obtain the region information.

Sample code for token-based authentication

  • Sample code
    1
    2
    3
    4
    5
    6
    7
      String domainName = "domainname";
      String userName = "username";
      String password = "password";
      String regionName = "regionname";
      String projectId = "project_id";
      DLIInfo dliInfo = new DLIInfo(regionName, domainName, userName, password, projectId);
      DLIClient client = new DLIClient(AuthenticationMode.TOKEN, dliInfo);
    
  • Parameter description
    • domainname: Name of the account to which the IAM user belongs
    • username: Username
    • password: User password
    • regionname: Region name
    • project_id: Project ID
    • Hard coding passwords or storing them in code in plaintext poses significant security risks. You are advised to store them in encrypted form in configuration files or environment variables and decrypt them when needed to ensure security.
    • You can change the endpoint in set mode. Run the following statement: dliInfo.setServerEndpoint(endpoint).