全部產品
Search
文件中心

Data Lake Formation:DataLake SDK for Java 使用參考

更新時間:Nov 07, 2024

歡迎使用阿里雲開發人員工具套件(Alibaba Cloud SDK for Java),DataLake SDK for Java讓您不用複雜編程即可訪問資料湖構建。本教程介紹如何安裝並開始使用DataLake SDK for Java。

前提條件

  • 建立AccessKey

  • 已安裝Java環境。

    DataLake SDK for Java要求使用JDK 1.7或更高版本。

安裝Alibaba Cloud SDK for Java

您可以在Maven Repository中擷取資料湖構建最新的SDK包,擷取地址Maven SDK地址

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>datalake20200710</artifactId>
  <version>2.0.12</version>
</dependency>

請求步驟

  1. 初始化請求用戶端。調用介面前,需配置環境變數,通過環境變數讀取存取憑證。

            Config authConfig = new Config();
       	// 阿里雲帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常營運,請登入RAM控制台建立RAM使用者。
            // 強烈建議不要將AccessKey ID和AccessKey Secret儲存到代碼裡,會存在密鑰泄漏風險。
    	// 此處以將AccessKey ID和 AccessKey Secret儲存在環境變數為例說明。運行樣本前,請先配置環境變數。
            authConfig.accessKeyId= System.getenv("AK_ENV");
            authConfig.accessKeySecret= System.getenv("SK_ENV");
            authConfig.type= "access_key";
            authConfig.endpoint= "dlf.cn-shanghai.aliyuncs.com";
            authConfig.regionId= "cn-shanghai";
            Client authClient = new Client(authConfig);
  2. 建立請求對象,構造參數。

    下述代碼以CreateDatabase(新增中繼資料庫)為例。

            CreateDatabaseRequest request = new CreateDatabaseRequest();
            request.catalogId = "";
    
            DatabaseInput input = new DatabaseInput();
            input.description = "";
            input.locationUri = "oss://test";
            input.name = "example";
    
            request.databaseInput = input;
  3. 執行調用,擷取返回結果。

            CreateDatabaseResponseBody response = authClient.createDatabase(request).body;

參考樣本

以下範例程式碼以建立一個中繼資料庫為例。

package com.aliyun.datalake.examples;

import com.aliyun.datalake20200710.Client;
import com.aliyun.datalake20200710.models.CreateDatabaseRequest;
import com.aliyun.datalake20200710.models.CreateDatabaseResponseBody;
import com.aliyun.datalake20200710.models.DatabaseInput;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;

public class SchemaExample {

    public static void main(String[] args) throws Exception {
        // 1 Create and initialize a Config instance.
        Config authConfig = new Config();
   	// 阿里雲帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常營運,請登入RAM控制台建立RAM使用者。
        // 強烈建議不要將AccessKey ID和 AccessKey Secret儲存到代碼裡,會存在密鑰泄漏風險。
      	// 此處以將AccessKey ID和 AccessKey Secret儲存在環境變數為例說明。您也可以根據業務需要,儲存到設定檔裡。
        authConfig.accessKeyId= System.getenv("AK_ENV");
        authConfig.accessKeySecret= System.getenv("SK_ENV");
        authConfig.type = "access_key";
        authConfig.endpoint = "dlf.cn-shanghai.aliyuncs.com";
        authConfig.regionId = "cn-shanghai";

        Client authClient = new Client(authConfig);

        // 2 Create an API request and set parameters.
        CreateDatabaseRequest request = new CreateDatabaseRequest();
        request.catalogId = "";

        DatabaseInput input = new DatabaseInput();
        input.description = "";
        input.locationUri = "oss://test";
        input.name = "example";

        request.databaseInput = input;
        // 3 Initiate the request and handle the response or exceptions.
        CreateDatabaseResponseBody response = authClient.createDatabase(request).body;
        System.out.println(new Gson().toJson(response));
    }

}

調用成功時,返回結果response樣本:

{"code":"OK","message":"","requestId":"1739F0B0-A94E-49AC-95FC-C1CE5E4171FA","success":true}

調用出現異常時,SDK會將狀態代碼和錯誤資訊封裝成異常拋出給調用方,樣本:

Exception in thread "main" com.aliyun.tea.TeaException: code: 409, Database example already exists request id: 598B1E2F-9AEF-4B13-AE4D-EB8733B643EB
 at com.aliyun.teaopenapi.Client.doROARequest(Client.java:303)
 at com.aliyun.datalake20200710.Client.createDatabaseWithOptions(Client.java:790)
 at com.aliyun.datalake20200710.Client.createDatabase(Client.java:772)
 at com.aliyun.datalake.examples.SchemaExample.main(SchemaExample.java:34)

調用出現網路等未知異常時,SDK會直接拋出,樣本:

Exception in thread "main" com.aliyun.tea.TeaException
 at com.aliyun.tea.Tea.doAction(Tea.java:67)
 at com.aliyun.teaopenapi.Client.doROARequest(Client.java:292)
 at com.aliyun.datalake20200710.Client.createDatabaseWithOptions(Client.java:790)
 at com.aliyun.datalake20200710.Client.createDatabase(Client.java:772)
 at com.aliyun.datalake.examples.SchemaExample.main(SchemaExample.java:34)
Caused by: java.net.UnknownHostException: dlf.cn-shanghai.aliyuncs.com
 at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
 at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
 at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
 at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
 at java.net.InetAddress.getAllByName(InetAddress.java:1192)
 at java.net.InetAddress.getAllByName(InetAddress.java:1126)
 at okhttp3.Dns$1.lookup(Dns.java:39)
 at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:171)
 at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:137)
 at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:82)
 at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:171)
 at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
 at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
 at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
 at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
 at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
 at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
 at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
 at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
 at okhttp3.RealCall.execute(RealCall.java:69)
 at com.aliyun.tea.Tea.doAction(Tea.java:64)
 ... 4 more

最佳實務

為了便於返回結果的統一處理,擷取到和API文檔一致的異常結果,我們可以通過一些固定的寫法,來處理SDK正常或異常的返回結果。

例如,我們把API調用層用統一的方法來封裝:

public class AbstractAPI {

    protected final Client client;

    public AbstractAPI(Client client) {
        this.client = client;
    }

    public  <M, V extends ResultModel<M>> ResultModel<M> call(Callable<V> c) throws Exception {
        try {
            return c.call();
        } catch (TeaException e) {
            Map<String, Object> data = e.getData();
            if (data!= null && data.get("Code") != null) {
                return TeaModel.toModel(data, new ResultModel<M>());
            } else {
                throw e;
            }
        }
    }
}

對於具體的API,可以繼承上面的類,用適合自己的參數構造調用方法。

public class DatabaseAPI extends AbstractAPI {

    public DatabaseAPI(Client client) {
        super(client);
    }

    public ResultModel<Void> createDatabase(String catalogId, String databaseName, String description,
                                            String locationUri, Map<String, String> parameters,
                                            String ownerName, String ownerType, PrincipalPrivilegeSet privileges) throws Exception {
        return call(()-> {
            CreateDatabaseRequest request = new CreateDatabaseRequest();
            request.catalogId = catalogId;

            DatabaseInput input = new DatabaseInput();
            input.description = description;
            input.locationUri = locationUri;
            input.parameters = parameters;
            input.name = databaseName;
            input.ownerName = ownerName;
            input.ownerType = ownerType;
            input.privileges = privileges;

            request.databaseInput = input;

            CreateDatabaseResponseBody response = client.createDatabase(request).body;
            return new ResultModel<>(response.success, response.code, response.message,
                    response.requestId);
        });
    }
}

這樣,在使用每個API的時候,就可以直接拿到標準的Response了:

public class SchemaExample {

    public static void main(String[] args) throws Exception {
        // 1 Create and initialize a Config instance.
        Config authConfig = new Config();
   	// 阿里雲帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常營運,請登入RAM控制台建立RAM使用者。
        // 強烈建議不要將AccessKey ID和AccessKey Secret儲存到代碼裡,會存在密鑰泄漏風險
      	// 此處以將AccessKey ID和 AccessKey Secret儲存在環境變數為例說明。運行程式碼範例前,請先配置環境變數。
        authConfig.accessKeyId= System.getenv("AK_ENV");
        authConfig.accessKeySecret= System.getenv("SK_ENV");
        authConfig.type = "access_key";
        authConfig.endpoint = "dlf.cn-shanghai.aliyuncs.com";
        authConfig.regionId = "cn-shanghai";

        Client authClient = new Client(authConfig);

        // 2 Initiate the request and handle the response or exceptions.
        ResultModel<Void> response = new DatabaseAPI(authClient).createDatabase("", "example3", "",
                "oss://test", null,
                null, null, null);
        System.out.println(new Gson().toJson(response));
    }

}

建立成功時的response樣本:

{"success":true,"code":"OK","message":"","requestId":"50778D55-696D-45FA-8328-B01983F6CEB1"}

建立出錯時的response樣本:

{"success":false,"code":"AlreadyExists","message":"Database example3 already exists","requestId":"94617169-DA17-4020-9027-7D8F89160682","httpStatusCode":409}

更多資訊

  • DLF目前支援的地區(Region)和網域名稱(Endpoint),請參考已開通的地區和訪問網域名稱

  • 線上調試和產生SDK樣本。OpenAPI Explorer提供線上調用雲產品API、動態產生SDK範例程式碼和快速檢索介面等功能,能顯著降低使用API的難度,推薦您使用。