Cette rubrique explique comment installer et utiliser le SDK Java Realtime Compute for Apache Flink.
Remarques d'utilisation
Du 19 septembre au 27 octobre 2022, Alibaba Cloud a déployé une mise à jour du SDK pour Realtime Compute for Apache Flink dans toutes les régions. Cette nouvelle version est désormais la version par défaut.
Pour en savoir plus sur l'impact de cette mise à jour du SDK, consultez la section Avis de service.
Ce document décrit la nouvelle version du SDK. Pour consulter la documentation de la version précédente, cliquez sur OpenAPI SDK (Obsolète).
Prérequis
-
Une AccessKey a été créée. Pour plus d'informations, consultez la page Créer une AccessKey.
RemarquePour atténuer les risques de sécurité en cas de divulgation de l'AccessKey de votre compte Alibaba Cloud, nous vous recommandons de créer un utilisateur RAM, de lui accorder les autorisations liées à Flink, puis d'utiliser son AccessKey pour appeler le SDK. Pour plus d'informations, consultez les rubriques suivantes :
Pour créer un utilisateur RAM et sa clé d'accès, consultez la page Créer un utilisateur RAM ou Créer une AccessKey.
Pour accorder des autorisations à un utilisateur RAM, consultez la page Autorisation dans la console Alibaba Cloud Management Console.
Java 8 ou une version ultérieure est installé.
Votre compte dispose des autorisations requises. Pour plus d'informations, consultez la page Gestion des autorisations.
SDK Java Realtime Compute for Apache Flink
Selling console
|
Méthode d'installation |
Code |
|
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' |
Development console
|
Méthode d'installation |
Code |
|
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' |
Déboguer en ligne et générer des exemples de SDK
Utilisez OpenAPI Explorer pour appeler des opérations API en ligne, générer dynamiquement du code d'exemple SDK et rechercher rapidement des opérations API. Vous pouvez afficher et télécharger du code d'exemple SDK pour les opérations API sur les pages Realtime Compute for Apache Flink et Realtime Compute Selling Console. Pour plus d'informations, consultez la section Démarrage rapide.
Dans l'onglet SDK Example, sélectionnez une langue, telle que Java, cliquez sur Run Example pour exécuter le code en ligne, ou cliquez sur Download Complete Project pour télécharger l'exemple de code.
Exemples
Pour connaître les points de terminaison de la console de vente Realtime Compute for Apache Flink, consultez la section Points de terminaison.
Pour connaître les points de terminaison de la console de développement Realtime Compute for Apache Flink, consultez la section Points de terminaison.
Espaces de travail achetés
Cet exemple montre comment récupérer les détails des espaces de travail Realtime Compute for Apache Flink achetés dans une région spécifiée.
Region: ID de la région . Pour plus d'informations, consultez la page Points de terminaison. Par exemple, 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>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.foasconsole20211028.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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);
// Query the region ID of an instance.
System.out.println(response.getBody().getInstances().get(0).zoneId);
// Query the ID of the resource group to which an instance belongs.
System.out.println(response.getBody().getInstances().get(0).resourceGroupId);
System.out.println(JSON.toJSON(response));
} catch (TeaException error) {
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Créer un déploiement
Déploiement SQL
Cet exemple montre comment créer un déploiement SQL.
workspace: ID de l'espace de travail. Vous pouvez obtenir cet ID à partir du champResourceIdrenvoyé lorsque vous consultez les espaces de travail achetés. Par exemple,adf9e514****.namespace: Nom du namespace. Par exemple, test-default.body.name: Nom du job. Par exemple, mysql_data_holo_test.body.engineVersion: Version du moteur. Par exemple, vvr-8.0.7-flink-1.17. Vous pouvez obtenir les versions de moteur prises en charge en répertoriant les versions de moteur prises en charge.body.sqlArtifact.sqlScript: Contenu du script SQL. Exemple :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: Type du job. Par exemple, SQLSCRIPT.body.deploymentTarget.mode: Mode de déploiement. Seul le mode PER_JOB est pris en charge.body.deploymentTarget.name: Nom de la file d'attente de déploiement. Par exemple, default-queue.body.executionMode: mode d'exécution. Par exemple, STREAMING (mode streaming).body.streamingResourceSetting.resourceSettingMode: Mode de ressource pour le mode streaming. Par exemple, BASIC.body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.cpu: Nombre de cœurs CPU pour le JobManager. Par exemple, 2.body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.memory: Mémoire du JobManager. Par exemple, 4,0 Go.body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.cpu: Nombre de cœurs CPU du TaskManager. Par exemple, 2.body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.memory: Mémoire du TaskManager. Par exemple, 4,0 Go.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.teaopenapi.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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 parameters</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()
// The name of the API operation.
.setAction("CreateDeployment")
// The version of the API operation.
.setVersion("2022-07-18")
// The protocol of the API operation.
.setProtocol("HTTPS")
// The HTTP method of the API operation.
.setMethod("POST")
.setAuthType("AK")
.setStyle("ROA")
// The request path of the API operation.
.setPathname("/api/v2/namespaces/" + namespace + "/deployments")
// The format of the request body.
.setReqBodyType("json")
// The format of the response body.
.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);
// The method returns a Map. You can obtain the response body, response headers, and HTTP status code from the map.
client.callApi(params, request, runtime);
java.util.Map<String, ?> response = client.callApi(params, request, runtime);
System.out.println(response);
}
}
Déploiement JAR
Cet exemple montre comment créer un déploiement JAR.
Téléchargez le package JAR dans un compartiment OSS et assurez-vous que votre espace de travail Realtime Compute for Apache Flink dispose des autorisations nécessaires pour y accéder. Pour plus d'informations, consultez la page Téléchargement simple.
Une fois le fichier téléchargé, l'URL de téléchargement se présente sous la forme https://<Bucket>.oss-<Region>.aliyuncs.com/<FileName>.
workspace: ID de l'espace de travail. Vous pouvez obtenir cet ID à partir du champResourceIdrenvoyé lorsque vous consultez les espaces de travail achetés. Par exemple, adf9e514****.namespace: Nom du namespace. Par exemple, test-default.body.name: Nom du job. Par exemple, my-test-jar.body.engineVersion: Version du moteur. Par exemple, vvr-8.0.7-flink-1.17. Vous pouvez obtenir la liste des versions de moteur prises en charge.body.jarArtifact.kind: Type de l'artefact du job. Par exemple, JAR.body.jarArtifact.jarUri: Chemin URL complet du déploiement JAR. Par exemple, https://myBucket/oss-cn-hangzhou/test.jar.body.jarArtifact.entryClass: Classe de point d'entrée. Vous devez spécifier le nom de classe complet. Par exemple, org.apache.flink.test.body.deploymentTarget.mode: Mode de déploiement. Seul le mode PER_JOB est pris en charge.body.deploymentTarget.name: Nom de la file d'attente de déploiement. Par exemple, default-queue.body.executionMode: Mode d'exécution. Par exemple, STREAMING (mode streaming).body.streamingResourceSetting.resourceSettingMode: Mode de ressource pour le mode streaming. Par exemple, BASIC.body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.cpu: Nombre de cœurs CPU pour le JobManager. Par exemple, 2.body.streamingResourceSetting.basicResourceSetting.jobmanagerResourceSettingSpec.memory: Mémoire du JobManager. Par exemple, 4,0 Go.body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.cpu: Nombre de cœurs CPU pour le TM. Par exemple, 2.body.streamingResourceSetting.basicResourceSetting.taskmanagerResourceSettingSpec.memory: Mémoire du TaskManager. Par exemple, 4,0 Go.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* <b>description</b> :
* <p>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.teaopenapi.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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 parameters</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()
// The name of the API operation.
.setAction("CreateDeployment")
// The version of the API operation.
.setVersion("2022-07-18")
// The protocol of the API operation.
.setProtocol("HTTPS")
// The HTTP method of the API operation.
.setMethod("POST")
.setAuthType("AK")
.setStyle("ROA")
// The request path of the API operation.
.setPathname("/api/v2/namespaces/" + namespace + "/deployments")
// The format of the request body.
.setReqBodyType("json")
// The format of the response body.
.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);
// The method returns a Map. You can obtain the response body, response headers, and HTTP status code from the map.
java.util.Map<String, ?> response = client.callApi(params, request, runtime);
System.out.println(response);
}
}
Répertorier les déploiements
Cet exemple montre comment répertorier tous les déploiements dans un namespace.
workspace: ID de l'espace de travail. Vous pouvez obtenir l'ID à partir du champResourceIdrenvoyé par l'opération dans la section Consulter les espaces de travail achetés. Par exemple, adf9e514****.namespace: Nom du namespace. Par exemple, 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>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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) {
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Démarrer un job
Cet exemple montre comment démarrer un job à partir d'un déploiement dans un namespace.
workspace: ID de l'espace de travail. Par exemple, adf9e5147a****.namespace: Nom du namespace, par exemple test-default.deploymentId: ID du déploiement. Vous pouvez obtenir cet ID en appelant l'opération Répertorier les déploiements. Par exemple, 10283a02-c6a6-4f3e-9f93-8dab****.kind: Type de stratégie de restauration. Les valeurs prises en charge sont NONE (démarrage sans état), LATEST_SAVEPOINT (démarrage à partir du dernier savepoint), FROM_SAVEPOINT (démarrage à partir d'un savepoint spécifié) et LATEST_STATE (démarrage à partir du dernier état).
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>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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) {
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Répertorier les jobs
Cet exemple montre comment interroger tous les jobs pour un déploiement spécifique.
workspace: ID de l'espace de travail. Par exemple, adf9e5147****.namespace: Nom du namespace, par exemple test-default.deploymentId: ID du déploiement. Pour obtenir cet ID, consultez la section Obtenir la liste des déploiements. Par exemple, 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>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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);
// View the execution result of the job.
System.out.println("Execution result is: "+response.body.success);
// Obtain the job ID. This ID is used to cancel the job.
System.out.println(response.body.getData().get(0).jobId);
System.out.println(JSON.toJSON(response));
} catch (TeaException error) {
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Arrêter un job
Cet exemple montre comment arrêter un job.
workspace: ID de l'espace de travail. Par exemple, adf9e5147****.namespace: Nom du namespace, par exemple test-default.jobId: ID du job. Pour plus d'informations, consultez la section Obtenir des informations sur un déploiement spécifique. Par exemple, 3171d4d1-**--**-e762493b7765.stopStrategy: Stratégie d'arrêt du job. Les valeurs prises en charge sont NONE (arrête directement le job), STOP_WITH_SAVEPOINT (arrête le job après création d'un savepoint) et STOP_WITH_DRAIN (arrête le job en mode vidage).
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>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.ververica20220718.Client createClient() throws Exception {
// Hard-coding the AccessKey pair in your code may cause the AccessKey pair to be leaked and threaten the security of all the resources in your account. The following sample code is for reference only.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
.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) {
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In this example, the error message is printed for reference only. We recommend that you handle exceptions with caution in your project and do not ignore them.
// Error message
System.out.println(error.getMessage());
// Diagnostic address
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Références
Pour plus d'informations sur le SDK Python, consultez la section Référence du SDK Python.