创建并执行作业示例(Java版本)
0.版本说明
本示例基于华为云SDK V3.0.78版本开发
1.功能介绍
在MRS集群中新增并提交一个作业。
需要先在集群详情页的“概览”页签,单击“IAM用户同步”右侧的“同步”进行IAM用户同步,然后再通过该接口提交作业。
如需使用OBS加密功能,请先参考“MRS用户指南 > 管理现有集群 > 作业管理 > 使用OBS加密数据运行作业”页面进行相关配置后,再调用API接口运行作业。
所有示例中涉及的OBS路径、样例文件及终端节点和AKSK,请提前准备并在提交请求时根据实际情况替换。
2.前置条件
1.已注册华为云,并完成实名认证
2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。 请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
4.已获取华为云账号对应的project ID。请在华为云控制台“我的凭证 > api凭证”页面上查看您的project ID。具体请参见 api凭证 。
5.已获取对应的可用区ID。具体请参见 可用分区 。
6..已创建MapReduce服务集群。获取对应的集群ID,集群概览页“集群ID”。
7.已具备开发环境 ,支持Java JDK 1.8及其以上版本。
3.代码示例
1.创建并执行作业,更多参数请参考API参考
Copied!
public class CreateExecuteJob {
private static final Logger logger = LoggerFactory.getLogger(CreateExecuteJob.class.getName());
public static void main(String[] args) {
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
String projectId = "{******your project id******}";
BasicCredentials auth = new BasicCredentials()
.withAk(ak)
.withSk(sk).withProjectId(projectId);
MrsClient client = MrsClient.newBuilder()
.withCredential(auth)
.withRegion(MrsRegion.valueOf("{******region id******}"))
.build();
List<String> arguments = new ArrayList<>();
arguments.add("{******your job arguments******}");
Map<String, String> properties = new HashMap<>();
properties.put("{******your properties key******}", "{******your properties value******}");
JobExecution jobExecution = new JobExecution()
.withJobName("{******your job name******}")
.withJobType("{******your job type******}")
.withArguments(arguments)
.withProperties(properties);
CreateExecuteJobRequest request = new CreateExecuteJobRequest()
.withClusterId("{******your cluster id******}")
.withBody(jobExecution);
try {
CreateExecuteJobResponse response = client.createExecuteJob(request);
System.out.println(String.valueOf(response.getHttpStatusCode()));
System.out.println(response.toString());
} catch (ClientRequestException e) {
System.out.println(e.getHttpStatusCode());
System.out.println(e.toString());
} catch (ServerResponseException e) {
System.out.println(e.getHttpStatusCode());
System.out.println(e.toString());
}
}
}
5.修订记录
发布日期 |
文档版本 |
修订说明 |
2022-03-04 |
1.0 |
文档首次发布 |
创建并执行作业示例(Java版本)
0.版本说明
本示例基于华为云SDK V3.0.78版本开发
1.功能介绍
在MRS集群中新增并提交一个作业。
需要先在集群详情页的“概览”页签,单击“IAM用户同步”右侧的“同步”进行IAM用户同步,然后再通过该接口提交作业。
如需使用OBS加密功能,请先参考“MRS用户指南 > 管理现有集群 > 作业管理 > 使用OBS加密数据运行作业”页面进行相关配置后,再调用API接口运行作业。
所有示例中涉及的OBS路径、样例文件及终端节点和AKSK,请提前准备并在提交请求时根据实际情况替换。
2.前置条件
1.已注册华为云,并完成实名认证
2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。 请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
4.已获取华为云账号对应的project ID。请在华为云控制台“我的凭证 > api凭证”页面上查看您的project ID。具体请参见 api凭证 。
5.已获取对应的可用区ID。具体请参见 可用分区 。
6..已创建MapReduce服务集群。获取对应的集群ID,集群概览页“集群ID”。
7.已具备开发环境 ,支持Java JDK 1.8及其以上版本。
3.代码示例
1.创建并执行作业,更多参数请参考API参考
public class CreateExecuteJob { private static final Logger logger = LoggerFactory.getLogger(CreateExecuteJob.class.getName()); public static void main(String[] args) { // 基础认证信息: // ak: 华为云账号Access Key // sk: 华为云账号Secret Access Key // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份认证为例,运行示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); // projectId: 项目ID String projectId = "{******your project id******}"; // 1.初始化sdk BasicCredentials auth = new BasicCredentials() .withAk(ak) .withSk(sk).withProjectId(projectId); // 2.创建MrsClient实例,regionId根据实际情况进行修改 MrsClient client = MrsClient.newBuilder() .withCredential(auth) .withRegion(MrsRegion.valueOf("{******region id******}")) .build(); //3.创建jobExecution,参数信息请根据实际情况填写 List<String> arguments = new ArrayList<>(); arguments.add("{******your job arguments******}"); Map<String, String> properties = new HashMap<>(); properties.put("{******your properties key******}", "{******your properties value******}"); JobExecution jobExecution = new JobExecution() .withJobName("{******your job name******}") .withJobType("{******your job type******}") .withArguments(arguments) .withProperties(properties); // 3.创建请求,集群ID请根据实际情况填写 CreateExecuteJobRequest request = new CreateExecuteJobRequest() .withClusterId("{******your cluster id******}") .withBody(jobExecution); //4.新建并执行作业 try { CreateExecuteJobResponse response = client.createExecuteJob(request); System.out.println(String.valueOf(response.getHttpStatusCode())); System.out.println(response.toString()); } catch (ClientRequestException e) { System.out.println(e.getHttpStatusCode()); System.out.println(e.toString()); } catch (ServerResponseException e) { System.out.println(e.getHttpStatusCode()); System.out.println(e.toString()); } } }
4.参考
更多信息请参考APIExplorer
5.修订记录