0.X SDK的使用

Linkis 提供了方便的JAVA和SCALA调用的接口,只需要引入ujes-client的模块就可以进行使用

1 引入依赖模块

  1. <dependency>
  2. <groupId>com.webank.wedatasphere.linkis</groupId>
  3. <artifactId>linkis-ujes-client</artifactId>
  4. <version>0.9.3</version>
  5. </dependency>

2 Java测试代码

建立Java的测试类UJESClientImplTestJ,具体接口含义可以见注释:

  1. package com.wedatasphere.linkis.ujes.test;
  2. import com.webank.wedatasphere.linkis.common.utils.Utils;
  3. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.StaticAuthenticationStrategy;
  4. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.TokenAuthenticationStrategy;
  5. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfig;
  6. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfigBuilder;
  7. import com.webank.wedatasphere.linkis.ujes.client.UJESClient;
  8. import com.webank.wedatasphere.linkis.ujes.client.UJESClientImpl;
  9. import com.webank.wedatasphere.linkis.ujes.client.request.JobExecuteAction;
  10. import com.webank.wedatasphere.linkis.ujes.client.request.ResultSetAction;
  11. import com.webank.wedatasphere.linkis.ujes.client.response.JobExecuteResult;
  12. import com.webank.wedatasphere.linkis.ujes.client.response.JobInfoResult;
  13. import com.webank.wedatasphere.linkis.ujes.client.response.JobProgressResult;
  14. import org.apache.commons.io.IOUtils;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.concurrent.TimeUnit;
  18. public class UJESClientTest {
  19. public static void main(String[] args){
  20. String user = "hadoop";
  21. String executeCode = "show databases;";
  22. // 1. 配置DWSClientBuilder,通过DWSClientBuilder获取一个DWSClientConfig
  23. DWSClientConfig clientConfig = ((DWSClientConfigBuilder) (DWSClientConfigBuilder.newBuilder()
  24. .addUJESServerUrl("http://${ip}:${port}") //指定ServerUrl,linkis服务器端网关的地址,如http://{ip}:{port}
  25. .connectionTimeout(30000) //connectionTimeOut 客户端连接超时时间
  26. .discoveryEnabled(false).discoveryFrequency(1, TimeUnit.MINUTES) //是否启用注册发现,如果启用,会自动发现新启动的Gateway
  27. .loadbalancerEnabled(true) // 是否启用负载均衡,如果不启用注册发现,则负载均衡没有意义
  28. .maxConnectionSize(5) //指定最大连接数,即最大并发数
  29. .retryEnabled(false).readTimeout(30000) //执行失败,是否允许重试
  30. .setAuthenticationStrategy(new StaticAuthenticationStrategy()) //AuthenticationStrategy Linkis认证方式
  31. .setAuthTokenKey("${username}").setAuthTokenValue("${password}"))) //认证key,一般为用户名; 认证value,一般为用户名对应的密码
  32. .setDWSVersion("v1").build(); //linkis后台协议的版本,当前版本为v1
  33. // 2. 通过DWSClientConfig获取一个UJESClient
  34. UJESClient client = new UJESClientImpl(clientConfig);
  35. try {
  36. // 3. 开始执行代码
  37. System.out.println("user : " + user + ", code : [" + executeCode + "]");
  38. Map<String, Object> startupMap = new HashMap<String, Object>();
  39. startupMap.put("wds.linkis.yarnqueue", "default"); // 在startupMap可以存放多种启动参数,参见linkis管理台配置
  40. JobExecuteResult jobExecuteResult = client.execute(JobExecuteAction.builder()
  41. .setCreator("linkisClient-Test") //creator,请求linkis的客户端的系统名,用于做系统级隔离
  42. .addExecuteCode(executeCode) //ExecutionCode 请求执行的代码
  43. .setEngineType((JobExecuteAction.EngineType) JobExecuteAction.EngineType$.MODULE$.HIVE()) // 希望请求的linkis的执行引擎类型,如Spark hive等
  44. .setUser(user) //User,请求用户;用于做用户级多租户隔离
  45. .setStartupParams(startupMap)
  46. .build());
  47. System.out.println("execId: " + jobExecuteResult.getExecID() + ", taskId: " + jobExecuteResult.taskID());
  48. // 4. 获取脚本的执行状态
  49. JobInfoResult jobInfoResult = client.getJobInfo(jobExecuteResult);
  50. int sleepTimeMills = 1000;
  51. while(!jobInfoResult.isCompleted()) {
  52. // 5. 获取脚本的执行进度
  53. JobProgressResult progress = client.progress(jobExecuteResult);
  54. Utils.sleepQuietly(sleepTimeMills);
  55. jobInfoResult = client.getJobInfo(jobExecuteResult);
  56. }
  57. // 6. 获取脚本的Job信息
  58. JobInfoResult jobInfo = client.getJobInfo(jobExecuteResult);
  59. // 7. 获取结果集列表(如果用户一次提交多个SQL,会产生多个结果集)
  60. String resultSet = jobInfo.getResultSetList(client)[0];
  61. // 8. 通过一个结果集信息,获取具体的结果集
  62. Object fileContents = client.resultSet(ResultSetAction.builder().setPath(resultSet).setUser(jobExecuteResult.getUser()).build()).getFileContent();
  63. System.out.println("fileContents: " + fileContents);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. IOUtils.closeQuietly(client);
  67. }
  68. IOUtils.closeQuietly(client);
  69. }
  70. }

运行上述的代码即可以和Linkis进行交互

3 Scala测试代码

  1. package com.wedatasphere.linkis.ujes.test
  2. import java.util.concurrent.TimeUnit
  3. import com.webank.wedatasphere.linkis.common.utils.Utils
  4. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.StaticAuthenticationStrategy
  5. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfigBuilder
  6. import com.webank.wedatasphere.linkis.ujes.client.UJESClient
  7. import com.webank.wedatasphere.linkis.ujes.client.request.JobExecuteAction.EngineType
  8. import com.webank.wedatasphere.linkis.ujes.client.request.{JobExecuteAction, ResultSetAction}
  9. import org.apache.commons.io.IOUtils
  10. object UJESClientImplTest extends App {
  11. var executeCode = "show databases;"
  12. var user = "hadoop"
  13. // 1. 配置DWSClientBuilder,通过DWSClientBuilder获取一个DWSClientConfig
  14. val clientConfig = DWSClientConfigBuilder.newBuilder()
  15. .addUJESServerUrl("http://${ip}:${port}") //指定ServerUrl,Linkis服务器端网关的地址,如http://{ip}:{port}
  16. .connectionTimeout(30000) //connectionTimeOut 客户端连接超时时间
  17. .discoveryEnabled(false).discoveryFrequency(1, TimeUnit.MINUTES) //是否启用注册发现,如果启用,会自动发现新启动的Gateway
  18. .loadbalancerEnabled(true) // 是否启用负载均衡,如果不启用注册发现,则负载均衡没有意义
  19. .maxConnectionSize(5) //指定最大连接数,即最大并发数
  20. .retryEnabled(false).readTimeout(30000) //执行失败,是否允许重试
  21. .setAuthenticationStrategy(new StaticAuthenticationStrategy()) //AuthenticationStrategy Linkis认证方式
  22. .setAuthTokenKey("${username}").setAuthTokenValue("${password}") //认证key,一般为用户名; 认证value,一般为用户名对应的密码
  23. .setDWSVersion("v1").build() //Linkis后台协议的版本,当前版本为v1
  24. // 2. 通过DWSClientConfig获取一个UJESClient
  25. val client = UJESClient(clientConfig)
  26. try {
  27. // 3. 开始执行代码
  28. println("user : " + user + ", code : [" + executeCode + "]")
  29. val startupMap = new java.util.HashMap[String, Any]()
  30. startupMap.put("wds.linkis.yarnqueue", "default") //启动参数配置
  31. val jobExecuteResult = client.execute(JobExecuteAction.builder()
  32. .setCreator("LinkisClient-Test") //creator,请求Linkis的客户端的系统名,用于做系统级隔离
  33. .addExecuteCode(executeCode) //ExecutionCode 请求执行的代码
  34. .setEngineType(EngineType.SPARK) // 希望请求的Linkis的执行引擎类型,如Spark hive等
  35. .setStartupParams(startupMap)
  36. .setUser(user).build()) //User,请求用户;用于做用户级多租户隔离
  37. println("execId: " + jobExecuteResult.getExecID + ", taskId: " + jobExecuteResult.taskID)
  38. // 4. 获取脚本的执行状态
  39. var jobInfoResult = client.getJobInfo(jobExecuteResult)
  40. val sleepTimeMills : Int = 1000
  41. while (!jobInfoResult.isCompleted) {
  42. // 5. 获取脚本的执行进度
  43. val progress = client.progress(jobExecuteResult)
  44. val progressInfo = if (progress.getProgressInfo != null) progress.getProgressInfo.toList else List.empty
  45. println("progress: " + progress.getProgress + ", progressInfo: " + progressInfo)
  46. Utils.sleepQuietly(sleepTimeMills)
  47. jobInfoResult = client.getJobInfo(jobExecuteResult)
  48. }
  49. if (!jobInfoResult.isSucceed) {
  50. println("Failed to execute job: " + jobInfoResult.getMessage)
  51. throw new Exception(jobInfoResult.getMessage)
  52. }
  53. // 6. 获取脚本的Job信息
  54. val jobInfo = client.getJobInfo(jobExecuteResult)
  55. // 7. 获取结果集列表(如果用户一次提交多个SQL,会产生多个结果集)
  56. val resultSetList = jobInfoResult.getResultSetList(client)
  57. println("All result set list:")
  58. resultSetList.foreach(println)
  59. val oneResultSet = jobInfo.getResultSetList(client).head
  60. // 8. 通过一个结果集信息,获取具体的结果集
  61. val fileContents = client.resultSet(ResultSetAction.builder().setPath(oneResultSet).setUser(jobExecuteResult.getUser).build()).getFileContent
  62. println("First fileContents: ")
  63. println(fileContents)
  64. } catch {
  65. case e: Exception => {
  66. e.printStackTrace()
  67. }
  68. }
  69. IOUtils.closeQuietly(client)
  70. }