本文為您介紹如何安裝並使用Realtime ComputeFlink版Java SDK。
注意事項
2022年9月19日,我們對Realtime ComputeFlink版SDK進行了升級,後續將預設使用新版本的SDK為您服務。該版本在全網更新發布的時間為2022年9月19日至2022年10月27日。
關於SDK升級的影響,請參見產品公告。
本文檔為新版SDK使用文檔,如果您還需查看舊版SDK使用文檔,請單擊OpenAPI SDK(已停止維護)下載後進行查看。
前提條件
已建立AccessKey,詳情請參見建立AccessKey。
說明為避免主帳號泄露AccessKey帶來安全風險,建議您建立RAM使用者,授予RAM使用者Flink相關的存取權限,再使用RAM使用者的AccessKey調用SDK。相關文檔請參見:
建立RAM使用者操以及對應AccessKey,請參見建立RAM使用者或建立AccessKey。
為RAM使用者授權,請參見管理主控台授權。
已準備Java環境,要求Java 8及以上版本。
帳號具有相關訪問及操作許可權,詳情請參見許可權管理。
Realtime ComputeFlink版 Java SDK
Realtime Compute售賣控制台Java SDK
安裝方式 | 代碼 |
Apache Maven | <dependency> <groupId>com.aliyun</groupId> <artifactId>foasconsole20211028</artifactId> <version>2.1.0</version> </dependency> |
Gradle Groovy DSL | implementation 'com.aliyun:foasconsole20211028:2.1.0' |
Gradle Kotlin DSL | implementation("com.aliyun:foasconsole20211028:2.1.0") |
Scala SBT | libraryDependencies += "com.aliyun" % "foasconsole20211028" % "2.1.0" |
Apache Ivy | <dependency org="com.aliyun" name="foasconsole20211028" rev="2.1.0" /> |
Groovy Grape | @Grapes( @Grab(group='com.aliyun', module='foasconsole20211028', version='2.1.0') ) |
Leiningen | [com.aliyun/foasconsole20211028 "2.1.0"] |
Apache Buildr | 'com.aliyun:foasconsole20211028:jar:2.1.0' |
Realtime Compute開發控制台Java SDK
安裝方式 | 代碼 |
Apache Maven | <dependency> <groupId>com.aliyun</groupId> <artifactId>ververica20220718</artifactId> <version>1.7.0</version> </dependency> |
Gradle Groovy DSL | implementation 'com.aliyun:ververica20220718:1.7.0' |
Gradle Kotlin DSL | implementation("com.aliyun:ververica20220718:1.7.0") |
Scala SBT | libraryDependencies += "com.aliyun" % "ververica20220718" % "1.7.0" |
Apache Ivy | <dependency org="com.aliyun" name="ververica20220718" rev="1.7.0" /> |
Groovy Grape | @Grapes( @Grab(group='com.aliyun', module='ververica20220718', version='1.7.0') ) |
Leiningen | [com.aliyun/ververica20220718 "1.7.0"] |
Apache Buildr | 'com.aliyun:ververica20220718:jar:1.7.0' |
線上調試和產生SDK樣本
OpenAPI門戶提供了線上調用產品API、動態產生SDK範例程式碼和快速檢索介面等功能,可以顯著降低使用API的難度。您可以在Realtime Compute開發控制台API和Realtime Compute售賣控制台API頁面查看所需API的SDK樣本,並下載使用,具體操作步驟請參見快速開始。

參考樣本
查看已經購買的工作空間
查詢目標地區下已購買的Flink工作空間的詳細資料。必填請求參數如下。
Region:地區ID,詳情請參見服務存取點。例如cn-hangzhou。
package com.aliyun.sample;
import com.aliyun.foasconsole20211028.models.DescribeInstancesResponse;
import com.aliyun.tea.*;
import com.alibaba.fastjson2.JSON;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.foasconsole20211028.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "foasconsole.aliyuncs.com";
return new com.aliyun.foasconsole20211028.Client(config);
}
public static void main(String[] args_) throws Exception {
com.aliyun.foasconsole20211028.Client client = Sample.createClient();
com.aliyun.foasconsole20211028.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.foasconsole20211028.models.DescribeInstancesRequest()
.setRegion("cn-beijing");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
DescribeInstancesResponse response = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
System.out.println(response.statusCode);
// 查看某一個執行個體的地區id
System.out.println(response.getBody().getInstances().get(0).zoneId);
// 查看某一個執行個體所屬的資源群組id
System.out.println(response.getBody().getInstances().get(0).resourceGroupId);
System.out.println(JSON.toJSON(response));
} catch (TeaException error) {
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
建立已部署作業
SQL作業
建立一個已部署SQL作業,必填請求參數如下。
workspace:工作空間ID,可通過查看已經購買的工作空間返回的ResourceId擷取。例如adf9e514****。namespace:專案空間名稱。例如test-default。body.name:作業名稱。例如mysql_data_holo_test。body.engineVersion:引擎版本。例如vvr-8.0.7-flink-1.17。可以通過擷取系統支援的引擎版本列表擷取支援的引擎版本。body.sqlArtifact.sqlScript:SQL作業常值內容。例如CREATE TEMPORARY TABLE datagen_source( name VARCHAR ) WITH ( 'connector' = 'datagen' ); CREATE TEMPORARY TABLE blackhole_sink( name VARCHAR ) with ( 'connector' = 'blackhole' ); INSERT INTO blackhole_sink SELECT name from datagen_source;。body.sqlArtifact.kind:生效作業類型。例如SQLSCRIPT。body.deploymentTarget.mode:部署模式。僅支援選擇PER_JOB模式。body.deploymentTarget.name:部署隊列名稱。例如default-queue。body.executionMode:執行模式。例如STREAMING(流模式)。body.streamingResourceSetting.resourceSettingMode:流模式使用的資源模式。例如BASIC。body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.cpu:JM CPU核心數。例如2。body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.memory:JM記憶體。例如4.0 GiB。body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.cpu:TM CPU核心數。例如2。body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.memory:TM記憶體。例如4.0 GiB。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.teaopenapi.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "ververica.cn-beijing.aliyuncs.com";
return new com.aliyun.teaopenapi.Client(config);
}
/**
* <b>description</b> :
* <p>API 相關</p>
*
* @param path params
* @return OpenApi.Params
*/
public static com.aliyun.teaopenapi.models.Params createApiInfo(String namespace) throws Exception {
com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
// 介面名稱
.setAction("CreateDeployment")
// 介面版本
.setVersion("2022-07-18")
// 介面協議
.setProtocol("HTTPS")
// 介面 HTTP 方法
.setMethod("POST")
.setAuthType("AK")
.setStyle("ROA")
// 介面 PATH
.setPathname("/api/v2/namespaces/" + namespace + "/deployments")
// 介面請求體內容格式
.setReqBodyType("json")
// 介面響應體內容格式
.setBodyType("json");
return params;
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.teaopenapi.Client client = Sample.createClient();
com.aliyun.teaopenapi.models.Params params = Sample.createApiInfo("test-default");
// body params
java.util.Map<String, Object> body = TeaConverter.buildMap(
new TeaPair("name", "mysql_data_holo_test"),
new TeaPair("engineVersion", "vvr-8.0.7-flink-1.17"),
new TeaPair("artifact", TeaConverter.buildMap(
new TeaPair("sqlArtifact", TeaConverter.buildMap(
new TeaPair("sqlScript", "CREATE TEMPORARY TABLE datagen_source( name VARCHAR ) WITH ( 'connector' = 'datagen' ); CREATE TEMPORARY TABLE blackhole_sink( name VARCHAR ) with ( 'connector' = 'blackhole' ); INSERT INTO blackhole_sink SELECT name from datagen_source;")
)),
new TeaPair("kind", "SQLSCRIPT")
)),
new TeaPair("deploymentTarget", TeaConverter.buildMap(
new TeaPair("mode", "PER_JOB"),
new TeaPair("name", "default-queue")
)),
new TeaPair("executionMode", "STREAMING"),
new TeaPair("streamingResourceSetting", TeaConverter.buildMap(
new TeaPair("resourceSettingMode", "BASIC"),
new TeaPair("basicResourceSetting", TeaConverter.buildMap(
new TeaPair("jobmanagerResourceSettingSpec", TeaConverter.buildMap(
new TeaPair("cpu", 2),
new TeaPair("memory", "4")
)),
new TeaPair("taskmanagerResourceSettingSpec", TeaConverter.buildMap(
new TeaPair("cpu", 2),
new TeaPair("memory", "4")
))
))
))
);
// header params
java.util.Map<String, String> headers = new java.util.HashMap<>();
headers.put("workspace", "ab2*******884d");
// runtime options
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest()
.setHeaders(headers)
.setBody(body);
// 傳回值為 Map 類型,可從 Map 中獲得三類資料:響應體 body、回應標頭 headers、HTTP 返回的狀態代碼 statusCode。
client.callApi(params, request, runtime);
java.util.Map<String, ?> response = client.callApi(params, request, runtime);
System.out.println(response);
}
}JAR作業
建立並部署一個JAR作業,必填請求參數如下。
請確保已將JAR包上傳到OSS Bucket且Flink工作空間具有存取權限。具體操作可以參考簡單上傳OSS。
上傳後URL的下載連結為https://<Bucket>.oss-<Region>.aliyuncs.com/<FileName>。
workspace:工作空間ID,可通過查看已經購買的工作空間返回的ResourceId擷取。例如adf9e514****。namespace:專案空間名稱。例如test-default。body.name:作業名稱。例如my-test-jar。body.engineVersion:引擎版本。例如vvr-8.0.7-flink-1.17。可以通過擷取系統支援的引擎版本列表擷取支援的引擎版本。body.jarArtifact.kind:生效作業類型。例如JAR。body.jarArtifact.jarUri:JAR作業URL全路徑。例如https://myBucket/oss-cn-hangzhou/test.jar。body.jarArtifact.entryClass:啟動類,需要填寫類的全稱。例如org.apache.flink.test。body.deploymentTarget.mode:部署模式。僅支援選擇PER_JOB模式。body.deploymentTarget.name:部署隊列名稱。例如default-queue。body.executionMode:執行模式。例如STREAMING(流模式)。body.streamingResourceSetting.resourceSettingMode:流模式使用的資源模式。例如BASIC。body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.cpu:JM CPU核心數。例如2。body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.memory:JM記憶體。例如4.0 GiB。body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.cpu:TM CPU核心數。例如2。body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.memory:TM記憶體。例如4.0 GiB。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.teaopenapi.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "ververica.cn-hangzhou.aliyuncs.com";
return new com.aliyun.teaopenapi.Client(config);
}
/**
* <b>description</b> :
* <p>API 相關</p>
*
* @param path params
* @return OpenApi.Params
*/
public static com.aliyun.teaopenapi.models.Params createApiInfo(String namespace) throws Exception {
com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
// 介面名稱
.setAction("CreateDeployment")
// 介面版本
.setVersion("2022-07-18")
// 介面協議
.setProtocol("HTTPS")
// 介面 HTTP 方法
.setMethod("POST")
.setAuthType("AK")
.setStyle("ROA")
// 介面 PATH
.setPathname("/api/v2/namespaces/" + namespace + "/deployments")
// 介面請求體內容格式
.setReqBodyType("json")
// 介面響應體內容格式
.setBodyType("json");
return params;
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.teaopenapi.Client client = Sample.createClient();
com.aliyun.teaopenapi.models.Params params = Sample.createApiInfo("flink-default");
// body params
java.util.Map<String, Object> body = TeaConverter.buildMap(
new TeaPair("name", "my-test-jar"),
new TeaPair("engineVersion", "vvr-8.0.7-flink-1.17"),
new TeaPair("artifact", TeaConverter.buildMap(
new TeaPair("kind", "JAR"),
new TeaPair("jarArtifact", TeaConverter.buildMap(
new TeaPair("jarUri", "https://flink-test.oss-cn-hangzhou.aliyuncs.com/flinkDemo.jar?*****"),
new TeaPair("entryClass", "com.aliyun.FlinkDemo")
))
)),
new TeaPair("deploymentTarget", TeaConverter.buildMap(
new TeaPair("mode", "PER_JOB"),
new TeaPair("name", "default-queue")
)),
new TeaPair("executionMode", "STREAMING"),
new TeaPair("streamingResourceSetting", TeaConverter.buildMap(
new TeaPair("resourceSettingMode", "BASIC"),
new TeaPair("basicResourceSetting", TeaConverter.buildMap(
new TeaPair("jobmanagerResourceSettingSpec", TeaConverter.buildMap(
new TeaPair("cpu", 2),
new TeaPair("memory", "4")
)),
new TeaPair("taskmanagerResourceSettingSpec", TeaConverter.buildMap(
new TeaPair("cpu", 2),
new TeaPair("memory", "4")
))
))
))
);
// header params
java.util.Map<String, String> headers = new java.util.HashMap<>();
headers.put("workspace", "d05a*****e44");
// runtime options
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest()
.setHeaders(headers)
.setBody(body);
// 傳回值為 Map 類型,可從 Map 中獲得三類資料:響應體 body、回應標頭 headers、HTTP 返回的狀態代碼 statusCode。
java.util.Map<String, ?> response = client.callApi(params, request, runtime);
System.out.println(response);
}
}擷取已部署作業列表
擷取專案空間下所有已部署作業的資訊。必填請求參數如下。
workspace:工作空間ID,可通過查看已經購買的工作空間返回的ResourceId擷取。例如adf9e514****。namespace:專案空間名稱,例如test-default。
package com.sample;
import com.aliyun.tea.*;
import com.alibaba.fastjson2.JSON;
import com.aliyun.ververica20220718.models.ListDeploymentsResponse;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "ververica.cn-hangzhou.aliyuncs.com";
return new com.aliyun.ververica20220718.Client(config);
}
public static void main(String[] args_) throws Exception {
com.aliyun.ververica20220718.Client client = Sample.createClient();
com.aliyun.ververica20220718.models.ListDeploymentsHeaders listDeploymentsHeaders = new com.aliyun.ververica20220718.models.ListDeploymentsHeaders()
.setWorkspace("ab2a******884d");
com.aliyun.ververica20220718.models.ListDeploymentsRequest listDeploymentsRequest = new com.aliyun.ververica20220718.models.ListDeploymentsRequest();
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
ListDeploymentsResponse response=client.listDeploymentsWithOptions("test-default", listDeploymentsRequest, listDeploymentsHeaders, runtime);
System.out.println(response.body.data.get(0).name);
System.out.println(response.body.data.get(0).deploymentId);
System.out.println(JSON.toJSON(response));
} catch (TeaException error) {
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}啟動作業
啟動專案空間下一個已部署的作業。必填請求參數如下。
workspace:工作空間ID,例如adf9e5147a****。namespace:專案空間名稱,例如test-default。deploymentId:作業部署ID,可通過擷取已部署作業列表擷取。例如10283a02-c6a6-4f3e-9f93-8dab****。kind:啟動位點類型。支援NONE(無狀態啟動)、LATEST_SAVEPOINT(最新的作業快照啟動)、FROM_SAVEPOINT(從指定快照啟動)、LATEST_STATE(最新狀態啟動)。
package com.aliyun.sample;
import com.aliyun.tea.*;
import com.aliyun.ververica20220718.models.StartJobWithParamsResponse;
import com.alibaba.fastjson2.JSON;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "ververica.cn-hangzhou.aliyuncs.com";
return new com.aliyun.ververica20220718.Client(config);
}
public static void main(String[] args_) throws Exception {
com.aliyun.ververica20220718.Client client = Sample.createClient();
com.aliyun.ververica20220718.models.StartJobWithParamsHeaders startJobWithParamsHeaders = new com.aliyun.ververica20220718.models.StartJobWithParamsHeaders()
.setWorkspace("ab2a******884d");
com.aliyun.ververica20220718.models.DeploymentRestoreStrategy jobStartParametersDeploymentRestoreStrategy = new com.aliyun.ververica20220718.models.DeploymentRestoreStrategy()
.setKind("NONE");
com.aliyun.ververica20220718.models.JobStartParameters jobStartParameters = new com.aliyun.ververica20220718.models.JobStartParameters()
.setRestoreStrategy(jobStartParametersDeploymentRestoreStrategy)
.setDeploymentId("10283a02-****-****-****-8dabf617d52f");
com.aliyun.ververica20220718.models.StartJobWithParamsRequest startJobWithParamsRequest = new com.aliyun.ververica20220718.models.StartJobWithParamsRequest()
.setBody(jobStartParameters);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
StartJobWithParamsResponse response = client.startJobWithParamsWithOptions("test-default", startJobWithParamsRequest, startJobWithParamsHeaders, runtime);
System.out.println(JSON.toJSON(response.body));
} catch (TeaException error) {
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}擷取某個已部署作業的資訊
擷取某個已部署作業下所有工作執行個體的資訊。必填請求參數如下。
workspace:工作空間ID,例如adf9e5147****。namespace:專案空間名稱,例如test-default。deploymentId:作業部署ID,可以通過擷取已部署作業列表擷取。例如8489b7ec-****-****-****-cc4c17fa12b0。
package com.aliyun.sample;
import com.aliyun.tea.*;
import com.aliyun.ververica20220718.models.ListJobsResponse;
import com.alibaba.fastjson2.JSON;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "ververica.cn-beijing.aliyuncs.com";
return new com.aliyun.ververica20220718.Client(config);
}
public static void main(String[] args_) throws Exception {
com.aliyun.ververica20220718.Client client = Sample.createClient();
com.aliyun.ververica20220718.models.ListJobsHeaders listJobsHeaders = new com.aliyun.ververica20220718.models.ListJobsHeaders()
.setWorkspace("ab2a******884d");
com.aliyun.ververica20220718.models.ListJobsRequest listJobsRequest = new com.aliyun.ververica20220718.models.ListJobsRequest()
.setDeploymentId("8489b7ec-****-****-****-cc4c17fa12b0");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
ListJobsResponse response = client.listJobsWithOptions("test-default", listJobsRequest, listJobsHeaders, runtime);
// 查看作業執行結果
System.out.println("Execution result is:"+response.body.success);
// 擷取jobId,該參數用於停止作業
System.out.println(response.body.getData().get(0).jobId);
System.out.println(JSON.toJSON(response));
} catch (TeaException error) {
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}停止執行個體作業
停止一個工作執行個體。必填請求參數如下。
workspace:工作空間ID,例如adf9e5147****。namespace:專案空間名稱,例如test-default。jobId:工作執行個體ID,您可以通過擷取某個已部署作業的資訊擷取。例如3171d4d1-****-****-****-e762493b7765。stopStrategy:作業停止策略。支援NONE(直接停止)、STOP_WITH_SAVEPOINT(產生作業快照後停止)、STOP_WITH_DRAIN(以drain的方式停止)。
package com.aliyun.sample;
import com.alibaba.fastjson2.JSON;
import com.aliyun.tea.*;
import com.aliyun.ververica20220718.models.StopJobResponse;
public class Sample {
/**
* <b>description</b> :
* <p>使用AK&SK初始化帳號Client</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "ververica.cn-hangzhou.aliyuncs.com";
return new com.aliyun.ververica20220718.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.ververica20220718.Client client = Sample.createClient();
com.aliyun.ververica20220718.models.StopJobHeaders stopJobHeaders = new com.aliyun.ververica20220718.models.StopJobHeaders()
.setWorkspace("ab2a******884d");
com.aliyun.ververica20220718.models.StopJobRequestBody stopJobRequestBody = new com.aliyun.ververica20220718.models.StopJobRequestBody()
.setStopStrategy("NONE");
com.aliyun.ververica20220718.models.StopJobRequest stopJobRequest = new com.aliyun.ververica20220718.models.StopJobRequest()
.setBody(stopJobRequestBody);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
StopJobResponse response = client.stopJobWithOptions("test-default", "7970e881-****-****-****-1a3746710878", stopJobRequest, stopJobHeaders, runtime);
System.out.println(JSON.toJSON(response.getBody().getData()));
} catch (TeaException error) {
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
// 錯誤 message
System.out.println(error.getMessage());
// 診斷地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}相關文檔
Python SDK參考請參見Python SDK參考。